use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn in project poi by apache.
the class CreateTable method main.
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) wb.createSheet();
//Create
XSSFTable table = sheet.createTable();
table.setDisplayName("Test");
CTTable cttable = table.getCTTable();
//Style configurations
CTTableStyleInfo style = cttable.addNewTableStyleInfo();
style.setName("TableStyleMedium2");
style.setShowColumnStripes(false);
style.setShowRowStripes(true);
//Set which area the table should be placed in
AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
cttable.setRef(reference.formatAsString());
cttable.setId(1);
cttable.setName("Test");
cttable.setTotalsRowCount(1);
CTTableColumns columns = cttable.addNewTableColumns();
columns.setCount(3);
CTTableColumn column;
XSSFRow row;
XSSFCell cell;
for (int i = 0; i < 3; i++) {
//Create column
column = columns.addNewTableColumn();
column.setName("Column");
column.setId(i + 1);
//Create row
row = sheet.createRow(i);
for (int j = 0; j < 3; j++) {
//Create cell
cell = row.createCell(j);
if (i == 0) {
cell.setCellValue("Column" + j);
} else {
cell.setCellValue("0");
}
}
}
FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn in project poi by apache.
the class XSSFTable method getXmlColumnPrs.
/**
* Note this list is static - once read, it does not notice later changes to the underlying column structures
* To clear the cache, call {@link #updateHeaders}
* @return List of XSSFXmlColumnPr
*/
public List<XSSFXmlColumnPr> getXmlColumnPrs() {
if (xmlColumnPr == null) {
xmlColumnPr = new ArrayList<XSSFXmlColumnPr>();
for (CTTableColumn column : getTableColumns()) {
if (column.getXmlColumnPr() != null) {
XSSFXmlColumnPr columnPr = new XSSFXmlColumnPr(this, column, column.getXmlColumnPr());
xmlColumnPr.add(columnPr);
}
}
}
return xmlColumnPr;
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn in project poi by apache.
the class TestXSSFTable method bug56274.
@Test
public void bug56274() throws IOException {
// read sample file
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56274.xlsx");
// read the original sheet header order
XSSFRow row = wb1.getSheetAt(0).getRow(0);
List<String> headers = new ArrayList<String>();
for (Cell cell : row) {
headers.add(cell.getStringCellValue());
}
// save the worksheet as-is using SXSSF
File outputFile = TempFile.createTempFile("poi-56274", ".xlsx");
SXSSFWorkbook outputWorkbook = new SXSSFWorkbook(wb1);
FileOutputStream fos = new FileOutputStream(outputFile);
outputWorkbook.write(fos);
fos.close();
outputWorkbook.close();
// re-read the saved file and make sure headers in the xml are in the original order
FileInputStream fis = new FileInputStream(outputFile);
XSSFWorkbook wb2 = new XSSFWorkbook(fis);
fis.close();
CTTable ctTable = wb2.getSheetAt(0).getTables().get(0).getCTTable();
CTTableColumn[] ctTableColumnArray = ctTable.getTableColumns().getTableColumnArray();
assertEquals("number of headers in xml table should match number of header cells in worksheet", headers.size(), ctTableColumnArray.length);
for (int i = 0; i < headers.size(); i++) {
assertEquals("header name in xml table should match number of header cells in worksheet", headers.get(i), ctTableColumnArray[i].getName());
}
assertTrue(outputFile.delete());
wb2.close();
wb1.close();
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn in project poi by apache.
the class XSSFTable method getCommonXpath.
/**
*
* Calculates the xpath of the root element for the table. This will be the common part
* of all the mapping's xpaths
* Note: this function caches the result for performance. To flush the cache {@link #updateHeaders()} must be called.
*
* @return the xpath of the table's root element
*/
public String getCommonXpath() {
if (commonXPath == null) {
String[] commonTokens = {};
for (CTTableColumn column : getTableColumns()) {
if (column.getXmlColumnPr() != null) {
String xpath = column.getXmlColumnPr().getXpath();
String[] tokens = xpath.split("/");
if (commonTokens.length == 0) {
commonTokens = tokens;
} else {
final int maxLength = Math.min(commonTokens.length, tokens.length);
for (int i = 0; i < maxLength; i++) {
if (!commonTokens[i].equals(tokens[i])) {
List<String> subCommonTokens = Arrays.asList(commonTokens).subList(0, i);
String[] container = {};
commonTokens = subCommonTokens.toArray(container);
break;
}
}
}
}
}
commonTokens[0] = "";
commonXPath = StringUtil.join(commonTokens, "/");
}
return commonXPath;
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn in project poi by apache.
the class XSSFTable method findColumnIndex.
/**
* Gets the relative column index of a column in this table having the header name <code>column</code>.
* The column index is relative to the left-most column in the table, 0-indexed.
* Returns <code>-1</code> if <code>column</code> is not a header name in table.
*
* Column Header names are case-insensitive
*
* Note: this function caches column names for performance. To flush the cache (because columns
* have been moved or column headers have been changed), {@link #updateHeaders()} must be called.
*
* @since 3.15 beta 2
*/
public int findColumnIndex(String columnHeader) {
if (columnHeader == null)
return -1;
if (columnMap == null) {
// FIXME: replace with org.apache.commons.collections.map.CaseInsensitiveMap
final int count = getTableColumns().length;
columnMap = new HashMap<String, Integer>(count * 3 / 2);
int i = 0;
for (CTTableColumn column : getTableColumns()) {
String columnName = column.getName();
columnMap.put(caseInsensitive(columnName), i);
i++;
}
}
// Table column names with special characters need a single quote escape
// but the escape is not present in the column definition
Integer idx = columnMap.get(caseInsensitive(columnHeader.replace("'", "")));
return idx == null ? -1 : idx.intValue();
}
Aggregations