如何以编程方式从 Excel 工作表模板创建 Excel 工作表?
我正在从模板 Excel 工作表创建 Excel 工作表。
我有一个代码,
try
{
FileInfo newFile = new FileInfo(@"D:\ExcelFromTemplate.xlsx");
FileInfo template = new FileInfo(@"D:\template.xlsx");
using (ExcelPackage xlPackage = new ExcelPackage(newFile, template))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"];
ExcelCell cell = worksheet.Cell(5,1);
cell.Value = "15";
//worksheet.Cell(5, 1).Value = "Soap";
//xlPackage.Save();
Response.Write("Excel file created successfully");
}
}
catch (Exception ex)
{
Response.WriteFile(ex.InnerException.ToString());
}
此代码创建与模板 Excel 文件相同的新 Excel 文件,但无法添加单元格值。为什么?
我已经尝试了两种方法,如上面的单元格(5,1)代码。但是Excel工作表创建时没有写入单元格值。我们如何添加它。
I am creating a excel sheet from tempalte excel sheet.
I have a code
try
{
FileInfo newFile = new FileInfo(@"D:\ExcelFromTemplate.xlsx");
FileInfo template = new FileInfo(@"D:\template.xlsx");
using (ExcelPackage xlPackage = new ExcelPackage(newFile, template))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"];
ExcelCell cell = worksheet.Cell(5,1);
cell.Value = "15";
//worksheet.Cell(5, 1).Value = "Soap";
//xlPackage.Save();
Response.Write("Excel file created successfully");
}
}
catch (Exception ex)
{
Response.WriteFile(ex.InnerException.ToString());
}
this code creates the new excel file same as the template excel file but could not add the cell value. Why?
I had tried it with 2 ways as in above code for cell(5,1). But the excel sheet creates without writting cell value. How we can add it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保存文件以保留所做的更改。使用 save()
遇到了问题。该问题是 ExcelPackage 固有的。
同样,您必须打开每个工作表并进行一些更改才能保存。
搜索论坛以获取更多解释。
You need to save the file to persist the changes made. Using save()
Got the issue. The problem is inherent to ExcelPackage.
For the same you have to open each sheet and do some changes for it to get saved.
Search the forum for more explanation.