I want to create an Excel file and write data just like writing a text file with Java. I tried to change file extension from .txt
to .xls
. But I want to bold letters in the Excel file. How can I do that?
I have tried using the JXL API, but every time I have to create a label I want add no label. Can't O edit row and column of the table?
I've created an API to create an Excel file more easier.
Create Excel - Creating Excel from Template
Just set the required values upon instantiation then invoke execute(), it will be created based on your desired output directory.
But before you use this, you must have an Excel Template which will be use as a template of the newly created Excel file.
Also, you need Apache POI in your project's class path.
Changing the extension of a file does not in any way change its contents. The extension is just a label.
If you want to work with Excel spreadsheets using Java, read up on the Apache POI library.
To create a spreadsheet and format a cell using POI, see the Working with Fonts example, and use:
POI works very well. There are some things you can't do (e.g. create VBA macros), but it'll read/write spreadsheets with macros, so you can create a suitable template sheet, read it and manipulate it with POI, and then write it out.
Flat files do not allow providing meta information.
I would suggest writing out a HTML table containing the information you need, and let Excel read it instead. You can then use <b> tags to do what you ask for.
Fair warning about Apache POI's Excel generation... (I know this is an old post, but it's important in case someone looks this up again like I just did)
It had a memory leak issue, which supposedly was solved by 2006, but which people quite recently have still been experiencing. If you want to automate generating a large amount of excel (i.e., if you want to generate a single, large file, a large number of small files, or both), I'd recommend using a different API. Either that, or increasing the JVM stack size to preposterous proportions, and maybe looking into interning strings if you know you won't actually be working with many different strings (although, of course, interning strings means that if you have a large number of different strings, you'll have an entirely different program-crashing memory problem. So, consider that before you go that route).