How to read from a dynamic excel file? - java

Values in my dynamic excel file are changing every second, and i would like to read the new values through java. The problem I am facing is that I have to always save the file and then load its values to the program. Is it possible that excel internally saves the file automatically so that I'm loaded with new values every time.

Aman, can you clarify from where your excel sheet being updated dynamically? if you are writing it via another program, then you must use write method in the output stream. then only the values will be persisted

Values in my dynamic excel file are changing every second
Does this means it is saved in hard drive already or to be saved?
Who is changing the value? You or other application?
Make sure the changes are persisted. Other wise you are reading the same value from HD, not from RAM.
Is it possible that excel internally saves the file automatically
For this you need to use VBA., which I don't think you need to know.
But you can do that yourself from Java.

You keep this excel file in a folder and start a thread that will monitor the excel file. Means you can poll this excel file and get the latest content in your java code.

Related

Update objects written to a text files in java

Writing Java objects or a List into a text file is ok. But I want to know how I can update or rewrite a object which was written previously without writing objects again. For example, let s assume there is a java.util.List has a set of Objects. and then that list is written to a text file. Then later that file will be read again and get all objects from list and then change one object's value at run time by a java application. Then I don't need to write entire list back to the text file. Instead only the updated object in the list is required to be rewritten or updated in the text file without rewriting the whole list again. Any suggestion, or helpful source with sample codes please.
Take a look at RandomAccessFile. This will let you seek to the place in the file you want, and only update the part that you want to update.
Also take a look at this question on stackoverflow.
Without some fairly complex logic, you won't usually be able to update an object without rewriting the entire file. For example, if one of the objects on your list contains a string "shortstring", and you need to update it with string "muchmuchlongerstring", there will be no space in the file for the longer string without rewriting all the following content in the file.
If you want to persist large object trees to a file and still have the ability to update them, your code will be less buggy and life will be simplified by using one of the many file-based DBs out there, like:
SQLite (see Java and SQLite)
Derby
H2 (disk-based tables)

How to check if sheet exist?

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)

About quickfix .seqnums file

I want to read the value in quickfixj .seqnums file and write that value in another file or overwrite existing value with another value using java.i am beginner in java tell me how i can do this my boss give me the task.help me
We're not going do your job for you, but here is a set of steps:
Open the file (it's a text file - if you'd bothered to cat the file you would have seen)
There are two numbers - figure out which is which
Update as required
Write the new values in the same format to the file.

Java: How to export data (db, query or table contents) to Excel?

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.

Rapidly changing Configuration/Status File? JAVA

I need some way to store a configuration/status file that needs to be changed rapidly. The status of each key value pair (key-value) is stored in that file. The status needs to be changed rather too rapidly as per the status of a communication (Digital multimedia broadcasting) hardware.
What is the best way to go about creating such a file? ini? XML? Any off the shelf filewriter in Java? I can't use databases.
It sounds like you need random access to update parts of the file frequently without re-writing the entire file. Design binary file format and use RandomAccessFile API to read/write it. You are going to want to use fixed number of bytes for key and for value, such that you can index into the middle of the file and update the value without having to re-write all of the following records. Basically, you would be re-implementing how a database stores a table.
Another alternative is to only store a single key-value pair per file such that the cost of re-writing the file is minor. Maybe you can think of a way to use file name as the key and only store value in the file content.
I'd be inclined to try the second option unless you are dealing with more than a few thousand records.
The obvious solution would be to put the "configuration" information into a Properties object, and then use Properties.store(...) or Properties.storeToXML(...) to save to a file output stream or writer.
You also need to do something to ensure that whatever is reading the file will see a consistent snapshot. For instance, you could write to a new file each time and do a delete / rename dance to replace the the old with the new.
But if the update rate for the file is too high, you are going to create a lot of disc traffic, and you are bound slow down your application. This is going to apply (eventually) no matter what file format / API you use. So, you may want to consider not writing to a file at all.
At some point, configuration that changes too rapidly becomes "program state" and not configuration. If it is changing so rapidly, why do you have confidence that you can meaningfully write it to, and then read it from, a filesystem?
Say more about what the status is an who the consumer of the data is...

Categories