ASF Bugzilla – Attachment 36573 Details for
Bug 62444
Newline character in table header row cell causes damage to Excel file
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Testcase for bug 62444
file_62444.txt (text/plain), 6.86 KB, created by
David Gauntt
on 2019-05-07 12:55:52 UTC
(
hide
)
Description:
Testcase for bug 62444
Filename:
MIME Type:
Creator:
David Gauntt
Created:
2019-05-07 12:55:52 UTC
Size:
6.86 KB
patch
obsolete
>package org.apache.poi.ss.unfixedBugs; > >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.usermodel.CellStyle; >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.XSSFTableColumn; >import org.apache.poi.xssf.usermodel.XSSFWorkbook; >import org.junit.After; >import org.junit.Before; >import org.junit.Test; > >/** > * Test case for bug 62444 > */ >public class TestXSSFTableNewlineInHeader { > final File resultDir=new File("build/custom-reports-test"); > XSSFWorkbook workbook=null; > XSSFSheet sheet=null; > String fileName=null; > final int FIRST_ROW=2,FIRST_COL=2; > final int NUM_ROWS=3,NUM_COLS=4; > XSSFTable table=null; > > public TestXSSFTableNewlineInHeader() { > 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(); > sheet = workbook.createSheet(); > > // Create the table > table=addTable(sheet,FIRST_ROW,FIRST_COL,NUM_ROWS,NUM_COLS); > > // Turn on text wrapping in the first row > final XSSFRow row=sheet.getRow(FIRST_ROW); > final CellStyle style=workbook.createCellStyle(); > style.setWrapText(true); > for(int nCol=FIRST_COL;nCol<=FIRST_COL+NUM_COLS-1;++nCol) { > row.getCell(nCol).setCellStyle(style); > } > } > > > /** > * This method writes the workbook to resultDir/fileName. > */ > @After > public void cleanup() { > final String procName="TestXSSFTableRemoveColumn.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(); > } > } > } > > /** > * Put a \n in the header > */ > @Test > public void testNoLfInHeader() { > final String procName="testNoLfInHeader"; > fileName=procName+".xlsx"; > } > > /** > * Put a \n in the columnName but not the header cell > */ > @Test > public void testLfInColumnName() { > final String procName="testLfInColumnName"; > fileName=procName+".xlsx"; > > final XSSFTableColumn tblColumn=table.getColumns().get(0); > final String colName="Col\n1"; > tblColumn.setName(colName); > //sheet.getRow(FIRST_ROW).getCell(FIRST_COL).setCellValue(colName); > } > > /** > * Put a \n in the header cell but not the column name > */ > @Test > public void testLfInHeaderCell() { > final String procName="testLfInHeaderCell"; > fileName=procName+".xlsx"; > > final XSSFTableColumn tblColumn=table.getColumns().get(0); > final String colName="Col\n1"; > tblColumn.setName(colName); > // sheet.getRow(FIRST_ROW).getCell(FIRST_COL).setCellValue(colName); > } > > /** > * Put a \n in the columnName but not the header cell > */ > @Test > public void testLfInHeader() { > final String procName="testLfInHeader"; > fileName=procName+".xlsx"; > > final XSSFTableColumn tblColumn=table.getColumns().get(0); > final String colName="Col\n1"; > tblColumn.setName(colName); > sheet.getRow(FIRST_ROW).getCell(FIRST_COL).setCellValue(colName); > } > > > /** > * Put a \r in the header > */ > public void testCrInHeader() { > final String procName="testCrInHeader"; > fileName=procName+".xlsx"; > > final XSSFTableColumn tblColumn=table.getColumns().get(0); > final String colName="Col\r1"; > tblColumn.setName(colName); > sheet.getRow(FIRST_ROW).getCell(FIRST_COL).setCellValue(colName); > } > > /** > * Put a \r\n in the header > */ > public void testCrLfInHeader() { > final String procName="testCrLfInHeader"; > fileName=procName+".xlsx"; > > final XSSFTableColumn tblColumn=table.getColumns().get(0); > final String colName="Col\r\n1"; > tblColumn.setName(colName); > sheet.getRow(FIRST_ROW).getCell(FIRST_COL).setCellValue(colName); > } > > /** > * Add a table in the specified location and size, and fill the cells with some values. > * <p> > * 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); > > return 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 62444
: 36573 |
36574