Learn to generate Excel XLSX files in Java with Eclipse



It’s always interesting to be able to generate Microsoft Excel files in an application to propose reports to users for example. In that tutorial, you are going to learn how to generate Excel XLSX files in a Java Application with Eclipse.

Note that the logic will be the same to generate old Excel XLS format. For that tutorial, we are going to use the great Apache POI API added to the project as a Maven dependency.

The complete source code of this tutorial can be found on the SSaurel’s Blog :

Don’t hesitate to give it a try and give us your feedback in comments.

Nguồn: https://arabsn.net/

Xem thêm bài viết: https://arabsn.net/category/cong-nghe

13 thoughts on “Learn to generate Excel XLSX files in Java with Eclipse

  1. workbook won't change color. In my org.apache.poi.ss.usermodel.Workbook it says that this element neither has attached source nor attached Javadoc and hence no Javadoc could be found. Please help me..

  2. You may be interested in checking this alternative, its API is a bit simpler, smoother and faster:
    https://www.gemboxsoftware.com/spreadsheet-java/examples/create-write-excel-file-in-java/402

    For example, this is how you would create the same Excel file with it:

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import com.gembox.spreadsheet.*;

    public class App {

    private static String[] columns = { "First Name", "Last Name", "Email", "Date of Birth" };
    private static List<Contact> contacts = new ArrayList<>();

    public static void main(String[] args) throws IOException {

    contacts.add(new Contact("Sylvain", "Saurel", "sylvain.saurel@gmail.com", "17/01/1980"));
    contacts.add(new Contact("Albert", "Dupond", "sylvain.saurel@gmail.com", "17/01/1989"));
    contacts.add(new Contact("Johnny", "Clegg", "sylvain.saurel@gmail.com", "17/01/1956"));
    contacts.add(new Contact("Rob", "Robby", "sylvain.saurel@gmail.com", "17/01/1978"));
    contacts.add(new Contact("Dream", "Dreamer", "sylvain.saurel@gmail.com", "17/01/1987"));

    SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

    ExcelFile workbook = new ExcelFile();
    ExcelWorksheet sheet = workbook.addWorksheet("Contacts");

    ExcelFont headerFont = sheet.getRow(0).getStyle().getFont();
    headerFont.setWeight(ExcelFont.BOLD_WEIGHT);
    headerFont.setSize(17 * 20);
    headerFont.setColor(SpreadsheetColor.fromName(ColorName.RED));

    for (int i = 0; i< columns.length; i++) {
    sheet.getCell(0, i).setValue(columns[i]);
    }

    int rowNum = 1;
    for (Contact contact : contacts) {
    ExcelRow row = sheet.getRow(rowNum++);
    row.getCell(0).setValue(contact.firstName);
    row.getCell(1).setValue(contact.lastName);
    row.getCell(2).setValue(contact.email);
    row.getCell(3).setValue(contact.dateOfBirth);
    }

    workbook.save("contacts.xlsx");
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *