技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 軟件教程 > Excel教程 >

Excel代碼教程 模板文件填充數(shù)據(jù)另存為一個(gè)文件

來源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2019-03-06 18:23┆點(diǎn)擊:

  今天我們來分享Excel模板代碼的教程,看看如何打開一個(gè)Excel模板文件填充數(shù)據(jù)另存為一個(gè)文件。

  具體代碼如下呈現(xiàn):

  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;

  }

  //更新數(shù)據(jù)后另存為新文件

  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);來打開新文件