文件填充数据另存为一个文件

时间:2021-02-03 14:46:02 电脑技巧 我要投稿

模板文件填充数据另存为一个文件

  今天我们来分享Excel模板代码的教程,看看如何打开一个Excel模板文件填充数据另存为一个文件。

  具体代码如下呈现:

  string strFileName = Environment.CurrentDirectory+@"/template/template.tpl";

  string strSaveFileName =

  Environment.CurrentDirectory+@"/excel/"+System.DateTime.Now.ToString().Replace(":","").Replace("-","").Replace(" ","")+@".xls";

  Excel.Application ThisApplication = new Excel.ApplicationClass();

  Excel.Workbook ThisWorkBook;

  object missing = System.Reflection.Missing.Value;

  try

  {

  //加载Excel模板文件

  ThisWorkBook = ThisApplication.Workbooks.Open(strFileName,missing,missing,missing,missing,missing,

  missing,missing,missing,missing,missing,missing,missing);

  Excel.Worksheet ThisSheet = (Excel.Worksheet)ThisWorkBook.Sheets[1];

  ThisSheet.Cells[7,1] = dtEnd.ToShortDateString();

  progressBar.Value = 30;

  ThisApplication.Visible = false;

  DataRow dr = dt.Rows[0];

  for( int i=0; i

  {

  ThisSheet.Cells[7,i+2] = dr[i].ToString();

  progressBar.Value +=1;

  }

  //更新数据后另存为新文件

  ThisSheet.SaveAs(strSaveFileName,missing,missing,missing,missing,missing,missing,missing,missing);

  }

  catch{}

  finally

  {

  ThisApplication.Quit();

  ThisWorkBook = null;

  ThisApplication = null;

  //dt = null;

  }

  try

  { //打开刚才生成的Excel文件

  Excel.Workbook NewWorkBook;

  NewWorkBook = NewApplication.Workbooks.Open(strSaveFileName,missing,missing,missing,missing,

  missing,missing,missing,missing,missing,missing,missing,missing);

  Excel.Worksheet NewSheet = (Excel.Worksheet)NewWorkBook.Sheets[1];

  NewApplication.Visible = true;

  //也可以使用System.Diagnostics.Process.Start(strSaveFileName);来打开新文件

【模板文件填充数据另存为一个文件】相关文章:

怎么解压文件10-24

u盘文件变成了乱码文件怎么办10-24

电脑文件隐藏技巧12-15

电脑如何共享文件12-09

如何启动电脑文件12-08

如何打开zip文件12-08

php文件如何打开12-06

WinRAR解压文件教程11-25

iso文件如何打开11-01

如何恢复误删文件10-19