1
下面是我的代碼讀取現有excel文件:無法讀取現有excel文件
FileInfo newFile = new FileInfo("C:\\Excel\\SampleStockTakeExceptionReport.xlsx");
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
var ws = xlPackage.Workbook.Worksheets.Add("Content");
ws.View.ShowGridLines = false;
ws.Column(4).OutlineLevel = 1;
ws.Column(4).Collapsed = true;
ws.Column(5).OutlineLevel = 1;
ws.Column(5).Collapsed = true;
ws.OutLineSummaryRight = true;
//Headers
ws.Cells["B1"].Value = "Name";
ws.Cells["C1"].Value = "Size";
ws.Cells["D1"].Value = "Created";
ws.Cells["E1"].Value = "Last modified";
ws.Cells["B1:E1"].Style.Font.Bold = true;
System.Diagnostics.Process.Start("C:\\Excel\\SampleStockTakeExceptionReport.xlsx");
}
當我運行的代碼。它會引發運行時錯誤。
錯誤。
System.InvalidOperationException: A worksheet with this name already exists in the workbook
at OfficeOpenXml.ExcelWorksheets.Add(String Name)
at Report.Form1.ExportToExcel1(DataTable Tbl, String ExcelFilePath) in C:\SMARTAG_PROJECT\SUREREACH\EXCEL\Report\Report\Form1.cs:line 43
我想讀我的文件....其報告模板..我想tloo然後在編輯我的模板,並保存在同一文件夾... –
第一個選項是好的,只需打開文件檢查工作表是否存在,是否存在編輯並保存,否則添加,編輯和保存。 – dutzu
是啊...我有問題編輯文件...在哪裏我的代碼無法編輯..因爲它會拋出錯誤,一旦他發現文件...你可以分享任何示例代碼。謝謝。 –