public void TestNPOI() { string sourceFile = @"D:\sourceFile.xlsx"; string targetFile = @"D:\targetFile.xlsx"; IWorkbook workbook = new XSSFWorkbook(sourceFile); ISheet sheet1 = workbook.GetSheet("Sheet1"); sheet1.CreateRow(0).CreateCell(0).SetCellValue(1); sheet1.CreateRow(1).CreateCell(0).SetCellValue(2); sheet1.CreateRow(2).CreateCell(0).SetCellValue(3); FileStream fs = new FileStream(targetFile, FileMode.Create); workbook.Write(fs); workbook.Close(); }
public void TestEPPlus() { string sourceFile = @"D:\sourceFile.xlsx"; string targetFile = @"D:\targetFile.xlsx"; ExcelPackage.LicenseContext = LicenseContext.NonCommercial;//指明非商业应用 ExcelPackage package = new ExcelPackage(sourceFile);//加载Excel工作簿 ExcelWorksheet sheet1 = package.Workbook.Worksheets["Sheet1"];//读取工作簿中名为"Sheet1"的工作表 sheet1.Cells[1, 1].Value = "A";//设置单元格内容 sheet1.Cells[2, 2].Value = "B"; sheet1.Cells[3, 3].Value = "C"; sheet1.Cells[1, 2].Value = "1"; sheet1.Cells[2, 2].Value = "2"; sheet1.Cells[3, 2].Value = "3"; //package.Save();//将更改保存到原文件 package.SaveAs(targetFile);//将更改保存到新的文件,类似于另存为 }
public void TestSpireOffice() { string sourceFile = @"D:\sourceFile.xlsx"; string targetFile = @"D:\targetFile.xlsx"; Workbook workbook = new Workbook(); workbook.LoadFromFile(sourceFile);//加载Excel工作簿 Worksheet sheet1 = workbook.Worksheets["Sheet1"];//读取工作簿中名为"Sheet1"的工作表 sheet1.SetCellValue(1, 1, "A");//设置单元格内容 sheet1.SetCellValue(2, 1, "B"); sheet1.SetCellValue(3, 1, "C"); sheet1.SetCellValue(1, 2, "1"); sheet1.SetCellValue(2, 2, "2"); sheet1.SetCellValue(3, 2, "3"); workbook.Save();//将更改保存到原文件 workbook.SaveToFile(targetFile);//将更改保存到新的文件,类似于另存为 }