git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1846489 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-11-13 08:20:47 +00:00
parent 0aa9b34d08
commit 6b7e322227
4 changed files with 75 additions and 52 deletions

View File

@ -36,8 +36,8 @@ public class CreateTable {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
try (Workbook wb = new XSSFWorkbook()) { try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = (XSSFSheet) wb.createSheet(); XSSFSheet sheet = wb.createSheet();
// Set which area the table should be placed in // Set which area the table should be placed in
AreaReference reference = wb.getCreationHelper().createAreaReference( AreaReference reference = wb.getCreationHelper().createAreaReference(

View File

@ -4109,18 +4109,17 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1; int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1;
// the id could already be taken after insertion/deletion of different tables // the id could already be taken after insertion/deletion of different tables
outerloop: boolean loop = true;
while(true) { while(loop) {
loop = false;
for (PackagePart packagePart : getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType())) { for (PackagePart packagePart : getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType())) {
String fileName = XSSFRelation.TABLE.getFileName(tableNumber); String fileName = XSSFRelation.TABLE.getFileName(tableNumber);
if(fileName.equals(packagePart.getPartName().getName())) { if(fileName.equals(packagePart.getPartName().getName())) {
// duplicate found, increase the number and start iterating again // duplicate found, increase the number and start iterating again
tableNumber++; tableNumber++;
continue outerloop; loop = true;
} }
} }
break;
} }
RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false); RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false);

View File

@ -55,10 +55,7 @@ import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
@ -1993,12 +1990,11 @@ public final class TestXSSFSheet extends BaseTestXSheet {
@Test @Test
public void testGetHeaderFooterProperties() throws IOException { public void testGetHeaderFooterProperties() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sh = wb.createSheet(); XSSFSheet sh = wb.createSheet();
XSSFHeaderFooterProperties hfProp = sh.getHeaderFooterProperties(); XSSFHeaderFooterProperties hfProp = sh.getHeaderFooterProperties();
assertNotNull(hfProp); assertNotNull(hfProp);
}
wb.close();
} }
} }

View File

