Upon opening the xls sheet, I see a value of "13.09". If I click on that cell, I can see a value of "13.087100872". I need to extract the unrounded value for all cells without manually opening the excel file.
I have tried
cell.getNumericCellValue()
as well as
objectDefaultFormat.formatCellValue(cell, objFormulaEvaluator)
where objDefaultFormat is a DataFormatter and objFormulaEvaluator is a FormulaEvaluator:
DataFormatter objDefaultFormat = new DataFormatter();
FormulaEvaluator objFormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
What do I need to do differently to get the unrounded value?
Related
I am trying to copy an excel workbook to another excel workbook(some cell values needs some calculations as per)
I need to copy the CellStyles as well, as there are Data and Amount fields with styling.
Im using the below lines of code :
CellStyle cellStyle = wb_out.createCellStyle();
cellStyle.cloneStyleFrom(cell_in.getCellStyle());
Cell cell_out = row_out.createCell(cellIndex);
cell_out.setCellValue(value);
cell_out.setCellStyle(cellStyle);
For large files,
The cloneStyleFrom throws ArrayIndexOutofBound error,
How do we fix this.
Thanks in advance.
Basically there's a limit to number of Styles that one can create,
and if the excel sheet is a large file, the limit crosses and the error 'ArrayIndexOutofBound' is thrown.This is called Style explosion.
To escape this scenario, you have to create separate cell styles and use it wherever is required
This is what I tried and it worked.
I created a cell style for the Date format and Currency format, and applied those wherever required.
CellStyle cellStyleDate = wb_out.createCellStyle();
short dateFormat = wb_out.createDataFormat().getFormat("d-mmm");
cellStyleDate.setDataFormat(dateFormat);
CellStyle cellStyleCurrency = wb_out.createCellStyle();
short currencyFormat = wb_out.createDataFormat().getFormat("#,##0.0");
cellStyleCurrency.setDataFormat(currencyFormat);
Likewise one can create a cell style and add different specific format or properties to it and use it in the workbook,
like this :
cell_out.setCellStyle(cellStyleDate);
I am using Apache POI to generate xlsx sheet for reports. One of the POI generated report I saved as another using Microsoft excel . When comparing the original file and the saved file there was 12Mb difference. The original file was 15Mb while the saved file is just 2.5Mb. The Workbook used is XSSFWorkbook.
Is it possible to reduce the file size created by Apache POI
Here is the code snippet I have used:
XSSFWorkbook workbookTitle = new XSSFWorkbook(fileInputStream);
workbook = new SXSSFWorkbook(workbookTitle, maxRows);
font = workbook.createFont();
font.setFontHeightInPoints((short) 9);
font.setFontName(FONT_NAME);
cellTwoDecimal = workbook.createCellStyle();
DataFormat format = workbook.createDataFormat();
cellTwoDecimal.setDataFormat(format.getFormat("0.00"));
cellTwoDecimal.setFont(font);
cellCommon = workbook.createCellStyle();
cellCommon.setFont(font);
cellText = workbook.createCellStyle();
cellText.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text"));
cellText.setFont(font);
cellWrpText = workbook.createCellStyle();
cellWrpText.setWrapText(true);
cellWrpText.setFont(font);
Row row;
Cell cell;
for (int i = 0; i < size; i++) {
row = excelSheet.createRow(rowIndex++);
cell = row.createCell(i);
cell.setCellValue(rowHeader);
cell.setCellStyle(cellCommon);
}
I have removed some internal logics from code. Please share your ideas.
[Edit 1]
I am inserting a lot of blank cells where there is no value, ie. some part of the report will not have any value. So I put a blank cell there. I am also setting style for the blank cell. Can this be the reason?
Thanks in advance.
According to your "edit 1"... if i understand you correctly you create cells with no value.
you do not have to do so. if you dont want to write something then do not create the empty cell.
on my poi-experience you only have to create rows and cells if you want to write something.
from this point of view it is clear, that your xlsx is very large (many many cell-objects). i think MS Excel removes the empty cells on manual save.
added: Need to mention that there is also an issue with styling your cells. please try to use as few as possible instances of CellStyle. if you have cells with same style do not create a new instance of CellStyle with same attributes. please apply the same instance of CellStyle. Also do not assign style to simple text cells. in this case excel uses a default style (background='white', textcolor='black', font='any default', size='any default', format='default').
I had a similar problem, and later figured out that I was opening the FileOutputStream in append mode(append=true). The file size grew exponentially(say from 7KB to 54KB) every-time I update a single cell on the sheet.
When removed the append, it worked just fine.
I have an excel sheet which has a formula reference to another sheet of the same workbook.
Currently, I'm on Sheet 1 and trying to set a formula for a cell using cell.setFormula(Sheet2[#All]).
While I'm doing that, I'm encountering the following error :
Specified named range 'Sheet2' does not exist in the current workbook. org.apache.poi.ss.formula.FormulaParseException
at org.apache.poi.ss.formula.FormulaParser.parseNonRa nge(FormulaParser.java:569)
at org.apache.poi.ss.formula.FormulaParser.parseRange able(FormulaParser.java:429)
at org.apache.poi.ss.formula.FormulaParser.parseRange Expression(FormulaParser.java:268)
at org.apache.poi.ss.formula.FormulaParser.parseSimpl eFactor(FormulaParser.java:1119)
at org.apache.poi.ss.formula.FormulaParser.percentFac tor(FormulaParser.java:1079)
at org.apache.poi.ss.formula.FormulaParser.powerFacto r(FormulaParser.java:1066)
at org.apache.poi.ss.formula.FormulaParser.Term(Formu laParser.java:1426)
at org.apache.poi.ss.formula.FormulaParser.additiveEx pression(FormulaParser.java:1526)
at org.apache.poi.ss.formula.FormulaParser.concatExpr ession(FormulaParser.java:1510)
at org.apache.poi.ss.formula.FormulaParser.comparison Expression(FormulaParser.java:1467)
at org.apache.poi.ss.formula.FormulaParser.Arguments( FormulaParser.java:1051)
at org.apache.poi.ss.formula.FormulaParser.function(F ormulaParser.java:936)
However, in the workbook, I have created the necessary sheet. The sheet name is Sheet2. But still the code is not able to refer to that sheet. Is there a way to fix this issue or any workaround?
Sheet2 is not the same as Sheet 2. Rename Sheet 2 to Sheet2.
I am using Apache POI to generate Exccel Templete which my clients could download, add values and upload back.
I would like to set the cell values non editable so that the template headers could not be edited.
I tried this code but it does not work,
cell.getCellStyle().setLocked(true)
I also read that locking the excel sheet and then allowing the columns to setlocked(false) would work but I am not sure how many columns will be filled by client, so I want is all other columns t be edited except the one which I filled dynamically with Apache POI.
I hope my query is clear to understand.
Try the following code, it may solve your problem:
HSSFWorkbook workbook = new XSSFWorkbook();
// Cell styles. Note the setLocked(true) method call.
HSSFCellStyle lockedNumericStyle = workbook.createCellStyle();
lockedNumericStyle.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
lockedNumericStyle.setLocked(true);
HSSFSheet sheet = workbook.createSheet("Protection Test");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue(100);
cell.setCellStyle(lockedNumericStyle);
// This line should cause all locked cells to be protected,
// the user should not be able to change the cells
// contents.
sheet.protectSheet("password");
The password makes it possible to remove the protection from the sheet and makes it possible then for the locked cells to be modified.
I don't recall how well this works -- for example, I think that the client can unprotect the sheet using the menu -- but you do need to protect the sheet via something like Sheet.protectSheet("") (no password, but nevertheless a protected sheet.)
i have an excel file whose cells have different data formats from simple currency formats $#,##0.00 ($1,200.00) to more complex ones.
the problem is that when i write to these cells, the formats are not followed. so if i enter a value to a currency-formatted cell, the value shows up without the formatting:
ex:
i entered 1234. what i expect is $1,234.00 (as defined in the excel cell). what i get is still 1234
during my attempts i even tried to apply the cell's own format to itself
this is the code im using:
ServletContext context = getServletContext();
InputStream inp = context.getResourceAsStream("/WEB-INF/MyExcel.xlsx");
Workbook wbTemp = new XSSFWorkbook(inp);
Sheet wbTempSheet = wbTemp.getSheetAt(0);
Cell targetCell = wbTempSheet.getRow(0).getCell(0);
targetCell.setCellValue(request.getParameter("myNumber"));
//i even tried reapplying the cell's own format to itself
CellStyle cs = targetCell.getCellStyle();
String df = cs.getDataFormatString();
System.out.println(df);
cs.setDataFormat(wbTemp.createDataFormat().getFormat(df));
targetCell.setCellStyle(cs);
upon checking the excel file i noticed that the formats were indeed implemented. however i needed to press tab per cell to see it take effect. is there an alternative or at least a way to simulate the tab action so that the cells are formatted upon opening?
found what's wrong. its this line:
targetCell.setCellValue(request.getParameter("myNumber"));
it should be:
targetCell.setCellValue(Double.parseDouble (request.getParameter("myNumber") ));
forgot that numeric excel cells to be properly formatted as must be passed doubles.