I have developed a program in which POI 3.11 is used. After upgrading to POI 3.16, the error ..depricated .. is shown for some instances (for example, HSSFCellStyle.BORDER_THIN).
Despite intensive search, I am unable to find the correct new syntax for the function ...setBorderLeft(HSSFCellStyle.BORDER_THIN).
Here is the section of the code:
try {FileInputStream file = new FileInputStream(new File(eP));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFFont cF = workbook.createFont();
HSSFCellStyle cS=workbook.createCellStyle();
cS.setBorderLeft(HSSFCellStyle.BORDER_THIN);
How can I migrate the code to POI 3.16?
Greetings from Aachen
According to the documentation of Apache Poi, since the version 3.15, the method setBorderLeft(....) consumes newly the instance of the BorderStyle enum.
I suggest you to try the following:
HSSFCellStyle cS=workbook.createCellStyle();
cS.setBorderLeft(BorderStyle.THIN);
Related
I'm trying to test a xml to excel converter in Java and I have the following exception
Exception in thread "main" java.lang.NoSuchFieldError: Factory
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:475)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:232)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:226)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:214)
at xmlToExcel.Converter.initXls(Converter.java:135)
at xmlToExcel.Converter.getAndReadXml(Converter.java:60)
at xmlToExcel.Converter.main(Converter.java:36)
The exception refers to the this line:
workbook = new XSSFWorkbook();
And here are my current jars
How can I solve this? Are there any conflicts in the jars?
EDIT 1: Here is the code for the method which initialises the POI workbook and writes the header row
workbook = new XSSFWorkbook();
CellStyle style = workbook.createCellStyle();
Font boldFont = workbook.createFont();
boldFont.setBold(true);
style.setFont(boldFont);
style.setAlignment(HorizontalAlignment.CENTER);
Sheet sheet = workbook.createSheet();
rowNum = 0;
Row row = sheet.createRow(rowNum++);
Cell cell = row.createCell(SUBSTANCE_NAME_COLUMN);
cell.setCellValue("Substance name");
cell.setCellStyle(style);
The exception references the first line, the creation of the workbook.
While using apache poi never mix jars from different versions. See faq-N10204.
So when used apache poi version 5.2.1 all apache poi jars needs to be from that version. So in your case apache poi 5.2.1 cannot be used together with poi-ooxml-schemas-4.1.2.jar. It needs to be used with either poi-ooxml-lite-5.2.1.jar or poi-ooxml-full-5.2.1.jar.
To avoid conflicts, furthermore all jars from not matching versions should be excluded from class path while running apache poi code.
So in your case using apache poi 5.2.1 there should not be any poi-ooxml-schemas-4.1.2.jar in class path additional to either poi-ooxml-lite-5.2.1.jar or poi-ooxml-full-5.2.1.jar. Also ooxml-schemas-1.4.jar should not be in class path while using apache poi 5. The ooxml-schemas-1.4.jar was the former version of poi-ooxml-full-5.*.jar and also cannot be used together with apache poi 5.
XSSFSheet sheet = workbook.getXSSFWorkbook().getSheetAt(0);
XSSFSheet pivot_sheet = workbook.getXSSFWorkbook().createSheet("Sheet1");
XSSFPivotTable pivotTable = pivot_sheet.createPivotTable(new AreaReference("B10:AJ24"), new CellReference("B2"),sheet);
I am getting following error at above line -
java.lang.NullPointerException at
org.apache.poi.xssf.usermodel.XSSFPivotCacheDefinition.createCacheFields(XSSFPivotCacheDefinition.java:145)
at
org.apache.poi.xssf.usermodel.XSSFSheet.createPivotTable(XSSFSheet.java:4065)
I didn't find solution for this using apache poi.
Workaround that I used is mentioned as follows :
Create SXSSFWorkbook as below
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream("xyz.xlsm")));
workbook.removeSheetAt(0);
xyz.xlsm is macro enabled excel which holds VBA code for generating pivot
Write in workbook as usual
Pivots will get generated in excel on opening it after download
I am using Apache POI HSSF/XSSF library to support Excel file reading functionality in my java code.
Now we are struggling with a thing that, we are not sure which version of Excel we will receive. It can be very old one or new one with macros and so on.
Workbook worbook = null;
if(FileUtils.getFileExt(file).equalsIgnoreCase("xls")) {
workbook = new HSSFWorkbook(new FileInputStream(file));
} else if (FileUtils.getFileExt(file).equalsIgnoreCase("xlsx"))
workbook = new XSSFWorkbook(new FileInputStream(file));
}
According to the Excel doc there is more formats like xlsm. xlt which we can receive.
Is there any other option to recognize which implementation should I use (HSSF vs XSSF)? Is there any possibility that xls will be not supported by HSSF?
I use poi-3.2-FINAL-20081019.jar . Error:
Type mismatch: cannot convert from HSSFWorkbook to Workbook
try {
if (strType.equals("xls")) {
wb = new HSSFWorkbook(inputStream);
} else {
wb = new XSSFWorkbook(inputStream);
}
Sheet sheet = wb.getSheetAt(0);
How to fix it?
As the date in your jar shows - poi-3.2-FINAL-20081019.jar - you're usigng a jar that's almost 10 years old! You need to upgrade to something more modern, and at the very least something from this decade...
Right now (November 2017), the latest version is Apache POI 3.17. You can find the latest version on the Apache POI homepage, and see all the fixes in the Changelog
In addition, you should swap to using WorkbookFactory rather than looking at file extensions to work out what class to use. That hides all the detection complexity for you, works around mis-named files etc
Your code can then become the very simple
Workbook wb = WorkbookFactory.create(new File("input.xlsx"));
Sheet s = wb.getSheetAt(0);
(Use a File if you can, rather than InputStream, for lower memory)
Use newer version:
https://mvnrepository.com/artifact/org.apache.poi/poi/3.5-FINAL
Read more at: http://poi.apache.org/spreadsheet/converting.html
i want to read a microsoft word document i.e. .docx format using POI but getting error:
The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents
if anyone can help me to get rid of this??
You should have a read through the Converting from HSSF to the Common SS Usermodel page to help you understand what you need to change.
As a general guide though, if your code was previously
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("foo.xls"));
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.getRow(0);
System.out.println("Cell A1 is " + r.getCell(0));
It should instead become
Workbook wb = WorkbookFactory.create(new File("foo.xls")); // Or foo.xlsx
Sheet s = wb.getSheetAt(0);
Row r = s.getRow(0);
System.out.println("Cell A1 is " + r.getCell(0));
Please check http://poi.apache.org/spreadsheet/converting.html about new models XSSF and HSSF.
Not sure which API from POI you are using, but I presume that you are using the HSSF API. Instead you should use the the XSSF API (see http://poi.apache.org/spreadsheet/index.html for details). For example instead of using Workbook wb = new HSSFWorkbook(); use Workbook wb = new XSSFWorkbook();
HSSF workbook will not work for higher version of excel file instead of HSSFWorkbook use XXSFWOrkbook.You can have the complete code in the below link
http://aravind-soa.blogspot.com/2017/02/how-to-import-excel-file-into-oracle.html