Apache Poi update xls file - java

I am using Apache Poi in an Android application, but I have a problem with the update of xls file.
To change the value of a cell where there is already a value I have no problem, but I have a null pointer exeption when I change the value of empty cells.
I use the method setCellValue.

You may need to create the cell before you change the value of it. If a cell has no value it 'doesn't exist' so to speak and so you need to create it and then set it's value. You could try using the getCell() with mising rowPolicy to try and obtain a cell if their isn't currently one, like so:
myRow.getCell(7, Row.CREATE_NULL_AS_BLANK);//Should create cell if it is currently blank
Once you have the cell, try setting it's value as you have been doing.
Alternatively, try checking beforehand if you have a cell, e.g
if (myCell ==null) {
//Create cell code
Cell cell = row.createCell(0);
}
Good luck!

Related

How to lock data in excel sheet using POI, leaving cells without any data / the rest of the sheet unlocked

I am trying to lock the data within an Excel Worksheet so that it is not possible to edit the data already written to the Worksheet, but leaving the rest of the empty spaces free for editing/ adding more data.
I have tried going through the whole sheet trying to set the cell style, using the code provided below but it is only the relevant code, but it doesn't work, which agrees to this question already asked
Lock single column in Excel using Apache POI
XSSFCellStyle lockCell = getFileWriter().getWorkbook().createCellStyle();
lockCell.setLocked(true);
for(Row row : sheet){
for(Cell mycell : row){
mycell.setCellStyle(lockCell);
}
}
the opposite: by locking the whole sheet and setting the relevant rows' cell style to unlock, I have tried that but the cells without any data don't unlock and so it hasn't worked for me. In any case how far and wide should one unlock the cells as it is not known how much space is needed for the unknown amount of data to be added.
Any help or suggestions would be greatly appreciated.
The iterator for a row will return no cells if the cells have not been explicitly created before. To unlock a cell it must be set explicitly to an unlocked style. Changing the default is not possible to my knowledge.
So the only way i see is to create cells for every row up to a certain column index.
To unlock cells with no data, suposing that your sheet is protected, you must set the default style of the columns you want to be unlocked, using the setDefaultColumnStyle method.
In your case you must do something like:
CellStyle editableStyle = workbook.createCellStyle();
editableStyle.setLocked(false);
for (int i = 0; i < numColumns; i++) {
sheet.setDefaultColumnStyle(i, editableStyle);
}

How to get cell number in excel by providing cell content using java POI 3.8?

I need to get the cell index of particular string in excel using java code.
e.g if the cell(23,1) contains the string 'Student_address', the method have to get the return as 'cell index'
System.out.println(sheet.getRow(1).getCell(23) will print the value of student_id in corresponding row-1.
Here sheet is the excel worksheet.
I just want to get the 23 dynamically by giving cellContent as 'Student_id'.I need the colomn number in excel exactly.
Please help me out to get cell index by considering java poi 3.8 version.
Thanks in advance.

Generating a new Excel while retaining old formula from template Excel file

I have an Excel template with first 5 columns left blank (to be populated from an XML). The sixth column has a drop-down list such that depending on whatever value is selected from the list, a value appears in 7th column. This is done by using INDEX-MATCH formula for the 7th column.
=INDEX(Sheet7!$B$1:$B$312,MATCH(F3,Sheet7!$A$1:$A$312,0))
The task is to take this template Excel, and using POI, populate the first 5 columns and generate a new Excel workbook. (Not changing in the existing sheet).
The problem is when I generate the new Excel workbook, the formula isn't copied over from the template. It is really important to retain the formula for the task I am working on. I read about
formulaEvaluator()
but I don't think it'll work in that case because I need the formula to be retained as it is. I cannot evaluate it before copying as there won't be any data in column 6 at that point to evaluate the formula and store the value.
I'd really appreciate if someone who has any experience in this area could guide me a bit.
If you have access to the Cell, simply call:
String formula = cell.getCellFormula();
Note though that this will only work if the cell is formula cell. IE: cell.getCellType() == Cell.CELL_TYPE_FORMULA. If the cell is not a formula cell, you're going to end up with an IllegalStateException as described in the API

Open excelfile with an preselected cell

i have an application where i read an excelfile(.xls), make some calculations and present the result. At the result is a button implemented which opens the original excelfile.
Is it possible to provide the specific cell as an arguement to show the cell directly, so that the user does not have to select the cell by himself?
The selected cell is stored within the Excel file. You have to study the API of your Excel writing library to find out, how to set this.
E.g. with POI it would be http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#setActiveCell(java.lang.String)
Like Holger and Moritz said in their comments, it seems that that it is not possible to startup excel with a given cell preselected.

Inserting image in to the excel cell using jxl api

I have to insert image in to the excel cell using jxl api
I have tried with some sample code but it will insert at some coordinator, but I want in to at particular cell index or inside cell.
Here is sample code that I found while searching for this.
Anyone has solution for this?
From a discussion on channel9:
No version of Excel allows you to insert a picture into a cell. Pictures are inserted into the worksheet and will always float.
One of the properties of a picture can be set to "move and size with cells" but that only moves or stretches the picture when the underlying rows and columns are inserted, deleted or sized. It does not confine a picture to a cell.
Here is a step-by-step procedure on how to do it from Excel, maybe you can mime it programmatically.

Categories

Resources