Page 1 of 1

How to remove a sheet from a loaded XLSX template

PostPosted: Wed Mar 21, 2012 6:09 pm
by ikola
Hi Jason ,

Can some one you let me know how to remove or hide a sheet from the parsed XLSX template?

I need to remove some of the sheets from the existing template based on some configuration.

I tried as below but no luck ..

WorksheetPart sheet = worksheets.get(Integer.parseInt(sheetArray[0]));

Worksheet ws = sheet.getJaxbElement();
SheetViews sViews =ws.getSheetViews();

List<SheetView> listView = sViews.getSheetView();

for(int i=0; i<listView.size();i++)
{
listView.remove(i);
}



Appreciate your help.

Thanks,
IK

Re: How to remove a sheet from a loaded XLSX template

PostPosted: Mon Mar 26, 2012 9:45 pm
by jason
The workbook part (Part /xl/workbook.xml [org.docx4j.openpackaging.parts.SpreadsheetML.WorkbookPart] containing JaxbElement:org.xlsx4j.sml.Workbook) lists the worksheets:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  <fileVersion appName="xl" lastEdited="5" lowestEdited="4" rupBuild="9302"/>
  <workbookPr filterPrivacy="1" defaultThemeVersion="124226"/>
  <bookViews>
    <workbookView xWindow="240" yWindow="105" windowWidth="14805" windowHeight="8010" activeTab="1"/>
  </bookViews>
  <sheets>
    <sheet name="My Sheet1" sheetId="1" r:id="rId1"/>
    <sheet name="My Sheet2" sheetId="2" r:id="rId2"/>
    <sheet name="Sheet3" sheetId="3" r:id="rId3"/>
  </sheets>
  <calcPr calcId="122211"/>
</workbook>

 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



You'll need to remove the relevant sheet from the list of sheets.

The worksheet part will still exist within the package (its a rel of the workbook part). You don't need to remove the part in order to "hide" the sheet in Excel, but you may want to do so if it contains sensitive info or is large etc