Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XSSFWorkbook write issue #1395

Open
1 of 5 tasks
KoakiMiku opened this issue Jul 27, 2024 · 2 comments
Open
1 of 5 tasks

XSSFWorkbook write issue #1395

KoakiMiku opened this issue Jul 27, 2024 · 2 comments
Labels

Comments

@KoakiMiku
Copy link

NPOI Version

2.7.1

File Type

  • 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 KoakiMiku added the bug label Jul 27, 2024
@wuzxc1230123
Copy link
Contributor

` public XSSFWorkbook(FileInfo file, bool readOnly = false)
: this(OPCPackage.Open(file, readOnly? PackageAccess.READ: PackageAccess.READ_WRITE))
{

}`

XSSFWorkbook READ_WRITE

@wuzxc1230123
Copy link
Contributor

`
public XSSFWorkbook(FileInfo file, bool readOnly = false)
: this(OPCPackage.Open(file, readOnly? PackageAccess.READ: PackageAccess.READ_WRITE))
{

}
`

templateFile create set

` public XSSFWorkbook(FileInfo file, bool readOnly = false)
: this(OPCPackage.Open(file, readOnly? PackageAccess.READ: PackageAccess.READ_WRITE))
{

}`

XSSFWorkbook new set readOnly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants