npoi icon indicating copy to clipboard operation
npoi copied to clipboard

XSSFWorkbook write issue

Open KoakiMiku opened this issue 6 months ago • 2 comments

NPOI Version

2.7.1

File Type

  • [x] XLSX
  • [ ] XLS
  • [ ] DOCX
  • [ ] XLSM
  • [ ] OTHER

Issue Description

I have a template excel file, when I use the following code and write to a new excel file, both "1.xlsx" and "2.xlsx" are modified. Is this behavior correct?

var templateFile = "./1.xlsx";
var newFile = "./2.xlsx";

using var workbook = new XSSFWorkbook(templateFile);
var sheet = workbook.GetSheetAt(0);
var baseCell = sheet.GetRow(1).GetCell(0);
var cell = sheet.CreateRow(2).CreateCell(0);
cell.SetCellValue("Test");
cell.CellStyle = baseCell.CellStyle;

using var fileStream = new FileStream(newFile, FileMode.OpenOrCreate);
workbook.Write(fileStream, false);

Now I am using the following code to avoid this issue.

var templateFile = "./1.xlsx";
var newFile = "./2.xlsx";

File.Copy(templateFile, newFile, true);
using var workbook = new XSSFWorkbook(newFile);
var sheet = workbook.GetSheetAt(0);
var baseCell = sheet.GetRow(1).GetCell(0);
var cell = sheet.CreateRow(2).CreateCell(0);
cell.SetCellValue("Test");
cell.CellStyle = baseCell.CellStyle;

workbook.Write(Stream.Null, false);

KoakiMiku avatar Jul 27 '24 04:07 KoakiMiku