ASF Bugzilla – Attachment 31063 Details for
Bug 55805
Error When Attempting to Save Workbook with New XSSF Table
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Contains code to create XSSF Table
TestXSSFTable.java (text/plain), 5.49 KB, created by
David Wilt
on 2013-11-21 16:36:46 UTC
(
hide
)
Description:
Contains code to create XSSF Table
Filename:
MIME Type:
Creator:
David Wilt
Created:
2013-11-21 16:36:46 UTC
Size:
5.49 KB
patch
obsolete
>/** David L. Wilt (dwilt@iotatx.com) >TestXSSFTable.java >Nov 17, 2013 > * > */ >package sample; > >import java.io.File; > >import java.io.FileInputStream; >import java.io.FileNotFoundException; >import java.io.IOException; >import java.io.InputStream; > >import org.apache.poi.hssf.util.AreaReference; >import org.apache.poi.hssf.util.CellReference; >import org.apache.poi.openxml4j.exceptions.InvalidFormatException; >import org.apache.poi.openxml4j.opc.OPCPackage; >import org.apache.poi.ss.usermodel.Cell; >import org.apache.poi.xssf.usermodel.XSSFRow; >import org.apache.poi.xssf.usermodel.XSSFSheet; >import org.apache.poi.xssf.usermodel.XSSFTable; >import org.apache.poi.xssf.usermodel.XSSFWorkbook; >import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable; >import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn; >import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns; > >/** > * @author david > * > */ >public class TestXSSFTable { > private XSSFSheet xwsSum = null; > private File fSum = null; > private String fsumName = null; > private XSSFWorkbook xWBSum = null; > /** > * > */ > public TestXSSFTable() { > // TODO Auto-generated constructor stub > } > > /** > * @param args > * @throws IOException > * @throws InvalidFormatException > * @throws FileNotFoundException > */ > // Call here for testing > public static void main(String[] args) throws FileNotFoundException, InvalidFormatException, IOException { > // Call this for testing > TestXSSFTable clTT = new TestXSSFTable(); > FileSelector clFS = new FileSelector(); > String fName = clFS.selectFile("xlsx"); > clTT.fsumName = fName; > //clFS.selectFile("xlsx"); > File f = new File(fName); > clTT.fSum = f; > XSSFWorkbook xWBSm = clTT.getWorkBook(f); > clTT.xWBSum = xWBSm; > XSSFSheet xwsSm = xWBSm.getSheet("Combinations"); > clTT.createTables(xwsSm); > > } > private void setCCTableContents(XSSFSheet wsSum, CTTable sumCTTable){ > XSSFRow rHeaderRow = wsSum.getRow(0); > int iCTCol = 0; > CTTableColumns cttabcols = sumCTTable.addNewTableColumns(); > for (Cell cell : rHeaderRow){ > CTTableColumn cttabcolumn = cttabcols.addNewTableColumn(); > cttabcolumn.setId(iCTCol); > cttabcolumn.setName(cell.getStringCellValue()); > System.out.println(" CTTable Col " + iCTCol + " " + cttabcolumn.getName()); > iCTCol ++; > } > } > // Call here for production use - Pass summary WS > private void createTables(XSSFSheet wsSum) throws IOException{ > XSSFTable sumTable = wsSum.createTable(); > sumTable.setName("Results"); > sumTable.setDisplayName(sumTable.getName()); > // Create CTTable > CTTable sumCTTable = sumTable.getCTTable(); > sumCTTable.setRef(createTableRef(wsSum).formatAsString()); > sumCTTable.setId(1000); > sumCTTable.setTotalsRowShown(false); > sumCTTable.setName("CTResults"); > sumCTTable.setHeaderRowCellStyle("Table Style Light 4"); > sumCTTable.setDataCellStyle("Table Style Light 4"); > setCCTableContents(wsSum,sumCTTable); > //sumTable.updateHeaders(); > SummarizeFoTResults clResults = new SummarizeFoTResults(); > clResults.writeFile(xWBSum, fsumName); > System.exit(0); > } > private AreaReference createTableRef(XSSFSheet wsSum){ > AreaReference NewRef = new AreaReference( > new CellReference(0, wsSum.getLeftCol()), > new CellReference(wsSum.getLastRowNum(), wsSum.getRow(0).getLastCellNum()) > ); > System.out.println("CTTable area ref " + NewRef); > return NewRef; > } > > private XSSFWorkbook getWorkBook(File f) throws FileNotFoundException, > IOException, InvalidFormatException { > System.out.println("WorkBook reading " + f.getAbsolutePath()); > InputStream is_Sum = new FileInputStream(f); > OPCPackage pkg = OPCPackage.open(is_Sum); > XSSFWorkbook wb = new XSSFWorkbook(pkg); > pkg.close(); > is_Sum.close(); > //System.out.println("Workbook " + f.getName().toString()); > return wb; > } > >} > >/** >following code works with 2007.. > >public static XSSFTable AddTableGeneric(XSSFSheet p_sheet, >String p_tableName, > List<String> p_listOfColumnNames, >int p_startRow, > int p_startCol, >int p_numRows, > TTPropBase p_fontProps) throws Exception >{ > >XSSFTable l_table = p_sheet.createTable(); > XSSFRow l_row = p_sheet.createRow(p_startRow); > > l_table.setName(p_tableName); >l_table.setDisplayName(p_tableName); > >CTTable l_cttable = l_table.getCTTable(); > CTTableStyleInfo addNewTableStyleInfo = l_cttable.addNewTableStyleInfo(); > > AreaReference NewRef = new AreaReference(new CellReference(p_startRow, >p_startCol), new CellReference((p_startRow + p_numRows - 1), p_startCol + >p_listOfColumnNames.size() - 1)); > l_cttable.setRef(NewRef.formatAsString()); >l_cttable.setId(++m_tableCount); > l_cttable.setTotalsRowShown(true); > > >l_cttable.setHeaderRowCellStyle("Table Style Light 4"); > >CTTableColumns cttabcols = l_cttable.addNewTableColumns(); > >int l_iCoumnCount = 1; > for (String l_ColonName : p_listOfColumnNames) >{ > CTTableColumn l_cttabcolumn = cttabcols.addNewTableColumn(); >l_cttabcolumn.setId(l_iCoumnCount); > l_cttabcolumn.setName(l_ColonName); >l_row.createCell((p_startCol - 1) + >l_iCoumnCount).setCellValue(l_ColonName); > l_iCoumnCount++; >} > >//Header Cell styles > if (p_fontProps != null) >{ > XSSFCellStyle l_cellStyle = getStyle(p_fontProps, p_sheet.getWorkbook()); > > XSSFRow l_headerRow = p_sheet.getRow(p_startRow); > > for (int l_i = 0; l_i < p_listOfColumnNames.size(); l_i++) >{ > l_headerRow.getCell(l_i + p_startCol).setCellStyle(l_cellStyle); >} > } > >return l_table; > } >*/
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 55805
: 31063 |
31064
|
31065