Quantcast
Viewing all articles
Browse latest Browse all 1621

Edited Issue: SetSheetOrder breaks XLSX [13142]

NPOI 2.1
XSSFWorkbook.cs

Issue:
When setting the sheet order (i.e. moving a worksheet) with XSSF, the XLSX file will break and cannot be opened with Excel.

Reason:
CT_Sheet.Set will set the SheetId of the moved sheet to 0, which the OpenXML format does not allow.

our workaround:
keep the sheetId
```
public void SetSheetOrder(String sheetname, int pos)
{
int idx = GetSheetIndex(sheetname);
XSSFSheet sheet = sheets[idx];
sheets.RemoveAt(idx);
sheets.Insert(pos,sheet);
// Reorder CT_Sheets
CT_Sheets ct = workbook.sheets;
CT_Sheet cts = ct.GetSheetArray(idx).Copy();

// get sheetid from sheet to be moved
uint Mysheetid = ct.GetSheetArray(idx).sheetId;

workbook.sheets.RemoveSheet(idx);
CT_Sheet newcts = ct.InsertNewSheet(pos);
newcts.Set(cts);

// retrieve sheetid
newcts.sheetId = Mysheetid;

//notify sheets
for (int i = 0; i < sheets.Count; i++)
{
sheets[i].sheet = ct.GetSheetArray(i);
}
}
```

Viewing all articles
Browse latest Browse all 1621

Trending Articles