I have an existing workbook that acts as template. I tried to update a cell value in an existing row, meaning the adjacent cell has a value. The problem is that after the file is created and I open it, I get the following error:
"Excel found unreadable content in...."
I assume the reason is SXSSFWorkbook only handles writing and if a row exists it cant update the contents of the row, which would entail reading and then writing - is this correct or am I experiencing a bug?
Thanks
Another issue is that if you're using addMergedRegion, the CellRangeAddress values for rows and columns have to be in ascending numerical order:
// Note that rows and columns for CellRangeAddress constructor have to be in ascending order
// The commented out line below will generate an error when opening the sheet:
// sheet.addMergedRegion(new CellRangeAddress(lastRow, lastRow-(rowsTobeCreated-1), 0, 0));
// In order this call works:
sheet.addMergedRegion(new CellRangeAddress(lastRow-(rowsTobeCreated-1), lastRow, 0, 0));
In case anyone else has this problem, check to make sure the length of your SheetName is not too long. I was getting this error when setting the workbook sheet name longer than around 30 characters.
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, "Supporting Documentation"); // make sure this is not too long
Related
I'm having a problem evaluating excel sheet cell which uses named cell in its formula. I'm using apache-poi version 3.17, java 1.8 and the file I'm working with is .xlsm. Here is an example code:
//Getting the excel file from the database
Document doc = documentRepository.findByDocumentType(DocumentType.EXAMPLE);
InputStream fis = new ByteArrayInputStream(doc.getFileData());
Workbook wb = new XSSFWorkbook(fis);
Sheet mappingSheet = workbook.getSheet("Quote services");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// This cell contains a formula which uses named cell
CellReference cellReference = new CellReference("A109");
Row row = mappingSheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
CellValue cellValue = evaluator.evaluate(cell);
This gives me the following error:
org.apache.poi.ss.formula.FormulaParseException: Specified named range
'Discount' does not exist in the current workbook.
The excel formula looks something like that:
=Discount*A7
Where Discount is A5 and if I change the formula to look like this:
=A5*A7
it works fine.
This is kind of strange cause if I try to get the named field Discount it finds it, but if I use evaluate on a cell which uses the field it doesn't.
The document is predefined so I can't change anything in it. If I name the given discount cell with poi and with the same name there is no problem, but I don't want to do this cause there are more named fields in the document and if something change I have to change the code. Any help will be appreciated!
I found the problem! It was that the Discount field was referenced from a hidden sheet in the document and it was referenced like this: Discount instead of 'Quote services'!Discount.
Are you sure that Discount is indeed within the Workbook that you are evaluating? The message is pretty clear.
Can you show exactly how the formula looks like - is it something like this:
=SUM('C:\Data\[Finances.xlsx]Discount'!D10:D25)
Try setIgnoreMissingWorkbooks(true), and if it works, then the Discount is set in another workbook. setIgnoreMissingWorkbooks.
I am getting an exception when inserting more than 1.2 million records. Successfully inserted 1 047 000 records in 1 sheet and when creating the new sheet to insert the rest of the records, I am getting
java.lang.IllegalArgumentException: The workbook already contains a
sheet of this name
I am using this condition for creating the new sheet
if(rowCount>1047000){
wsSheet = createSheet(sheetName, xssfWorkbook);
createHeader(wsSheet, columsnList, xssfWorkbook, null,listResult.isNameFieldRequired());
rowCount = 1;
}
Tried of changing the sheetName in
wsSheet = createSheet(sheetName, xssfWorkbook) as sheetName+"1"
but still the same exception.
There is some oddity in the way the org.apache.poi.ss.usermodel.Sheet checks if a string is already used as a sheet's name: the candidate name is truncated using name.substring(0, 31) and compared to existing names truncated the same way, so you have to make strings differ in at least a character in this range.
XSSFSheet newSheetName = workbook.createSheet("sheetName"+1);
That should simply work. Then depending on your code, you may need to increase the newSheetName variable scope.
Instead of inserting 1 you can insert datetime as its trying to add all the record at once so taking the same sheet name.
use the below code for datetime and call it in your code in place of 1
please refer this code for datetime;
============================================================
String dateTime=new Date().toString().replaceAll(":", "_");
wsSheet = createSheet(sheetName, xssfWorkbook) as sheetName+dateTime;
=====================================================================
It should work.
if not work the use datetime with miliseconds
Hope it help
I am trying to retrieve a value from a cell in excel but it is merged. I am using Java
This is what i have:
ByteArrayInputStream bxf = new ByteArrayInputStream(entity.getAttachment().getFile());
Workbook wb = WorkbookFactory.create(bxf);
Sheet sheet = wb.getSheetAt(0);
Row cpRow = sheet.getRow(4);
Here is the example of the excel:
Red Block is a merged cell :
edit
I am using apache.poi library
Reason why you getting a null pointer are that the value you are looking at currently is the actual row 5 on sheet.
As stated by #Berger in the comment the 'getRow is 0 based'
Use getRow(3) instead.
Please see https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFRow.html
aswell as
it states https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html
HSSFRow getRow(int rowIndex)
Returns the logical row (not physical) 0-based.
I have a requirement Where I need to add data validation to an entire column rather than a specific cell. I went through the documentation of Apache poi and found the example below
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Data Validation");
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(
new String[]{"10", "20", "30"});
DataValidation dataValidation = new HSSFDataValidation
(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
sheet.addValidationData(dataValidation);
But the above example creates a drop down datavalidation for a specific cell. In this case row 0, column 0. The rest of the cells in a column doesn't have validation. But in actual excel file we can do it so it should be possible. I tried and searched a lot but could not come to a solution. Please help..
Another way to get the validation on the whole column you can also use -1 for both row parameters like:
CellRangeAddressList columnRange = new CellRangeAddressList(-1, -1, columnIndex, columnIndex);
Tested this in POI 3.16
Chetan all the four parameters of the constructor CellRangeAddressList as below
CellRangeAddressList(index_of_starting_row, index_of_ending_row, index_of_starting_column,index_of_ending_column);
so eg. if there are 10 rows and 5 columns, and if you want to add drop down list in 2nd column throughout the all rows means in entire 2nd column then use below code for cell address
CellRangeAddressList addressList = new CellRangeAddressList(0,9,1,1);
based on your code this will add drop down with the values 10,20,30 in entire 2nd column if you add the above line of code by replacing your code.
hope this will clear the concept and you would get the desire result.
I have an Excel sheet in which 2 columns have a dropdown with one about 4-5
values and the other with 2 values that can be selected. I would like to read
the value that is there for each row in these two columns. . What is the code
sample to do that? I did browse a while on the net and this forum, but did not
find any answers. I posted in the JExcel Yahoo groups , but no success.
I have added the following code, but this does not help. This code sample just prevents the "common Assertion failed" error
WorkbookSettings settings = new WorkbookSettings();
settings.setSuppressWarnings(true);
Workbook workbook = Workbook.getWorkbook(sis, settings);
The sheet gives an erroneous row count when the dropdown is there. Has anyone been able to read the values selected in the drop down ?
If you have applied an autofilter to your Excel sheet then probably that is responsible for the issue. I would suggest you to please remove the autofilter and then try fetching the value of the combobox.
Cell yourCell = yourSheet.getCell(x,y);
String comboboxValue = yourCell.getContents();
logger.log("value selected in combobx is : " + comboboxValue );