Create Excel file in Java

233,945

Solution 1

//Find jar from here "http://poi.apache.org/download.html"
import  java.io.*;
import  org.apache.poi.hssf.usermodel.HSSFSheet;
import  org.apache.poi.hssf.usermodel.HSSFWorkbook;
import  org.apache.poi.hssf.usermodel.HSSFRow;

public class CreateExlFile{
    public static void main(String[]args) {
        try {
            String filename = "C:/NewExcelFile.xls" ;
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("FirstSheet");  

            HSSFRow rowhead = sheet.createRow((short)0);
            rowhead.createCell(0).setCellValue("No.");
            rowhead.createCell(1).setCellValue("Name");
            rowhead.createCell(2).setCellValue("Address");
            rowhead.createCell(3).setCellValue("Email");

            HSSFRow row = sheet.createRow((short)1);
            row.createCell(0).setCellValue("1");
            row.createCell(1).setCellValue("Sankumarsingh");
            row.createCell(2).setCellValue("India");
            row.createCell(3).setCellValue("[email protected]");

            FileOutputStream fileOut = new FileOutputStream(filename);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
            System.out.println("Your excel file has been generated!");

        } catch ( Exception ex ) {
            System.out.println(ex);
        }
    }
}

Solution 2

You can use Apache POI for creating native binary xls files.

Or you can use JExcelApi which is another, and somewhat light-weight as far as I can remember, Java library for Excel.

Solution 3

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).

Solution 4

File fileName = new File(".....\\Fund.xlsx");

public static void createWorkbook(File fileName) throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream(fileName);
        XSSFWorkbook  workbook = new XSSFWorkbook();            

        XSSFSheet sheet = workbook.createSheet("fund");  

        Row row = sheet.createRow(0);   
        Cell cell0 = row.createCell(0);
        cell0.setCellValue("Nav Value");

        Cell cell1 = row.createCell(1);

        cell1.setCellValue("Amount Change");       

        Cell cell2 = row.createCell(2);
        cell2.setCellValue("Percent Change");

        workbook.write(fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Solution 5

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.

Share:
233,945
Admin
Author by

Admin

Updated on November 27, 2021

Comments

  • Admin
    Admin over 2 years

    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?

  • Dejell
    Dejell about 10 years
    Hi, this created for me a binary file. how can I create an excel file that I can open with Excel?
  • David Levy
    David Levy over 8 years
    Small question here - why are you casting the int value into HSSFSheet.createRow to a short? The API takes an int.
  • Hesham Saleh
    Hesham Saleh over 8 years
    Now in 2015 and was interested in using Apache POI but have doubts due to your experience, however just wanted to share this FAQ question on their website that I thought may be relevant for troubleshooting similar issues.
  • Sujatha Girijala
    Sujatha Girijala over 7 years
    How can i see that file existence. I am working on it from morning but no use. please help me out.
  • Evan
    Evan over 6 years
    @Hesham's link no longer links to a specific question. This one and this one seem relevant.
  • Pasha GR
    Pasha GR over 6 years
    Do you have other tools to generate pdf, csv... ?
  • Erieze Lagera
    Erieze Lagera over 6 years
    @PashaGharibi You may try JasperReports to generate PDF. It has IDE for creating the template of the report. Jaspersoft Studio
  • NixonUposseen
    NixonUposseen about 5 years
    I found that creating Excel files with GemBox.Spreadsheet for Java gave faster performances and less memory consumption/allocation. For instance, try out this example.
  • Vibhav Chaddha
    Vibhav Chaddha over 4 years
    HI @Fangming If I have an array and I want to create sheets up to the length of that array. For example: if I have an array of length 5, then I have to create 5 sheets and if that length is 4, then I have to create 4 sheets. And the length is not fixed. So how can I do that? Thanks in advance.
  • Sankumarsingh
    Sankumarsingh over 4 years
    @Me_developer. you are posting a new question as comment. Please raise a new question instead.
  • YourHelper
    YourHelper over 3 years
    this gives back a tar.gz file how can i get the jar file ???
  • helvete
    helvete over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • MD. RAKIB HASAN
    MD. RAKIB HASAN over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.