@ -348,15 +348,45 @@ public final class TestXSSFTable {
IOUtils.closeQuietly(wb); IOUtils.closeQuietly(wb);
} }
@Test
public void testCreateTableIds() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
AreaReference reference1 = wb.getCreationHelper().createAreaReference(
new CellReference(0, 0), new CellReference(2, 2));
XSSFTable table1 = sheet.createTable(reference1);
assertEquals(1, table1.getCTTable().getTableColumns().getTableColumnArray(0).getId());
assertEquals(2, table1.getCTTable().getTableColumns().getTableColumnArray(1).getId());
assertEquals(3, table1.getCTTable().getTableColumns().getTableColumnArray(2).getId());
assertEquals(1, table1.getCTTable().getId());
AreaReference reference2 = wb.getCreationHelper().createAreaReference(
new CellReference(10, 10), new CellReference(12, 12));
XSSFTable table2 = sheet.createTable(reference2);
// these IDs dupplicate those from table1 and may be cause of https://bz.apache.org/bugzilla/show_bug.cgi?id=62906
assertEquals(1, table2.getCTTable().getTableColumns().getTableColumnArray(0).getId());
assertEquals(2, table2.getCTTable().getTableColumns().getTableColumnArray(1).getId());
assertEquals(3, table2.getCTTable().getTableColumns().getTableColumnArray(2).getId());
assertEquals(2, table2.getCTTable().getId());
}
}
@Test @Test
public void testSetArea() throws IOException { public void testSetArea() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sh = wb.createSheet(); XSSFSheet sh = wb.createSheet();
AreaReference tableArea = new AreaReference("B10:D12", wb.getSpreadsheetVersion()); AreaReference tableArea = new AreaReference("B10:D12", wb.getSpreadsheetVersion());
XSSFTable table = sh.createTable(tableArea); XSSFTable table = sh.createTable(tableArea);
assertEquals(3, table.getColumnCount()); assertEquals(3, table.getColumnCount());
assertEquals(3, table.getRowCount()); assertEquals(3, table.getRowCount());
@ -366,11 +396,11 @@ public final class TestXSSFTable {
assertEquals(3, table.getColumnCount()); assertEquals(3, table.getColumnCount());
assertEquals(3, table.getRowCount()); assertEquals(3, table.getRowCount());
// increase size by 1 row and 1 column // increase size by 1 row and 1 column
AreaReference tableArea3 = new AreaReference("B11:E14", wb.getSpreadsheetVersion()); AreaReference tableArea3 = new AreaReference("B11:E14", wb.getSpreadsheetVersion());
table.setArea(tableArea3); table.setArea(tableArea3);
assertEquals(4, table.getColumnCount()); assertEquals(4, table.getColumnCount());
assertEquals(4, table.getRowCount()); assertEquals(4, table.getRowCount());
@ -380,43 +410,41 @@ public final class TestXSSFTable {
assertEquals(2, table.getColumnCount()); assertEquals(2, table.getColumnCount());
assertEquals(2, table.getRowCount()); assertEquals(2, table.getRowCount());
}
IOUtils.closeQuietly(wb);
} }
@Test @Test
public void testCreateColumn() { public void testCreateColumn() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sh = wb.createSheet(); XSSFSheet sh = wb.createSheet();
AreaReference tableArea = new AreaReference("A2:A3", wb.getSpreadsheetVersion());
XSSFTable table = sh.createTable(tableArea);
assertEquals(1, table.getColumnCount()); AreaReference tableArea = new AreaReference("A2:A3", wb.getSpreadsheetVersion());
assertEquals(2, table.getRowCount()); XSSFTable table = sh.createTable(tableArea);
// add columns assertEquals(1, table.getColumnCount());
XSSFTableColumn c1 = table.getColumns().get(0); assertEquals(2, table.getRowCount());
XSSFTableColumn cB = table.createColumn("Column B");
XSSFTableColumn cD = table.createColumn("Column D");
XSSFTableColumn cC = table.createColumn("Column C", 2); // add between B and D
table.updateReferences();
table.updateHeaders();
assertEquals(4, table.getColumnCount()); // add columns
assertEquals(2, table.getRowCount()); XSSFTableColumn c1 = table.getColumns().get(0);
XSSFTableColumn cB = table.createColumn("Column B");
XSSFTableColumn cD = table.createColumn("Column D");
XSSFTableColumn cC = table.createColumn("Column C", 2); // add between B and D
table.updateReferences();
table.updateHeaders();
// column IDs start at 1, and increase in the order columns are added (see bug #62740) assertEquals(4, table.getColumnCount());
assertEquals("Column c ID", 1, c1.getId()); assertEquals(2, table.getRowCount());
assertTrue("Column B ID", c1.getId() < cB.getId());
assertTrue("Column D ID", cB.getId() < cD.getId());
assertTrue("Column C ID", cD.getId() < cC.getId());
assertEquals("Column 1", table.getColumns().get(0).getName()); // generated name
assertEquals("Column B", table.getColumns().get(1).getName());
assertEquals("Column C", table.getColumns().get(2).getName());
assertEquals("Column D", table.getColumns().get(3).getName());
IOUtils.closeQuietly(wb); // column IDs start at 1, and increase in the order columns are added (see bug #62740)
assertEquals("Column c ID", 1, c1.getId());
assertTrue("Column B ID", c1.getId() < cB.getId());
assertTrue("Column D ID", cB.getId() < cD.getId());
assertTrue("Column C ID", cD.getId() < cC.getId());
assertEquals("Column 1", table.getColumns().get(0).getName()); // generated name
assertEquals("Column B", table.getColumns().get(1).getName());
assertEquals("Column C", table.getColumns().get(2).getName());
assertEquals("Column D", table.getColumns().get(3).getName());
}
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)