Right now, I'm trying to modify the following labels from an existing .xlsx
Graph Image:
The graph is already modified their formulas and values for the ones I want, but those numbers are still getting their value from previous values in the graph. How can I change them? I was looking for previous questions, and supposedly the method to use to get the current label values (before modifying them) would be the following one:
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(2).getVal().getNumRef().getNumCache()
The drawing obtained is the following one from my sheet:
XSSFDrawing drawing = sheet.createDrawingPatriarch();
But I get a list with 107 values... So I'm not sure if it's correct or not. I don't know what do I need to modify. I would appreciate some help please.
Minimal example about how did I modify the graph:
This excel sheet has five seriesList with a formula based on other excel sheets. So I did the following code:
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(0).getVal().getNumRef().setF("PERD_POLICY!$S$15:$S$" + lineasPerdPolicy + "");
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(0).getCat().getNumRef().setF("PERD_POLICY!$Q$15:$Q$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(1).getVal().getNumRef().setF("PERD_POLICY!$T$15:$T$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(1).getCat().getNumRef().setF("PERD_POLICY!$Q$15:$Q$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(2).getVal().getNumRef().setF("PERD_POLICY!$U$15:$U$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(2).getCat().getNumRef().setF("PERD_POLICY!$Q$15:$Q$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(3).getVal().getNumRef().setF("PERD_POLICY!$V$15:$V$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(3).getCat().getNumRef().setF("PERD_POLICY!$Q$15:$Q$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(4).getVal().getNumRef().setF("PERD_POLICY!$W$15:$W$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(4).getCat().getNumRef().setF("PERD_POLICY!$Q$15:$Q$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(5).getVal().getNumRef().setF("PERD_POLICY!$R$15:$R$" + lineasPerdPolicy);
drawing.getCharts().get(0).getCTChart().getPlotArea().getLineChartList().get(0).getSerList().get(5).getCat().getNumRef().setF("PERD_POLICY!$Q$15:$Q$" + lineasPerdPolicy);
lineasPerdPolicy is a variable I used to count the last row in the sheet we are getting the values from. The sheet "PERD_POLICY" . This graph is based on months from years. I added now to the current serList a new value, until December 2019. But the last label of the green chart, is showing 9,66. That value is from October 2019.
I think you will get it better with the following images. This one shows what is the value of the last label:
The current label value
And the selected value in this other picture is the one I want to show in the label, 9,75
The graph value I want to show in the label
If you don't understand any word please let me know, because my excel is in spanish.
Valor --> Value
Punto --> Point
Your code using the low level ooxml-schemas classes only updates the reference formulas of the series. It does not update the cached values in the chart.
Since the current apache poi 4.1.1 provides XDDFChartData.Series.replaceData to update the chart's data, we should use this instead of the low level ooxml-schemas classes.
Let's have a complete example to show how to do this.
We start having a ExcelWithChartMar.xlsx looking like so:
As you see there are the chart data in A1:D4 for months Jan to Mar already and a chart showing those data.
What we need to know: The first data row is 1 (row 0 is header row) and the current last data row is 3. The last data row will increase. The category column is 0 (A) and the series columns are 1 (B),2 (C) and 3 (D). Note, all indexes are 0 based.
Now we can run the following code using apache poi 4.1.1:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
class ExcelChangeChartData {
static void updateChartData(XSSFChart chart, XSSFSheet dataSheet,
int firstDataRow, int lastDataRow, int categoryColumn, int[] seriesColumns) {
for (XDDFChartData chartData : chart.getChartSeries()) {
for (int s = 0; s < chartData.getSeriesCount() ; s++) {
XDDFChartData.Series series = chartData.getSeries(s);
if (seriesColumns.length > s) {
XDDFCategoryDataSource category = XDDFDataSourcesFactory.fromStringCellRange(
dataSheet, new CellRangeAddress(firstDataRow, lastDataRow, categoryColumn, categoryColumn));
int seriesColumn = seriesColumns[s];
XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(
dataSheet, new CellRangeAddress(firstDataRow, lastDataRow, seriesColumn, seriesColumn));
series.replaceData(category, values);
series.plot();
}
}
}
}
public static void main(String[] args) throws Exception {
String[] months = new String[]{"Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int firstDataRow = 1;
int lastDataRow = 3;
int categoryColumn = 0;
int[] seriesColumns = new int[]{1,2,3};
for (int m = 0; m < months.length - 1; m++) {
String monthSource = months[m];
String monthResult = months[m+1];
String filePath = "./ExcelWithChart" + monthSource + ".xlsx";
java.util.Random random = new java.util.Random();
XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream(filePath));
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.createRow(lastDataRow + 1);
XSSFCell cell = row.createCell(categoryColumn);
cell.setCellValue(monthResult);
for (int i = 0; i < seriesColumns.length; i++) {
cell = row.createCell(seriesColumns[i]);
cell.setCellValue(random.nextDouble() / 10 + 0.02);
cell.setCellStyle(sheet.getRow(lastDataRow).getCell(seriesColumns[i]).getCellStyle());
}
lastDataRow++;
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFChart chart = drawing.getCharts().get(0);
updateChartData(chart, sheet, firstDataRow, lastDataRow, categoryColumn, seriesColumns);
filePath = "./ExcelWithChart" + monthResult + ".xlsx";
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
}
}
This creates 9 additional Excel files ExcelWithChartApr.xlsx ... ExcelWithChartDec.xlsx where each has a new month's data added.
The method updateChartData updates the chart data using the XDDFChartData.Series.replaceData method.
Related
I've got an table in excel with formulae I would like to add data to.
My motivation for this is the fact that tables in excel can dynamically expand to the range of data you add to them, meaning that the formula rows automatically keep up with the amount of data rows.
I'm however having a hard time finding out if this is possible using apache-POI.
One thing I was going to try (see code below) was to expand the AreaReference of the table to cover the data, however both AreaReference(CR,CR2); (as used in this example) and AreaReference(CR,CR2, SpreadsheetVersion.EXCEL2007) (seen in the apache docs) give "constructor is undefined".
No idea what is causing that constructor error as I do have org.apache.poi.ss.util imported.
The other option on the apache docs AreaReference(java.lang.String reference) lets me compile and run but instead gives a "NoSuchMethod" error.
List<XSSFTable> tableList = spreadSheet.getTables();
CellReference CR = new CellReference(0, 0);
CellReference CR2 = new CellReference(5, 2);
AreaReference my_data_range = new AreaReference(CR,CR2);
tableList.get(0).setArea(my_data_range);
Any help will be appreciated.
The main problem using apache poi until now is that it is not ready to be used without having detailed knowledge about Microsoft Office as such and about the storage of Microsoft Office files. There are many things only half way ready and there are regressions often in new versions (bugs occur again which were solved already).
So your requirement: "Expanding an existing table in Excel using Apache POI" is not possible only simply using apache poi. One must know that Office Open XML files *.xlsx are simply ZIP archives which can be unzipped. And after unzipping we find /xl/tables/table1.xml for storage of the table. This XML we can analyzing and comparing it with XML which was created using Excel's GUI. So we can find problems which results from shortcomings of apache poi. Same is with the sheet's XML in /xl/tables/sheet1.xml.
Also we need to know that apache poi builds on the low level classes of ooxml-schemas. Partially we need using those classes because of the halfway readiness of apache poi. In the following example we need ooxml-schemas-1.4.jar additionally because apache poi's poi-ooxml-schemas-4.0.0.jar has not included org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableFormula until now. Unfortunately there is no documentation about ooxml-schemas public available. So we need downloading the sources and doing javadoc our own.
The following example works for me using apache poi 4.0.0. If you get problems while compiling or running, the reason might be that multiple different versions of apache poi jars are in class path while compile time and/or run time. Do not mix different apache poi versions. Also, as said already, my code needs the full jar of all of the schemas ooxml-schemas-1.4.jar.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.SpreadsheetVersion;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
class ExcelExpandingTable {
static void addRowToTable(XSSFTable table) {
int lastTableRow = table.getEndCellReference().getRow();
int totalsRowCount = table.getTotalsRowCount();
int lastTableDataRow = lastTableRow - totalsRowCount;
// we will add one row in table data
lastTableRow++;
lastTableDataRow++;
// new table area plus one row
AreaReference newTableArea = new AreaReference(
table.getStartCellReference(),
new CellReference(
lastTableRow,
table.getEndCellReference().getCol()
),
SpreadsheetVersion.EXCEL2007
);
// new table data area plus one row
AreaReference newTableDataArea = new AreaReference(
table.getStartCellReference(),
new CellReference(
lastTableDataRow,
table.getEndCellReference().getCol()
),
SpreadsheetVersion.EXCEL2007
);
XSSFSheet sheet = table.getXSSFSheet();
if (totalsRowCount > 0) {
//if we have totals rows, shift totals rows down
sheet.shiftRows(lastTableDataRow, lastTableRow, 1);
// correcting bug that shiftRows does not adjusting references of the cells
// if row 3 is shifted down, then reference in the cells remain r="A3", r="B3", ...
// they must be adjusted to the new row thoug: r="A4", r="B4", ...
// apache poi 3.17 has done this properly but had have other bugs in shiftRows.
for (int r = lastTableDataRow; r < lastTableRow + 1; r++) {
XSSFRow row = sheet.getRow(r);
if (row != null) {
long rRef = row.getCTRow().getR();
for (Cell cell : row) {
String cRef = ((XSSFCell)cell).getCTCell().getR();
((XSSFCell)cell).getCTCell().setR(cRef.replaceAll("[0-9]", "") + rRef);
}
}
}
// end correcting bug
}
// if there are CalculatedColumnFormulas do filling them to the new row
XSSFRow row = sheet.getRow(lastTableDataRow); if (row == null) row = sheet.createRow(lastTableDataRow);
int firstTableCol = table.getStartCellReference().getCol();
for (CTTableColumn tableCol : table.getCTTable().getTableColumns().getTableColumnList()) {
if (tableCol.getCalculatedColumnFormula() != null) {
int id = (int)tableCol.getId();
String formula = tableCol.getCalculatedColumnFormula().getStringValue();
XSSFCell cell = row.getCell(firstTableCol + id - 1); if (cell == null) cell = row.createCell(firstTableCol + id - 1);
cell.setCellFormula(formula);
}
}
table.setArea(newTableArea);
// correcting bug that Autofilter includes possible TotalsRows after setArea new
// Autofilter must only contain data area
table.getCTTable().getAutoFilter().setRef(newTableDataArea.formatAsString());
// end correcting bug
table.updateReferences();
}
public static void main(String[] args) throws Exception {
try (Workbook workbook = WorkbookFactory.create(new FileInputStream("SAMPLE.xlsx"));
FileOutputStream out = new FileOutputStream("SAMPLE_NEW.xlsx")) {
XSSFSheet sheet = ((XSSFWorkbook)workbook).getSheetAt(0);
XSSFTable table = sheet.getTables().get(0);
addRowToTable(table);
workbook.write(out);
}
}
}
The above was 2018. Now we have 2022.
Using current apache poi 5.2.2 the code may be like follows:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.SpreadsheetVersion;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
class ExcelExpandingTable {
static XSSFRow addRowToTable(XSSFTable table) {
int lastTableRow = table.getEndCellReference().getRow();
int totalsRowCount = table.getTotalsRowCount();
int lastTableDataRow = lastTableRow - totalsRowCount;
int firstTableCol = table.getStartCellReference().getCol();
// we will add one row in table data
lastTableRow++;
lastTableDataRow++;
// new table area plus one row
AreaReference newTableArea = new AreaReference(
table.getStartCellReference(),
new CellReference(
lastTableRow,
table.getEndCellReference().getCol()
),
SpreadsheetVersion.EXCEL2007
);
XSSFSheet sheet = table.getXSSFSheet();
if (totalsRowCount > 0) {
//if we have totals rows, shift totals rows down
sheet.shiftRows(lastTableDataRow, lastTableRow, 1);
//correct all sheet table-reference-formulas which probably got damaged after shift rows
for (CTTableColumn tableCol : table.getCTTable().getTableColumns().getTableColumnList()) {
if (tableCol.getCalculatedColumnFormula() != null) {
int id = (int)tableCol.getId();
String formula = tableCol.getCalculatedColumnFormula().getStringValue();
int rFirst = table.getStartCellReference().getRow() + table.getHeaderRowCount();
int rLast = table.getEndCellReference().getRow() - table.getTotalsRowCount();
int c = table.getStartCellReference().getCol() + id - 1;
sheet.getWorkbook().setCellFormulaValidation(false);
for (int r = rFirst; r <= rLast; r++) {
XSSFRow row = sheet.getRow(r); if (row == null) row = sheet.createRow(r);
XSSFCell cell = row.getCell(c); if (cell == null) cell = row.createCell(c);
cell.setCellFormula(formula);
}
}
}
}
// if there are CalculatedColumnFormulas do filling them to the new row
XSSFRow row = sheet.getRow(lastTableDataRow); if (row == null) row = sheet.createRow(lastTableDataRow);
for (CTTableColumn tableCol : table.getCTTable().getTableColumns().getTableColumnList()) {
if (tableCol.getCalculatedColumnFormula() != null) {
int id = (int)tableCol.getId();
String formula = tableCol.getCalculatedColumnFormula().getStringValue();
XSSFCell cell = row.getCell(firstTableCol + id - 1); if (cell == null) cell = row.createCell(firstTableCol + id - 1);
cell.getSheet().getWorkbook().setCellFormulaValidation(false); // see https://bz.apache.org/bugzilla/show_bug.cgi?id=66039
cell.setCellFormula(formula);
}
}
// copy cell styles to the new row from the row above
row = sheet.getRow(lastTableDataRow); if (row == null) row = sheet.createRow(lastTableDataRow);
XSSFRow rowAbove = sheet.getRow(lastTableDataRow - 1); if (row == null) row = sheet.createRow(lastTableDataRow - 1);
for (CTTableColumn tableCol : table.getCTTable().getTableColumns().getTableColumnList()) {
int id = (int)tableCol.getId();
XSSFCell cellAbove = rowAbove.getCell(firstTableCol + id - 1);
if (cellAbove != null) {
XSSFCellStyle styleAbove = cellAbove.getCellStyle();
XSSFCell cell = row.getCell(firstTableCol + id - 1); if (cell == null) cell = row.createCell(firstTableCol + id - 1);
cell.setCellStyle(styleAbove);
}
}
// set new table area
table.setArea(newTableArea);
// update table references
table.updateReferences();
return sheet.getRow(lastTableDataRow);
}
public static void main(String[] args) throws Exception {
try (Workbook workbook = WorkbookFactory.create(new FileInputStream("SAMPLE.xlsx"));
FileOutputStream out = new FileOutputStream("SAMPLE_NEW.xlsx")) {
XSSFSheet sheet = ((XSSFWorkbook)workbook).getSheetAt(0);
XSSFTable table = sheet.getTables().get(0);
XSSFRow row = addRowToTable(table);
workbook.write(out);
}
}
}
According to FAQ, this code needs poi-ooxml-full-5.2.2.jar instead of ooxml-schemas-1.4.jar now for run using apache poi 5.2.2.
Some bugs are fixed. But there is one new bug when using XSSFCell.setCellFormula when formula contains table references. But XSSFWorkbook.setCellFormulaValidation(false) avoids this.
Have small problem when I want to set formula to cell by column name format. Let me show example:
In excel file I can do something like this =[Name]
So value from colmn B is copied to column A
But when I try to do something like this in Apache POI
cell.setCellFormula("[Name]");
I get exception:
Parse error near char 0 '[' in specified formula '[Name]'. Expected number, string, defined name, or data table"
How can I handle such situation?
The formula =[Name] is a structured reference to an Excel table. But it is a very short unqualified form. The fully qualified form would be =tableName[[#This Row],[Name]], And that fully qualified form also will be stored and so must be set using apache poi.
Let's have a complete example:
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
class CreateExcelTableUsingStructuredReferences {
public static void main(String[] args) throws Exception {
try (XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("./Excel.xlsx") ) {
//data
String[] tableHeadings = new String[]{"Id", "Name", "Description"};
String[] tableContent = new String[]{null, "Name1", "Description1"};
//variables
String tableName = "Table1";
int firstRow = 0; //start table in row 1
int firstCol = 0; //start table in column A
int rows = 2; //we have to populate headings row and 1 data row
int cols = 3; //three columns in each row
//prepairing the sheet
XSSFSheet sheet = workbook.createSheet();
//set sheet content
for (int r = 0; r < rows; r++) {
XSSFRow row = sheet.createRow(firstRow+r);
for (int c = 0; c < cols; c++) {
XSSFCell localXSSFCell = row.createCell(firstCol+c);
if (r == 0) {
localXSSFCell.setCellValue(tableHeadings[c]);
} else {
localXSSFCell.setCellValue(tableContent[c]);
}
}
}
//create the table
CellReference topLeft = new CellReference(sheet.getRow(firstRow).getCell(firstCol));
CellReference bottomRight = new CellReference(sheet.getRow(firstRow+rows-1).getCell(firstCol+cols-1));
AreaReference tableArea = workbook.getCreationHelper().createAreaReference(topLeft, bottomRight);
XSSFTable dataTable = sheet.createTable(tableArea);
dataTable.setName(tableName);
dataTable.setDisplayName(tableName);
//set table column formula
dataTable.getCTTable().getTableColumns().getTableColumnList().get(0).addNewCalculatedColumnFormula().setStringValue(
tableName + "[[#This Row],[Name]]");
//set the formula in sheet
XSSFCell formulaCell = sheet.getRow(firstRow+1).getCell(firstCol);
formulaCell.setCellFormula(tableName + "[[#This Row],[Name]]");
//following is not necessary up to apache poi 5.1.0, but later versions of apache poi uses formula parser which damages structured table formulas
formulaCell.getCTCell().getF().setStringValue(tableName + "[[#This Row],[Name]]");
workbook.write(fileout);
}
}
}
This works using apache poi 4 or later.
Note, the additional formulaCell.getCTCell().getF().setStringValue(tableName + "[[#This Row],[Name]]"); is only needed using apache poi versions after 5.1.0. Later versions of apache poi uses a formula parser which damages the structured table formula and so it must be reset using this code line.
I'm trying to create a sheet with progress bar which will represent some progress.
I'm using these libraries:
org.apache.poi:poi:4.1.0
org.apache.poi:poi-ooxml:4.1.0
org.apache.poi:poi-ooxml-schemas:4.1.0
All I get is a progress bar with gradient, but I need a progress bar with solid color instead color scale.
All conditional formatting data bars defined in Office Open XML are using gradient colors. There is not even a attribute or property to change that. Later Excel versions are using extensions from namespace x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main". But those are not part of the Ecma Office Open XML File Formats Standard.
Apache poi until now only bases on Ecma Office Open XML File Formats Standard. So the only way to make solid color for conditional formatting data bar in apache poi is creating the XML of the extended x14 data bar conditional formatting from scratch.
Complete example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFDataBarFormatting;
import org.apache.poi.xssf.usermodel.XSSFConditionalFormattingRule;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
public class ConditionalFormattingDataBars {
public static void applyDataBars(SheetConditionalFormatting sheetCF, String region, ExtendedColor color) throws Exception {
CellRangeAddress[] regions = { CellRangeAddress.valueOf(region) };
ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(color);
DataBarFormatting dbf = rule.getDataBarFormatting();
dbf.getMinThreshold().setRangeType(ConditionalFormattingThreshold.RangeType.NUMBER);
dbf.getMinThreshold().setValue(0d);
dbf.getMaxThreshold().setRangeType(ConditionalFormattingThreshold.RangeType.NUMBER);
dbf.getMaxThreshold().setValue(100d);
dbf.setIconOnly(true);
dbf.setWidthMin(0); //cannot work for XSSFDataBarFormatting, see https://svn.apache.org/viewvc/poi/tags/REL_4_0_1/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java?view=markup#l57
dbf.setWidthMax(100); //cannot work for XSSFDataBarFormatting, see https://svn.apache.org/viewvc/poi/tags/REL_4_0_1/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java?view=markup#l64
if (dbf instanceof XSSFDataBarFormatting) {
Field _databar = XSSFDataBarFormatting.class.getDeclaredField("_databar");
_databar.setAccessible(true);
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar ctDataBar =
(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar)_databar.get(dbf);
ctDataBar.setMinLength(0);
ctDataBar.setMaxLength(100);
}
// use extension from x14 namespace to set data bars not using gradient color
if (rule instanceof XSSFConditionalFormattingRule) {
Field _cfRule = XSSFConditionalFormattingRule.class.getDeclaredField("_cfRule");
_cfRule.setAccessible(true);
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule ctRule =
(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule)_cfRule.get(rule);
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtensionList extList =
ctRule.addNewExtLst();
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtension ext = extList.addNewExt();
String extXML =
"<x14:id"
+ " xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\">"
+ "{00000000-000E-0000-0000-000001000000}"
+ "</x14:id>";
org.apache.xmlbeans.XmlObject xlmObject = org.apache.xmlbeans.XmlObject.Factory.parse(extXML);
ext.set(xlmObject);
ext.setUri("{B025F937-C7B1-47D3-B67F-A62EFF666E3E}");
Field _sh = XSSFConditionalFormattingRule.class.getDeclaredField("_sh");
_sh.setAccessible(true);
XSSFSheet sheet = (XSSFSheet)_sh.get(rule);
extList = sheet.getCTWorksheet().addNewExtLst();
ext = extList.addNewExt();
extXML =
"<x14:conditionalFormattings xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\">"
+ "<x14:conditionalFormatting xmlns:xm=\"http://schemas.microsoft.com/office/excel/2006/main\">"
+ "<x14:cfRule type=\"dataBar\" id=\"{00000000-000E-0000-0000-000001000000}\">"
+ "<x14:dataBar minLength=\"" + 0 + "\" maxLength=\"" + 100 + "\" gradient=\"" + false + "\">"
+ "<x14:cfvo type=\"num\"><xm:f>" + 0 + "</xm:f></x14:cfvo>"
+ "<x14:cfvo type=\"num\"><xm:f>" + 100 + "</xm:f></x14:cfvo>"
+ "</x14:dataBar>"
+ "</x14:cfRule>"
+ "<xm:sqref>" + region + "</xm:sqref>"
+ "</x14:conditionalFormatting>"
+ "</x14:conditionalFormattings>";
xlmObject = org.apache.xmlbeans.XmlObject.Factory.parse(extXML);
ext.set(xlmObject);
ext.setUri("{78C0D931-6437-407d-A8EE-F0AAD7539E65}");
}
sheetCF.addConditionalFormatting(regions, rule);
}
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("new sheet");
double[] list = new double[]{0d, 10d, 20d, 30d, 40d, 50d, 60d, 70d, 80d, 90d, 100d};
for (int i = 0; i < list.length; i++) {
sheet.createRow(i+1).createCell(0).setCellValue(0d);
sheet.getRow(i+1).createCell(1).setCellValue(list[i]);
sheet.getRow(i+1).createCell(2).setCellValue(100d);
}
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ExtendedColor color = workbook.getCreationHelper().createExtendedColor();
color.setARGBHex("FF80C279");
applyDataBars(sheetCF, "B2:B12", color);
sheet.setColumnWidth(1, 50*256);
FileOutputStream out = new FileOutputStream("ConditionalFormattingDataBars.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
This code will only work properly always for new created XSSFWorkbook. If the XSSFWorkbook was created from an existing workbook, the this could contain org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtensionList for x14 extensions already. If so, then these must be taken into account. But that would be a much more complex and challenging project.
I have a worksheet with data in it, I am trying to create a pivot table with report filter. I want to set default value to the report filter.
pivotTable.addReportFilter(13);
column contains 0's and 1's, I would like to set 0 as my default value in the report filter.
At first this question is not answerable in that general context as it is asked now. Creating pivot tables using apache poi is in beta state until now. So we need not only the high level apache poi API but also the underlying low level objects. And we need exactly to know which kind of data shall be in the pivot table. To be general able creating pivot tables from all kind of data, as Excel can do, there is much more effort necessary. Microsoft has programmed this in decades with big teams of programmers. From this apache poi is far away.
Until now apache poi adds as much pivot field items of type "default" (<item t="default"/>) as rows are present in the data range, if the pivot fields where used as axis fields.
This is because they don't want to have a look at the data, and so they are assuming as much different values as rows where in the data.
This is fine because Excel will rebuild its pivot cache while opening. But if we want preselect items, then this is not fine. Then we must know what items there are that can be preselected.
So we need at least as much items, as we want preselecting, as numbered items: <item x="0"/><item x="1"/><item x="2"/>...
And we need to build a cache definition which has shared elements for those items.
Example:
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.util.Random;
import java.io.*;
class PivotTableTest4 {
private static void setCellData(Sheet sheet) {
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Name");
cell = row.createCell(1);
cell.setCellValue("Value1");
cell = row.createCell(2);
cell.setCellValue("Value2");
cell = row.createCell(3);
cell.setCellValue("City");
for (int r = 1; r < 15; r++) {
row = sheet.createRow(r);
cell = row.createCell(0);
cell.setCellValue("Name " + ((r-1) % 4 + 1));
cell = row.createCell(1);
cell.setCellValue(r * new java.util.Random().nextDouble());
cell = row.createCell(2);
cell.setCellValue(r * new java.util.Random().nextDouble());
cell = row.createCell(3);
cell.setCellValue("City " + ((r-1) % 3 + 1));
}
}
public static void main(String[] args) {
try {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
//Create some data to build the pivot table on
setCellData(sheet);
XSSFPivotTable pivotTable = sheet.createPivotTable(
new AreaReference(new CellReference("A1"), new CellReference("D15")), new CellReference("H5"));
//Configure the pivot table
//Use first column as row label
pivotTable.addRowLabel(0);
//Sum up the second column
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
//Avarage the third column
pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);
//Add fourth column as page filter
pivotTable.addReportFilter(3);
/*
Apache poi adds 15 pivot field items of type "default" (<item t="default"/>) here.
This is because there are 15 rows (A1:D15) and, because they don't have a look at the data,
they are assuming max 15 different values. This is fine because Excel will rebuild its pivot cache while opening.
But if we want preselect items, then this is not fine. Then we must know what items there are that can be preselected.
So we need at least as much items as we want preselecting as numbered items: <item x="0"/><item x="1"/><item x="2"/>...
And we must build a cache definition which has shared elements for those items.
*/
for (int i = 0; i < 3; i++) {
//take the first 3 items as numbered items: <item x="0"/><item x="1"/><item x="2"/>
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(3).getItems().getItemArray(i).unsetT();
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(3).getItems().getItemArray(i).setX((long)i);
//build a cache definition which has shared elements for those items
//<sharedItems><s v="City 1"/><s v="City 2"/><s v="City 3"/></sharedItems>
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldArray(3).getSharedItems().addNewS().setV("City " + (i+1));
}
//Now we can predefinite a page filter. Second item, which is "City 2", in this case.
pivotTable.getCTPivotTableDefinition().getPageFields().getPageFieldArray(0).setItem(1);
FileOutputStream fileOut = new FileOutputStream("PivotTableTest4.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This needs the full jar of all of the schemas, ooxml-schemas-1.3.jar, as mentioned in the FAQ.
As it stands right now I have 241 lines of code used to created 46 rows and populate the first column of each row in excel using HSSFRow. Each row is named so I can reference it later on to add more columns to that particular row.
I am trying to create a loop that will create these row's for me and save me 200+ lines of code which is just plain boring to read over. Is there anyway to dynamically name and create rows using loops?
Here is a chunk of my code that works just fine but is repeated an insane amount. The only difference between the blocks(Besides the index which is easy to loop over and change) are the variable names that I put a ** by:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("Ions");
HSSFRow **name = worksheet.createRow((short) 0);
HSSFCell **cellA1 = **name.createCell((short) 0);
**cellA1.setCellValue("Name: ");
HSSFRow **lot = worksheet.createRow((short) 1);
HSSFCell **cellA2 = **lot.createCell((short) 0);
**cellA2.setCellValue("Lot #: ");
//..... This is done 46 more times!
I have created some arrays that hold the strings that I want to be the variable names but cannot figure out how to implement this if it is even possible. So far this is what I was thinking, it doesn't work but shows the idea I am trying to get to:
String[] nameString = {"name", "lot",...etc};
String[] cells = new String[50];//cellA1-A46
for(int i=0; i<46; i++){
cells[i] = "cellA"+(i+1);
System.out.print(cells[i] + ", ");
}
String[] text = {"Name: ", "Lot #: ",...etc};
for(int i = 0; i < 46; i++){
//creates row named from the index of nameString[].
HSSFRow nameString[i] = worksheet.createRow((short) i);
//creates cell named from index of cells[].
HSSFCell cells[i] = nameString[i].createCell((short) 0);
//sets that created cell equal to the index of text[]
cells[i].setCellValue(text[i]);
If anyone has any ideas please share!
Thanks! -pdatric