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);来打开新文件
}
- 看不过瘾?点击下面链接!
- Excel如何让数据自动插入小数点
- Excel2013快速跳转至指定工作表
- Excel2013数据透视表字段分组
- excel快速输入大量含小数点的数字
- Excel 2013办公小技巧三则
- excel圆饼图怎么做?