感谢网站上的人们的帮助,我能够使用NPOI修改.xlsx文件。代码如下,但我的最终目标是能够修改.xlsm文件单元格值。有没有办法做到这一点?我尝试使用以下代码运行,但在运行程序后将无法打开文件。
XSSFWorkbook templateWorkbook;
using (FileStream fs = new FileStream(@"C:\Users\jzhu\Desktop\FlexTimeSheetTest.xlsm", FileMode.Open, FileAccess.ReadWrite))
{
templateWorkbook = new XSSFWorkbook(fs);
fs.Close();
}
XSSFSheet sheet = (XSSFSheet)templateWorkbook.GetSheet("Sheet1");
sheet.GetRow(0).GetCell(0).SetCellValue("Drago");
sheet.ForceFormulaRecalculation = true;
File.Delete(@"C:\Users\jzhu\Desktop\FlexTimeSheetTest.xlsm");
using (FileStream file = new FileStream(@"C:\Users\jzhu\Desktop\FlexTimeSheetTest.xlsm", FileMode.CreateNew, FileAccess.Write))
{
templateWorkbook.Write(file);
file.Close();
}
答案 0 :(得分:1)
答案 1 :(得分:0)
XSSFWorkbook xlWorkbook;
using (FileStream file = new FileStream(toEditFile,FileMode.Open, FileAccess.Read))
{
xlWorkbook = new XSSFWorkbook(file);
}
var xlWorksheet = xlWorkbook.GetSheet(@"sheet");
// do stuff
using (FileStream file = new FileStream(toEditFile, FileMode.Create, FileAccess.Write))
{
xlWorkbook.Write(file);
file.Close();
}