I have a huge excel file with tons of columns which looks like this :-
Column1 Column2 Column3 Column4 Column5
abc def ghi
mno pqr
......
The output generated by my code when I print all the values in excel is :-
abc;def;ghi;null;null
mno;pqr;null;null;null
So, If we look at the output above we can note that the cells where I left blank values were not picked up by the POI library. Is there a way in which I can get these values as null? Or a way to recognize that the values presented skipped blank cells?
Please note: I am not using the usermodel (org.apache.poi.ss.usermodel) but an Event API to process xls and xlsx files.
I am implementing HSSFListener and overriding its processRecord(Record record) method for xls files. For xlsx files I am using javax.xml.parsers.SAXParser and org.xml.sax.XMLReader.
I am using JDK7 with Apache POI 3.7. Can someone please help?
I have already seen this possible duplicate How to get an Excel Blank Cell Value in Apache POI? But this doesn't answer my question as I am using Event API.
Yes, it can be done, and there are several examples of it which ship with Apache POI. They all relate to Event based xls / xlsx -> CSV, which looks very close to what you're doing. That makes me worry you may be re-inventing the wheel...
For HSSF event model processing, the example you want to look at is XLS2CSVmra. That is powered by MissingRecordAwareHSSFListener
For XSSF event model, the example you need is XLSX2CSV
Related
I am filling cells of an Excel file using Apache POI, and there are a lot of formula cells in the document. However, their values are not refreshed when I open the document in Excel.
It's my understanding that I need to use a FormulaEvaluator to refresh formula cells. Is there a way, though, to update all formula cells at once? There are a lot of them, and while making an exhaustive list is not out of question, it's certainly not something I'm very willing to do.
Sure. Refreshing all the formulas in a workbook is possibly the more typical use case anyway.
If you're using HSSF, call evaluatorAllFormulaCells:
HSSFFormulaEvaluator.evaluateAllFormulaCells(hssfWorkbook)
If you're using XSSF, call evaluatorAllFormulaCells:
XSSFFormulaEvaluator.evaluateAllFormulaCells(xssfWorkbook)
More details are available on the poi website
wb.setForceFormulaRecalculation(true);
// replace "wb" with your HSSFWorkbook/XSSFWorkbook object
https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html#setForceFormulaRecalculation-boolean-
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html#setForceFormulaRecalculation-boolean-
I would like to know how to export the contents of a table or the data from a query to an Excel file. Also wich is the file extension that is better to export to, xls or csv?
Thanks in advance.
Edit: What i want is the user to to be able to export the contents of a JTable -containing the results from a query- to an Excel compatible file, by pressing a button.
I don't know what is the best way to do it? I found various ways but i'm not sure which one to follow. Is it possible to generate a JasperReport then export tha same data to excel?
Edit2:Ok so i decided to export to .csv like most of you suggest. My last question is which one is better to use, opecsv or javacsv? Both seem really easy to use.
Thanks!
Exporting to csv is easier - and could be done manually in a pinch depending on the data (Each new row is a new line, and cell values are seperated by a comma) - There are open source libraries available for this (http://opencsv.sourceforge.net/), and the code for copying a resultset to your output should be trivial
If you absolutely need Excel, use the Apache POI library.
You have to create text file (csv) and write the result of database.
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.csv")));
while(rs.next())
{
out.println(String.format("%s,%s,%s",rs.getString(1),rs.getString(2),rs.getString(3));
}
In addition to the answers already given, I would like to say that I would prefer CSV.
CSV is application-agnostic and you could manipulate the data later on with any other language/program (Python, R, Java, Excel, etc).
I had good success with jXLS:
http://jxls.sourceforge.net/
this lets you use JSP-like tags in a native Excel template with all the formatting etc. You pass data to substitute into that Excel template from Java API calls, via a Map structure (analogous to request scope vars.)
This is a good lighter-weight alternative to JasperReports if you just want formatted Excel output.
I am using apache poi api to deal with my spread sheet files.
I have observed, if we try to edit an existing .xls file it size is not the same as if that same file (same data ) is written in one go.
It is normal for an Excel spreadsheet to grow after being opened or edited. When a spreadsheet is opened in Microsoft Excel the formulas are automatically calculated, so this increases the size of the file. If a spreadsheet is opened by Apache POI it is up to the developer to call the (FormulaEvaluator) to update all the values. When a spreadsheet is read by Apache POI and the formulas have not been evaluated, formula answers may be invalid.
POI will always write out one record per cell
Excel, however, will sometimes bunch several similar sequential cells up into a single record. For example, if you have 3 cells in a row that are blank but styled, then excel will generate a MulBlankRecord which holds all of them. For several cells in a row with simple numbers in them, excel uses a MulRKRecord
When POI reads in a file, it expands all the Mul* records out. At write time, the individual cell records are written, so the file gets slightly bigger. I think there's an entry in the POI bugzilla for the enhancement to get POI to coalesce cells into Mul records, but no-one seems to have volunteered to work on it yet...
I am designing stock market predictor wherein the user is supposed to update his database of historical indexes through the web. I want to just add the new data to the TOP of my excel file. not Bottom but on Top of my file. Now i know i can make a copy and modify that copy and the regular stuff.
How to do i avoid this complicated steps and do in few steps instead?
is it possible to do this without making a copy
Apache POI's Sheet objects have a shiftRows method you could use to do this. Here's an example from the Busy Developers' Guide to HSSF and XSSF Features:
Shift rows up or down on a sheet
shiftRows API documentation
Here's an answer with some sample code:
How to create new rows in apache poi 3.6?
Why not update a .csv (which is easier) and at the end after all updates are done, convert it to .xls
I need to convert a semicolon delimited file to an Excel.
However, there are some fields that must be removed from the Excel file, and some extra fields to be added. These extra fields are drop-down fields.
Is there any way to do this? Programming language that is preferably to be used is Java, but also welcome the possibility to use Excel macro.
I'm pretty sure you can do this with vanilla Excel. You can either do a global search and replace on semicolon to comma and just open as CSV or use the "Text to Columns" feature.
EDIT: I've not done this programmatically in Java, but in Perl it should be pretty straightforward with Text::xSV and Spreadsheet::WriteExcel
You could look at opencsv and HSSF.
I'm not sure how to do this with an Excel macro, but for Java:
Read the file with FileReader
Use a StringTokenizer with a ";" delimeter to separate the fields
Make an array for each row holding a custom object representing each row. The object can store arrays for the data needed to populate the drop down box
Use Apache POI to create an Excel spreadsheet (There are lots of POI examples on Stackoverflow)
You have two options:
Use Apache POI to write and customise the XLS
Create a sample spreadsheet in Excel, but save it as an HTML page. Take the saved HTML and use it as a template for your data. You can save the output (template+data) as a file with .xls suffix. Even though its content is really HTML it will open correctly.
If you use CSV you won't be able to get additional features such as drop downs or styling.