简体   繁体   中英

Insert into Existing excel file using EPP on C#

I want to insert value on my excel,but i have a problem when i run my application, its create a new file excel,doesnt insert value into existing file,

 using (ExcelPackage excel = new ExcelPackage()) { excel.Workbook.Worksheets.Add("Worksheet1"); // Target a worksheet var worksheet = excel.Workbook.Worksheets["Worksheet1"]; worksheet.Cells[1, 1].Value = "Name"; worksheet.Cells[2, 1].Value = "ID"; FileInfo excelFile = new FileInfo(@"E:\ExcelTest.xls"); excel.SaveAs(excelFile);

I want my Program insert into existing file,not create a new one,

how i can solve this?

The EPPlus Wiki Getting Started covered this exact scenario.

FileInfo excelFile = new FileInfo(@"E:\ExcelTest.xls");
using (ExcelPackage excel = new ExcelPackage(excelFile))
{
      // Target a worksheet
      var worksheet = excel.Workbook.Worksheets["Worksheet1"];
      worksheet.Cells[1, 1].Value = "Name";
      worksheet.Cells[2, 1].Value = "ID";
      excel.Save();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM