程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> c#打

c#打

編輯:關於C#
 

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

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved