I have to read a text file and write it to an already existing excel file. The excel file is a customized excel sheet with different items in different columns. The items has different values for each of them... These items with there value can be found in a text file. But i dont have much idea as to how to do this.
E.g- example.txt
Name: John
Age=24
Sex=M
Graduate=M.S
example.xlsx
Age: Sex:
Name: Graduate:
Thanks in advance :)
Just as for so many other problems that need solved, there's an Apache library for that! In this case, it's the POI library. I've only used it for very basic spreadsheet manipulation, but managed that by just following a few tutorials. I'd link to one, but I can't now remember where it was.
Please see Apache POI-HSSF library for reading and writing Excel files with Java. There are some quick guides to get you started.
This post How to read and write excel file in java might help you.
You can also create a *.csv (comma separated value) file in Java. Just create a simple text file with CSV extension and put your values in there like that :
Age:,24,Sex:,M,
So you just separate your values with commas (or other delimiters like ';').
Every line in this file is a row, and every delimiter separates two columns. You won't be able to add colours/styles/formatting this way, but it gives you a file that is openable and understandable even without Excel (or other spreadsheet software).
Related
I am basically trying to write to a csv file, one of the cells in that csv file will contain multiple lines but one cell. I have read online that if you wrap it around "" you will generally be fine. This is the case in finder, however when I try to open it in excel it does not work that way what is your suggestion
Try to use this tutorial.
https://www.baeldung.com/apache-commons-csv
But, post your code to more detailed response
I need to output data to a CSV file from Java, but in that csv file I hope to create multiple sheets so that data can be organized in a better way. After some googling, it seems this is not possible. A CSV file can only receive one-sheet data.
Is this true? If yes, what would be the options? Thank you.
CSV file is interpreted a sequence of characters which comply to some standardization, therefor it cannot contains more than one sheet. You can output your data in a Excel file that contains more than one sheet using the Apache POI api.
Comma Separated Value lists are generally created in plain text files, which do not have multiple pages. What you could do instead is create multiple CSV files, but if you really have a lot of data, a data base might be your best bet.
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 need to suggest an input, excel file or text file.
assuming the input is large number of lines where I need to read the first String, for example:
A,B,C,D....
I need to read the first String (in this case A) to identify the matching row, should I use excel file and use POI to read the first cell of each row? or text file where each line tokens are separated by delimiter and to parse each line reading the first token.
Use a text file. Because computers like it more. If business requires it, rename that text file into a "csv" file and you've got an Excel file.
If humans are going to enter data then use Excel. If the file is used as a communication channel between two systems use as simple as possible file.
If at all possible, use text file - much easier to handle/troubleshoot, easier to generate, uses less memory, does not have restrictions on number of rows, etc. In general - more predictable.
If you go with text files and you have people manually preparing those text files, and you are dealing with non-ASCII text, you better make sure everybody will send you the files in correct encoding (usually UTF-8 would be the best). This is not an issue with Excel.
The only reason to use Excel workbook would be when you need some "business-people" to produce those input files, then that input effectively becomes a user interface to your system - Excel is usually considered more user friendly than Notepad. ;-)
If you do go with Excel, make sure that the people producing those Excel files will give you the correct version (I assume you would want the "old" XLS format, not the new XLSX format).
Rule of thumb: use a text file. It's more interchangeable and way easier to handle by any other software you may need to support in a few years.
If you need some humans to edit those data and you need some beautiful/color display the Excel can provide, consider creating a macro that would store data in csv.
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.