Java Apache Poi SXSSF with Excel Macros - java

I have a excel template that has macros (.xlsm), I want to read it in, add a million rows to it and write it out.
I know that reading and re-writing files with POI that contain macros will preserve the macros. I need to write out the excel using SXSSF (ram limitations), but SXSSF doesn't read files.
Question: How can I read in an excel with macros using XSSF, and then write out the excel with macros using SXSSF?

Apache POI supports writing a spreadsheet with a large number of rows via SXSSFWorkbook based on a "template workbook". See the relevant constructor for details.
So you would open the .xlsm via XSSFWorkbook and then create the SXSSFWorkbook with that as template.
This should also keep the macros in place as far as I see.

Related

What is the difference between poi and poi-ooxml

I'm learning about data driven testing using Selenium and Excel. I'm taking an online course that has asked used to add the Apache poi and poi-ooxml dependencies in Maven.
I'm struggling to understand what the differences between the two are. Are both required in order to retrieve data in Excel and pass these to our tests?
Thanks
Excel files has long history
Excel 97-2003 workbook:
This is a legacy Excel file that follows a binary file format. The file extension of the format is .xls.
Excel 97-2003 in terms of apache poi is called - Horrible Spreadsheet Format As the Excel file format is complex and contains a number of tricky characteristics,
apache-poi jar has code to handle these file
Excel 2007+ workbook:
This is the default XML-based file format for Excel 2007 and later versions. It follows the Office Open XML (OOXML) format, which is a zipped, XML-based file format developed by Microsoft for representing office documents. The file extension of the format is .xlsx. ( DOCX,PPTX are other OOXML based examples).
Excel 2007+ workbook in terms of apache poi is called - XML Spreadsheet Format -these file format are advanced version of HSSF and has additional features, code to handle these files are written in apache-poi-ooxml jar
More reading
As .xls is almost dead but still some applications use it, so for backward compatibility both dependencies are required.
here is what Apache have to say -
HSSF Excel XLS poi For HSSF only, if common SS is needed see below
Common SS Excel XLS and XLSX poi-ooxml WorkbookFactory and friends
all require poi-ooxml, not just core poi
you can read more at their official website http://poi.apache.org/components/index.html#components

Is it necessary to add POI for reading and writing Excel Sheets in JAVA?

Is it necessary to add Apache POI and similar libraries to read and write Excel Files in JAVA? Is there any predefined JAVA library for reading and writing Excel Sheets?

Apache POI - Invalid part to process data

I access an Excel spreadsheet using Java Apache POI (hssf). I got the following error :
java.lang.RuntimeException: org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:143)
at org.testng.internal.Parameters.handleParameters(Parameters.java:426)
What am I missing ?
To open an xlsx (Office Open XML) file, you should use XSSFWorkbook instead of HSSFWorkbook, which is used for xls (Excel 97-2003) files.
If you are using POI < 3.5, you need to upgrade to at least version 3.5, in order to be able to read xlsx files. Here's a guide for doing the conversion, but essentially, you'd need to load the file using WorkbookFactory which takes care of the creation of either an XSSFWorkbook or HSSFWorkbook for you:
Workbook workbook = WorkbookFactory.create(new File("file.xlsx"))
Make sure the excel sheet is not corrupt, by opening it. If you seen any error, save the file as MS Excel 97-2003 Worksheet.
Also make sure you have specified the filename as "**.xls"

Append rows to a Spreadsheet without loading whole file

I have been using the apache POI library to create XLS files but find it limited both in the fact that is follows the Microsoft Excel spec and also in the way that it requires you to have your entire Workbook loaded in memory if you want to append to it.
Are there any other libraries or techniques (save hand coding the XLS XML itself) that allow you to stream the contents of an XLS document, row by row, with out needing more then the full workbook loaded in memory?

Is there any API that takes an Excel file and converts it to a CSV file using Java?

I have 200,000 or more records in an Excel file. If I tried to read records row by row and create a CSV file it's very costly as well as time consuming. Is there any API that can help me with that?
There are several frameworks out there, if you have a look.
For example, you can try:
Apache POI (example)
JExcel (example)
This seems to be the same question as How to import an Excel file into MySQL using ODBC in Java?
What has changed? If you want to write to a CSV file you should use a CSV library such as OpenCSV: http://opencsv.sourceforge.net/

Categories

Resources