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
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 am currently creating a program that will take an excel spreadsheet and save the cell data in a 2D array. If a cell is empty then an empty string is used. I then generate a GUI using that data. My GUI needs to have input fields where the data can be extracted and used.
The excel spreadsheet has a set amount of columns but can have any amount of rows as well as empty cells.
I was wondering what the best data structure to use to store the data from the excel spreadsheet is that will allow me to edit the data and add new columns/rows.
Thanks.
If you are open to use a library, you might want to have a look at Apache POI instead of reinventing the wheel.
Assuming encryption isn't a concern, why not use a simple .csv file?
I need to find an specific string (id, name for example) in 1 sheet of excel.
this is a basic need.
Later on we need to find a user on several excel sheets and copy the whole record identified with that code and send it to a JTable in the frame.
Are you looking for a high-level search function or something? I don't think that exists.
As you load the sheets, you might consider just adding the interesting columns to a HashMap if you can use exact matches, otherwise just iterate over the sheets/columns/rows and search manually.
You could create some mid-level tooling to do this. A "Sheet Indexer" perhaps, that takes a sheet and a list of columns then lets you do lookups. Even if you have to write code to iterate over everything manually you shouldn't worry too much about speed--the number of sheets/rows are very unlikely to get large enough to effect performance or anything.
We actually have a lot of tooling built around poi including a ORM layer that lets us load from spreadsheets using annotations just like hibernate. We called it "son of poi" aka "poison".
I want to check whether given sheet (by name or number) exists in the file (xls
or xlsx) before I read it. I am using Event API to address memory footprint
issue and thus I don't want to use org.apache.poi.ss.usermodel.Workbook object.
I am using Apache POI 3.7 with JDK7. Can someone please help me?
Thanks,
Parag
The way to check using the UserModel code is the same for the two formats.
However, you've asked about the Event API. These are much lower level, so the differences between the two formats have to be handled by you as there's nothing in between to hide them. (If you want an easy life, just buy some more memory for your server and stick with the UserModel!)
For the .xls file format (HSSF), the details of the sheets are stored near the top of the file. Start processing the file, and wait for the BoundSheetRecord (sid=0x0085) to come past. When you've seen all of those, you'll know if your sheet is in the file or not. If it is, process as normal. If not, abort.
For the .xlsx file format (XSSF), open the file and grab the workbook part (it's fairly small). Check that for what sheets exist, then decide based on that if you want to process or not. If you're using XSSFReader, call getWorkbookData() to get the Workbook part, then probably use XmlBeans to process it (via WorkbookDocument.Factory)
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...