打开一个Excel模板文件填充数据另存为一个文件
时间:2022-10-16 15:49:09
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);来打开新文件
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用word制作电子手抄报
在word中怎么查看字数?word看字数方法介绍
![](https://img.aspxhome.com/file/2023/0/24240_0s.png)
Word2013图文混排编辑工具介绍
![](https://img.aspxhome.com/file/2023/5/29705_0s.jpg)
如何在word中打钩
![](https://img.aspxhome.com/file/2023/3/22033_0s.jpg)
在word中怎样制作灯泡发光效果
Word技巧:设置标题自动编号
excel设置显示批注教程
![](https://img.aspxhome.com/file/2023/4/36194_0s.jpg)
教育优惠活动赠送的 AirPods 可以添加 AppleCare 吗?
![](https://img.aspxhome.com/file/2023/1/a194781_0s.jpg)
word2003、2007怎么插入图片
![](https://img.aspxhome.com/file/2023/8/33658_0s.jpg)
excel删除空白页面
word 2019快速设置文字字体和字号的操作方法
![](https://img.aspxhome.com/file/2023/2/35192_0s.jpg)
Excel教程如何绘制盈利分析图
![](https://img.aspxhome.com/file/2023/7/38077_0s.png)
excel怎样同时调整图表中的所有字的大小和格式
![](https://img.aspxhome.com/file/2023/1/36281_0s.jpg)
word中如何设置分节符
word文档打开时出现提示发送错误报告怎么解决?
![](https://img.aspxhome.com/file/2023/8/26018_0s.jpg)
怎么让EXCEL文档打印时没有页边距
![](https://img.aspxhome.com/file/2023/9/36889_0s.png)
windows无法启动wlan autoconfig怎么办?windows无法启动wlan autoconfig教程
![](https://img.aspxhome.com/file/2023/6/47946_0s.png)
3种最给力的PDF转换Word方法分享
excel里如何输入千分号和万分号
![](https://img.aspxhome.com/file/2023/7/36077_0s.jpg)