package org.apache.poi.ss.usermodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Locale; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFName; 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.junit.After; import org.junit.Before; import org.junit.Test; public class TestXSSFSheetCreateTable { final File resultDir=new File("build/custom-reports-test"); XSSFWorkbook workbook=null; XSSFSheet sheet0=null; XSSFSheet sheet1=null; XSSFTable table2=null; XSSFTable table3=null; String fileName=null; boolean bDisable=false; public void TestCreateTable() { resultDir.mkdirs(); } /** * This creates a workbook with two worksheets. The second worksheet has * a range named "Table1", and two tables named "Table2" and "Table3" */ @Before public void setup() { workbook = new XSSFWorkbook(); sheet0 = workbook.createSheet(); sheet1 = workbook.createSheet(); // Create the named range, and fill it with its own name final XSSFName name=workbook.createName(); name.setNameName("Table1"); name.setRefersToFormula(sheet1.getSheetName()+"!$A$1"); name.setSheetIndex(1); final XSSFRow row=sheet1.createRow(0); final XSSFCell cell=row.createCell(0); cell.setCellValue("Table1"); // Create the two tables table2=addTable(sheet1,3,0,2,4); table3=addTable(sheet1,6,0,2,3); } /** * This method writes the workbook to resultDir/fileName. */ @After public void cleanup() { final String procName="TestXSSFSheetCreateTable.cleanup"; if (workbook == null) { System.out.println(String.format(Locale.ROOT,"%s: workbook==null",procName)); return; } if(fileName==null) { System.out.println(String.format(Locale.ROOT, "%s: fileName==null",procName)); return; } final File file=new File(resultDir,fileName); try (OutputStream fileOut = new FileOutputStream(file)) { workbook.write(fileOut); System.out.println(String.format(Locale.ROOT, "%s: test file written to %s",procName,file.getAbsolutePath())); } catch (Exception e) { System.err.println(e.getMessage()); } finally { try { workbook.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * Delete table2, and create a named range in sheet0; it should automatically be assigned the name "Table4" */ @Test public void testDefaultTableName() { final String procName="testDefaultTableName"; fileName=procName+".xlsx"; if(bDisable) { System.out.println("Disabling "+procName); return; } System.out.println(procName); // Deleting table2 corrupts the workbook; time for a new bug report! //sheet1.removeTable(table2); // Create the table; this should not throw an argument final XSSFTable table; try { table=addTable(sheet0,0,0,2,2); } catch(IllegalArgumentException err) { System.err.println(procName+": error in addTable: "+err.getMessage()); return; } assertEquals(table.getDisplayName(),"Table4"); } /** * Create a table in sheet0, and try to rename it Table1. This conflicts * with the named range in sheet1. */ @Test(expected=IllegalArgumentException.class) public void testRangeNameConflict() { final String procName="testRangeNameConflict"; fileName=procName+".xlsx"; // Create the table; this should not throw an argument final XSSFTable table; try { table=addTable(sheet0,0,0,2,2); } catch(IllegalArgumentException err) { System.err.println(procName+": error in addTable: "+err.getMessage()); return; } // Try to rename the table; this should throw an IllegalArgumentException table.setDisplayName("Table1"); } /** * Create a table in sheet0, and try to rename it Table2. This conflicts with * one of the tables in sheet1 */ @Test(expected=IllegalArgumentException.class) public void testTableNameConflict() { final String procName="testTableNameConflict"; fileName=procName+".xlsx"; // Create the table; this should not throw an argument final XSSFTable table; try { table=addTable(sheet0,0,0,2,2); } catch(IllegalArgumentException err) { System.err.println(procName+": error in addTable: "+err.getMessage()); return; } // Try to rename the table; this should throw an IllegalArgumentException table.setDisplayName("Table2"); } /** * Add a table in the specified location and size, and fill the cells with some values. *

* The header of the first column will be the name of the table * * @param sheet * @param nRow * @param nCol * @param nNumRows * @param nNumCols * @return */ private static XSSFTable addTable(XSSFSheet sheet,int nRow, int nCol, int nNumRows, int nNumCols) { final String procName="addTable"; for (int i = 0; i < nNumRows; i++) { XSSFRow row = sheet.createRow(i + nRow); for (int j = 0; j < nNumCols; j++) { XSSFCell localXSSFCell = row.createCell(j + nCol); if (i == 0) { localXSSFCell.setCellValue(String.format("Col%d", j + 1)); } else { localXSSFCell.setCellValue(String.format("(%d,%d)", i + 1, j + 1)); } } } final int nLastRow=nRow+nNumRows-1; final int nLastCol=nCol+nNumCols-1; final CellReference upperLeft = new CellReference(nRow,nCol); final CellReference lowerRight = new CellReference(nLastRow,nLastCol ); final AreaReference area = new AreaReference(upperLeft, lowerRight, SpreadsheetVersion.EXCEL2007); final XSSFTable table= sheet.createTable(area); sheet.getRow(nRow).getCell(nCol).setCellValue(table.getDisplayName()); return table; } }