Eclipse shows error for MissingCellPolicy - java

I am using Apache POI to read data from Excel files. I have some blank/null cells in my Excel which I want to handle using MissingCellPolicy.RETURN_NULL_AND_BLANK. However, Eclipse underlines RETURN_NULL_AND_BLANK when I hover the mouse over it and I get a pop-up saying RETURN_NULL_AND_BLANK cannot be resolved or is not a field
I have imported the Row.MissingCellPolicy class. Can anyone point out what needs to be done?

MissingCellPolicy is the static nested class of the Row interface. And RETURN_NULL_AND_BLANK is the static field of the ROW interface.
So you should access with:
Row.RETURN_NULL_AND_BLANK;
Or use static import instead:
import static org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import static org.apache.poi.ss.usermodel.Row.RETURN_NULL_AND_BLANK;
//....
MissingCellPolicy policy = RETURN_NULL_AND_BLANK;

Adding Row.RETURN_NULL_AND_BLANK; helped me instead of adding Row.MissingCellPolicy.RETURN_NULL_AND_BLANK; or MissingCellPolicy.RETURN_NULL_AND_BLANK; and Eclipse doesn't throw error.
This is because my worksheet is XSSFSheet which returns an XSSFRow just Row and XSSFRow (or even HSSFRow for that matter) directly inherits these constants from the Row class. See more info here

Row is interface
MissingCellPolicy is the enum in Row
and
1. RETURN_NULL_AND_BLANK
2. RETURN_BLANK_AS_NULL
3. CREATE_NULL_AS_BLANK
is the static field
To Access any of 1 to 3
use
Row.MissingCellPolicy.RETURN_NULL_AND_BLANK
or use other two as per requirement

Related

How to create hyperlink using Apache POI and Java?

I wanted to create a hyperlink using Apache POI and Java for Selenium Webdriver automation testing. But the thing is, when i'm creating a hyperlink using this code :
File file=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String screenshotLocation = "D:\\MyFolder\\Test Results\\Screenshots\\";
screenshotURL=screenshotLocation+datetimestamp+".png";
FileUtils.copyFile(file, new File(screenshotURL));
#SuppressWarnings("deprecation")
HSSFHyperlink link = (HSSFHyperlink)createHelper.createHyperlink(Hyperlink.LINK_FILE);
link.setAddress(screenshotURL);
hyperlinkList.add(link); // add all hyperlinks to an arraylist
createHyperlink and LINK_FILE are showing as deprecated. Is there any alternate way to create hyperlink without deprecation? ( I've tried with XSSF also, but getting the same deprecation.)
FYI: when i'm using selenium-server-standalone-2.47.1.jar and poi-bin-3.17-beta1-20170701 , it is showing only the deprecation, not any error. But when i'm using updated version, for example Selenium-java-2.48.2.jar and poi-bin-3.17-20170915, along with the deprecation, one error message is showing: LINK_FILE cannot be resolved or is not a field.
Please Help. Thanks in advance.
Hyperlink.LINK_FILE is replaced by HyperlinkType.FILE
and the createHyperlink function was replaced by a new Funtion with the Parameter if type HyperlinkType
From the javadoc:
Hyperlink createHyperlink(int type)
Deprecated. POI 3.15 beta 3. Use createHyperlink(HyperlinkType)
instead.
And
static int LINK_FILE
Deprecated. POI 3.15 beta 3. Use HyperlinkType.FILE instead.

Copy rows with JOpenDocument

I am trying to copy a row several times using JOpenDocument v1.3 and I am failing miserably.
Here is my code snippet:
private void copyRow(Sheet sheet, int templateRow, int target){
sheet.ensureRowCount(target);
sheet.duplicateRows(templateRow, 1, 1);
sheet.insertDuplicatedRows(templateRow, target);
}
No row gets changed with the code above and no error is displayed.
I know I could copy each cell separately but I think if there is a way to do it all at once it should be more efficient.
Thanks in advance.

XLLoop (Excel 2010) - Integrating a java function that returns an array/list of values

I've started looking at XLLoop as I need a way of getting data from a java system into Excel (2010 version).
I've followed the java SimpleServer example on the front page of the website:
package org.boris.xlloop.util;
import org.boris.xlloop.FunctionServer;
import org.boris.xlloop.handler.*;
import org.boris.xlloop.reflect.*;
public class ServerExample
{
public static void main(String[] args) throws Exception {
// Create function server on the default port
FunctionServer fs = new FunctionServer();
// Create a reflection function handler and add the Math methods
ReflectFunctionHandler rfh = new ReflectFunctionHandler();
rfh.addMethods("Math.", Math.class);
rfh.addMethods("Math.", Maths.class);
rfh.addMethods("CSV.", CSV.class);
rfh.addMethods("Reflect.", Reflect.class);
// Create a function information handler to register our functions
FunctionInformationHandler firh = new FunctionInformationHandler();
firh.add(rfh.getFunctions());
// Set the handlers
CompositeFunctionHandler cfh = new CompositeFunctionHandler();
cfh.add(rfh);
cfh.add(firh);
fs.setFunctionHandler(new DebugFunctionHandler(cfh));
// Run the engine
System.out.println("Listening on port " + fs.getPort() + "...");
fs.run();
}
}
and have successfully been able to integrate simple static java methods into Excel that return one value. E.g.Math.random(), Math.pow(2, 4.45) which both return a single double.
However, I have been unable to integrate any functions that return an array/list of values. For example, in the above we have registered the Maths class (an XLLoop class) for use in Excel. This class contains a static method
public static double[] normalDist(int var0)
The method returns an array of length var0, however when I call this in Excel only one cell is populated with a value, I would have expected var0 cells to have been populated. This seems like a fairly fundamental feature to me, so I'm pretty sure I must be doing something daft.
If there is anyone out there who could help with this it would be greatly appreciated.
I should also mention that I found a similar SO question , linked from a R related SO questions feed. Unfortunately the original SO question has been removed from SO by the poster...
You need to enter the function calling the method that returns an array as a matrix-function in excel: select the cells that shall recieve the returned data, enter the function and hit ctrl-shift-enter.
If the range selected is too small, then only the values fitting in the range are displayed.

Is there a way to set the width of a column in XWPFTableCell(POI)?

The full path of the class is org.apache.poi.xwpf.usermodel.XWPFTableCell.
The API goes here: http://poi.apache.org/apidocs/
Thanks for your advices.
No. You cannot set the width of column in XWPFTableCell.
But you can always use HSSFSheet.autoSizeColumn(int column) after finished entering data in sheet.
You can do it; to find this I had to study its internal XML schema.
This is a major and very tricky element; I spent several days solving this problem and finally I had managed to solve it using this method of setting the widths of a table cell:
private static void setTableColumnWidths(XWPFTable table) {
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2000));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3200));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1000));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1000));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1105));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1105));
}

Apache POI: changing CellType causes IllegalStateException

Why is it illegal to change cell type in Apache POI? The code bellow causes IllegalStateException: Cannot get a error value from a numeric cell.
Cell mycell = myrow.createCell(0);
// Make it numeric by default.
mycell.setCellType(Cell.CELL_TYPE_NUMERIC);
if (someCondition) {
mycell.setCellType(Cell.CELL_TYPE_STRING); // IllegalStateException
}
Is there perhaps a way around this problem (as in without introducing additional logic)?
Im not sure this is avoiding your "without additional logic" but here goes:
Cell mycell = myrow.createCell(0);
// Make it numeric by default.
int cellType = Cell.CELL_TYPE_NUMERIC;
if (someCondition) {
cellType = Cell.CELL_TYPE_STRING;
}
mycell.setCellType(cellType);
The Javadoc does not state why it would throw an IllegalStateException so I would just avoid it (by doing the above) or go digging in the sourcecode.
Fixed it. After replacing the streaming version (org.apache.poi.xssf.streaming.SXSSFWorkbook) with the non-streaming one (org.apache.poi.xssf.usermodel.XSSFWorkbook) the exception was gone. I'm okay with XSSFWorkbook since I'm filling XLSX with only a couple of lines.

Categories