Create excel file from java

I’ve created the API «generator-excel» to create an Excel file, below the dependecy:

<dependency>
  <groupId>com.github.bld-commons.excel</groupId>
  <artifactId>generator-excel</artifactId>
  <version>3.1.0</version>
</dependency>

This library can to configure the styles, the functions, the charts, the pivot table and etc. through a series of annotations.
You can write rows by getting data from a datasource trough a query with or without parameters.
Below an example to develop

  1. I created 2 classes that represents the row of the table.
  2. package bld.generator.report.junit.entity;
        
        import java.util.Date;
        
        import org.apache.poi.ss.usermodel.HorizontalAlignment;
        
        import bld.generator.report.excel.RowSheet;
        import bld.generator.report.excel.annotation.ExcelCellLayout;
        import bld.generator.report.excel.annotation.ExcelColumn;
        import bld.generator.report.excel.annotation.ExcelDate;
        import bld.generator.report.excel.annotation.ExcelImage;
        import bld.generator.report.excel.annotation.ExcelRowHeight;
        
        @ExcelRowHeight(height = 3)
        public class UtenteRow implements RowSheet {
            
            @ExcelColumn(columnName = "Id", indexColumn = 0)
            @ExcelCellLayout(horizontalAlignment = HorizontalAlignment.RIGHT)
            private Integer idUtente; 
            @ExcelColumn(columnName = "Nome", indexColumn = 2)
            @ExcelCellLayout
            private String nome; 
            @ExcelColumn(columnName = "Cognome", indexColumn = 1)
            @ExcelCellLayout
            private String cognome;
            @ExcelColumn(columnName = "Data di nascita", indexColumn = 3)
            @ExcelCellLayout(horizontalAlignment = HorizontalAlignment.CENTER)
            @ExcelDate
            private Date dataNascita;
            @ExcelColumn(columnName = "Immagine", indexColumn = 4)
            @ExcelCellLayout
            @ExcelImage(resizeHeight = 0.7, resizeWidth = 0.6)
            private byte[] image;   
            
            @ExcelColumn(columnName = "Path", indexColumn = 5)
            @ExcelCellLayout
            @ExcelImage(resizeHeight = 0.7, resizeWidth = 0.6)
            private String path;    
            
        
            public UtenteRow() {
            }
        
        
            public UtenteRow(Integer idUtente, String nome, String cognome, Date dataNascita) {
                super();
                this.idUtente = idUtente;
                this.nome = nome;
                this.cognome = cognome;
                this.dataNascita = dataNascita;
            }
        
        
            public Integer getIdUtente() {
                return idUtente;
            }
        
        
            public void setIdUtente(Integer idUtente) {
                this.idUtente = idUtente;
            }
        
        
            public String getNome() {
                return nome;
            }
        
        
            public void setNome(String nome) {
                this.nome = nome;
            }
        
        
            public String getCognome() {
                return cognome;
            }
        
        
            public void setCognome(String cognome) {
                this.cognome = cognome;
            }
        
        
            public Date getDataNascita() {
                return dataNascita;
            }
        
        
            public void setDataNascita(Date dataNascita) {
                this.dataNascita = dataNascita;
            }
        
        
            public byte[] getImage() {
                return image;
            }
        
        
            public String getPath() {
                return path;
            }
        
        
            public void setImage(byte[] image) {
                this.image = image;
            }
        
        
            public void setPath(String path) {
                this.path = path;
            }
        
        }
    

    package bld.generator.report.junit.entity;
    
    import org.apache.poi.ss.usermodel.DataConsolidateFunction;
    import org.apache.poi.ss.usermodel.HorizontalAlignment;
    
    import bld.generator.report.excel.RowSheet;
    import bld.generator.report.excel.annotation.ExcelCellLayout;
    import bld.generator.report.excel.annotation.ExcelColumn;
    import bld.generator.report.excel.annotation.ExcelFont;
    import bld.generator.report.excel.annotation.ExcelSubtotal;
    import bld.generator.report.excel.annotation.ExcelSubtotals;
    
    @ExcelSubtotals(labelTotalGroup = "Total",endLabel = "total")
    public class SalaryRow implements RowSheet {
    
        @ExcelColumn(columnName = "Name", indexColumn = 0)
        @ExcelCellLayout
        private String name;
        @ExcelColumn(columnName = "Amount", indexColumn = 1)
        @ExcelCellLayout(horizontalAlignment = HorizontalAlignment.RIGHT)
        @ExcelSubtotal(dataConsolidateFunction = DataConsolidateFunction.SUM,excelCellLayout = @ExcelCellLayout(horizontalAlignment = HorizontalAlignment.RIGHT,font=@ExcelFont(bold = true)))
        private Double amount;
        
        public SalaryRow() {
            super();
        }
        public SalaryRow(String name, Double amount) {
            super();
            this.name = name;
            this.amount = amount;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Double getAmount() {
            return amount;
        }
        public void setAmount(Double amount) {
            this.amount = amount;
        }
        
    }
    
  3. I created 2 class that represents the sheets.
  4. package bld.generator.report.junit.entity;
    
    import javax.validation.constraints.Size;
    
    import bld.generator.report.excel.QuerySheetData;
    import bld.generator.report.excel.annotation.ExcelHeaderLayout;
    import bld.generator.report.excel.annotation.ExcelMarginSheet;
    import bld.generator.report.excel.annotation.ExcelQuery;
    import bld.generator.report.excel.annotation.ExcelSheetLayout;
    
    @ExcelSheetLayout
    @ExcelHeaderLayout
    @ExcelMarginSheet(bottom = 1.5, left = 1.5, right = 1.5, top = 1.5)
    @ExcelQuery(select = "SELECT id_utente, nome, cognome, data_nascita,image,path "
            + "FROM utente "
            + "WHERE cognome=:cognome "
            + "order by cognome,nome")
    public class UtenteSheet extends QuerySheetData<UtenteRow> {
        
    
        public UtenteSheet(@Size(max = 31) String sheetName) {
            super(sheetName);
        }
    
        
    }
    

    package bld.generator.report.junit.entity;
    
    import javax.validation.constraints.Size;
    
    import bld.generator.report.excel.SheetData;
    import bld.generator.report.excel.annotation.ExcelHeaderLayout;
    import bld.generator.report.excel.annotation.ExcelMarginSheet;
    import bld.generator.report.excel.annotation.ExcelSheetLayout;
    @ExcelSheetLayout
    @ExcelHeaderLayout
    @ExcelMarginSheet(bottom = 1.5,left = 1.5,right = 1.5,top = 1.5)
    public class SalarySheet extends SheetData<SalaryRow> {
    
        public SalarySheet(@Size(max = 31) String sheetName) {
            super(sheetName);
        }
    
    }
    
  5. Class test, in the test function there are antoher sheets
  6. package bld.generator.report.junit;
    
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.List;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    import bld.generator.report.excel.BaseSheet;
    import bld.generator.report.excel.GenerateExcel;
    import bld.generator.report.excel.data.ReportExcel;
    import bld.generator.report.junit.entity.AutoreLibriSheet;
    import bld.generator.report.junit.entity.CasaEditrice;
    import bld.generator.report.junit.entity.GenereSheet;
    import bld.generator.report.junit.entity.SalaryRow;
    import bld.generator.report.junit.entity.SalarySheet;
    import bld.generator.report.junit.entity.TotaleAutoreLibriRow;
    import bld.generator.report.junit.entity.TotaleAutoreLibriSheet;
    import bld.generator.report.junit.entity.UtenteSheet;
    import bld.generator.report.utils.ExcelUtils;
    
    /**
     * The Class ReportTest.
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ConfigurationProperties
    @ComponentScan(basePackages = {"bld.generator","bld.read"})
    @EnableTransactionManagement
    public class ReportTestJpa {
    
        /** The Constant PATH_FILE. */
        private static final String PATH_FILE = "/mnt/report/";
    
        /** The generate excel. */
        @Autowired
        private GenerateExcel generateExcel;
    
        /**
         * Sets the up.
         *
         * @throws Exception the exception
         */
        @Before
        public void setUp() throws Exception {
        }
    
        /**
         * Test.
         *
         * @throws Exception the exception
         */
        @Test
        public void test() throws Exception {
            List<BaseSheet> listBaseSheet = new ArrayList<>();
            
            UtenteSheet utenteSheet=new UtenteSheet("Utente");
            utenteSheet.getMapParameters().put("cognome", "Rossi");
            listBaseSheet.add(utenteSheet);
            
            CasaEditrice casaEditrice = new CasaEditrice("Casa Editrice","Mondadori", new GregorianCalendar(1955, Calendar.MAY, 10), "Roma", "/home/francesco/Documents/git-project/dev-excel/linux.jpg","Drammatico");
            listBaseSheet.add(casaEditrice);
            
            
            AutoreLibriSheet autoreLibriSheet = new AutoreLibriSheet("Libri d'autore","Test label");
            TotaleAutoreLibriSheet totaleAutoreLibriSheet=new TotaleAutoreLibriSheet();
            totaleAutoreLibriSheet.getListRowSheet().add(new TotaleAutoreLibriRow("Totale"));
            autoreLibriSheet.setSheetFunctionsTotal(totaleAutoreLibriSheet);
            listBaseSheet.add(autoreLibriSheet);
            GenereSheet genereSheet=new GenereSheet("Genere");
            listBaseSheet.add(genereSheet);
            SalarySheet salarySheet=new SalarySheet("salary");
            salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
            salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
            salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
            salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
            salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
            salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
            salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
            salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
            listBaseSheet.add(salarySheet);
            ReportExcel excel = new ReportExcel("Mondadori JPA", listBaseSheet);
    
            byte[] byteReport = this.generateExcel.createFileXlsx(excel);
    
            ExcelUtils.writeToFile(PATH_FILE,excel.getTitle(), ".xlsx", byteReport);
    
        }
    
        
    
    }
    
  7. Application yaml
  8. logging:
      level:
        root: WARN
        org:
          springframework:
            web: DEBUG
          hibernate: ERROR
    
    
    
    spring:
      datasource:
        url: jdbc:postgresql://localhost:5432/excel_db
        username: ${EXCEL_USER_DB}
        password: ${EXCEL_PASSWORD_DB}
      jpa:
        show-sql: true
        properties:
          hibernate:
            default_schema: public
            jdbc:
              lob:
                non_contextual_creation: true 
            format_sql: true    
            ddl-auto: auto
        database-platform: org.hibernate.dialect.PostgreSQLDialect
        generate-ddl: true
    

below the link of the project on github:

  • https://github.com/bld-commons/dev-excel

In Java, read excel file and write excel file is a bit tricky because excel worksheet has cells to store data. Java does not provide direct API to read or write Microsoft Excel or Word documents. We have to rely on the third-party library that is Apache POI. In this section, we will learn how to create an excel file in Java and how to write or insert data in the excel file using the Apache POI Java library.

Java Apache POI Library

Apache POI (Poor Obfuscation Implementation) is a Java API for reading and writing Microsoft Documents. It contains classes and interfaces. The Apache POI library provides two implementations for reading or writing excel files:

  • HSSF (Horrible SpreadSheet Format) Implementation:It denotes an API that is working with Excel 2003 or earlier versions.
  • XSSF (XML SpreadSheet Format) Implementation:It denotes an API that is working with Excel 2007 or later versions.

Throughout this section, we have used HSSF implementation.

Create Excel File in Java

Follow the steps given below to create an excel file in Java.

Step 1: Create a Java project in eclipse. We have created a Java project with the name CreateExcelEile.

Step 2: Create a class with the name CreateExcelFileExample1 and write the code that we have written in CreateExcelFileExample1.java file.

Step 3: Download the Apache POI library (poi-3.17.jar).

Step 4: Add the Apache POI to the project. Right-click on the project -> Build Path -> Configure Build Path. It opens the Properties window for the current project.

Step 5: In the Properties window, click on the Add External JARs button.

Java Create Excel File

Step 6: Go to the path where the poi-3.17.jar file is located. Select the JAR file and click on the Open button. It adds the JAR file to the project. After that, click on the Apply and Close button to apply the changes.

Java Create Excel File

When we have done with all the above steps, the project structure looks like the following:

Java Create Excel File

Let’s understand the following code.

In the following program, we have used the Apache POI library to create an excel file. The library provides the class named HSSFWorkbook defined in the org.apache.poi.hssf.usermodel package.

CreateExcelFileExample1.java

Output:

Java Create Excel File

It creates a blank excel file at the specified location.

Java Create Excel File

Let’s create another Java program that creates an excel file.

CreateExcelFileExample2.java

Output:

Java Create Excel File

It creates a blank excel file at the specified location.

Java Create Excel File

Insert Data in the Excel File

Still, we have created a blank excel file. But it is not always necessary that create a blank excel file only. Some times we need to insert some data in the worksheet. Let’s see how to insert data in an excel file through a Java program.

CreateExcelFileExample3.java

Output:

Java Create Excel File

It creates an excel file at the specified location with the values that we have inserted using the setCellValue() method.

Java Create Excel File


Details
Written by  
Last Updated on 30 May 2019   |   Print  Email

In this tutorial, I’d love to share with you guys some examples of writing data to Excel files using the Apache POI library. If today is the first day you get to know Apache POI and you haven’t written any code snippet to read/write Excel files yet, I recommend you to read the sections 1 and 2 in the tutorial How to Read Excel Files in Java using Apache POI to understand the fundamentals of Apache POI.

Or if you are a kind of person who likes getting your hands-dirty first, let jump directly into the following examples.

 

1. Apache POI API Basics for Writing Excel Files

The fundamental interfaces include Workbook, Sheet, Row and Cell. For basic formatting, use the CellStyle and Font interfaces. Concrete implementing classes include:

  • Excel 2003: HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell, etc.
  • Excel 2007: XSSFWorkbook, XSSFSheet, XSSFRow, XSSFCell, etc.

But I recommend using the common interfaces for greater flexibility with both Excel formats 2003 (XLS) and 2007(XLSX).

Here are the basic steps for writing an Excel file:

  1. Create a Workbook.
  2. Create a Sheet.
  3. Repeat the following steps until all data is processed:
    1. Create a Row.
    2. Create Cellsin a Row. Apply formatting using CellStyle.
  4. Write to an OutputStream.
  5. Close the output stream.

Now, let’s see some examples that demonstrate writing a list of books to an Excel file.

 

2. A Simple Example to create an Excel file in Java

The following code snippet is a very simple program that demonstrates writing a list of books to an Excel file in the simplest and dirty form:

package net.codejava.excel;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * A very simple program that writes some data to an Excel file
 * using the Apache POI library.
 * @author www.codejava.net
 *
 */
public class SimpleExcelWriterExample {

	public static void main(String[] args) throws IOException {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("Java Books");
		
		Object[][] bookData = {
				{"Head First Java", "Kathy Serria", 79},
				{"Effective Java", "Joshua Bloch", 36},
				{"Clean Code", "Robert martin", 42},
				{"Thinking in Java", "Bruce Eckel", 35},
		};

		int rowCount = 0;
		
		for (Object[] aBook : bookData) {
			Row row = sheet.createRow(++rowCount);
			
			int columnCount = 0;
			
			for (Object field : aBook) {
				Cell cell = row.createCell(++columnCount);
				if (field instanceof String) {
					cell.setCellValue((String) field);
				} else if (field instanceof Integer) {
					cell.setCellValue((Integer) field);
				}
			}
			
		}
		
		
		try (FileOutputStream outputStream = new FileOutputStream("JavaBooks.xlsx")) {
			workbook.write(outputStream);
		}
	}

}

This program creates an Excel 2007 document which looks like the following screenshot (File: JavaBooks.xlsx):

Output Excel File

 

3. A More Object-Oriented Example to create an Excel file in Java

The following code snippets demonstrate a nicer program that focuses on OOP approach. That makes the program more flexible and easy to extend or upgrade in the future.

  • Create the model class (Book.java):
    package net.codejava.excel;
    
    public class Book {
    	private String title;
    	private String author;
    	private float price;
    
    	public Book() {
    	}
    
    	public Book(String title, String author, double price) {
    		this.title = title;
    		this.author = author;
    		this.price = price;
    	}
    
    	// getters and setters
    }
  • The method that writes a list of books to an Excel file (in 2003 format):
    public void writeExcel(List<Book> listBook, String excelFilePath) throws IOException {
    	Workbook workbook = new HSSFWorkbook();
    	Sheet sheet = workbook.createSheet();
    
    	int rowCount = 0;
    
    	for (Book aBook : listBook) {
    		Row row = sheet.createRow(++rowCount);
    		writeBook(aBook, row);
    	}
    
    	try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
    		workbook.write(outputStream);
    	}
    }
  • The method that writes information of a book to cells:
    private void writeBook(Book aBook, Row row) {
    	Cell cell = row.createCell(1);
    	cell.setCellValue(aBook.getTitle());
    
    	cell = row.createCell(2);
    	cell.setCellValue(aBook.getAuthor());
    
    	cell = row.createCell(3);
    	cell.setCellValue(aBook.getPrice());
    }
  • The following method creates some dummy data (a list of books):
    private List<Book> getListBook() {
    	Book book1 = new Book("Head First Java", "Kathy Serria", 79);
    	Book book2 = new Book("Effective Java", "Joshua Bloch", 36);
    	Book book3 = new Book("Clean Code", "Robert Martin", 42);
    	Book book4 = new Book("Thinking in Java", "Bruce Eckel", 35);
    
    	List<Book> listBook = Arrays.asList(book1, book2, book3, book4);
    
    	return listBook;
    }
  • And the following code snippet is for testing:
    NiceExcelWriterExample excelWriter = new NiceExcelWriterExample();
    
    List<Book> listBook = excelWriter.getListBook();
    String excelFilePath = "NiceJavaBooks.xls";
    
    excelWriter.writeExcel(listBook, excelFilePath);

 

4. Formatting Cells of the Excel file

Of course you may need to format the Excel file to make it looks nicely and professionally. Formatting is diversity and quite complex so in this introductory tutorial, I just show you how to format the basics like setting font style. Here are the steps:

  • Create a CellStyle object what holds formatting information:
    CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
  • Invoke the appropriate setters to apply the formatting you want. For example:
    cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
    cellStyle.setFont(font);
    cellStyle.setWrapText(true);

For example, the following method writes a header row for the document with font style bold and size is 16:

private void createHeaderRow(Sheet sheet) {

	CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
	Font font = sheet.getWorkbook().createFont();
	font.setBold(true);
	font.setFontHeightInPoints((short) 16);
	cellStyle.setFont(font);

	Row row = sheet.createRow(0);
	Cell cellTitle = row.createCell(1);

	cellTitle.setCellStyle(cellStyle);
	cellTitle.setCellValue("Title");

	Cell cellAuthor = row.createCell(2);
	cellAuthor.setCellStyle(cellStyle);
	cellAuthor.setCellValue("Author");

	Cell cellPrice = row.createCell(3);
	cellPrice.setCellStyle(cellStyle);
	cellPrice.setCellValue("Price");
}

You can see the complete program (FormattedExcelWriterExample.java) which can be found in the source code attached to this article.

 

5. Writing both Excel 2003 and Excel 2007 formats in Java

For better flexibility (supporting both common Excel formats), I recommend writing a factory method that either returns a HSSFWorkbook or XSSFWorkbook, depending on the extension of the file (.xls or .xlsx). Here’s the method:

private Workbook getWorkbook(String excelFilePath)
		throws IOException {
	Workbook workbook = null;

	if (excelFilePath.endsWith("xlsx")) {
		workbook = new XSSFWorkbook();
	} else if (excelFilePath.endsWith("xls")) {
		workbook = new HSSFWorkbook();
	} else {
		throw new IllegalArgumentException("The specified file is not Excel file");
	}

	return workbook;
}

You can see the complete program (FlexibleExcelWriterExample.java) which can be found in the source code attached to this article.

That’s how to read Excel files in Java programmatically. To learn more in-depth about Java programming, this Java software developers course would be good choice.

Related Java Excel Tutorials:

  • How to Read Excel Files in Java using Apache POI
  • Java Example to Read Password-protected Excel Files Using Apache POI
  • Java Example to Update Existing Excel Files Using Apache POI
  • Working with Formula Cells in Excel using Apache POI

 

References

  • Apache POI — the Java API for Microsoft Documents
  • POI API Documentation (Javadocs)
  • Apache POI Quick Guide
  • Apache POI HOWTO

 

About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.

Add comment

В прошлых двух статьях мы познакомились с библиотекой Apache POI, а также разобрались со считыванием данных из Excel документов в форматах .xls и .xlsx. Сегодня мы продолжим изучение возможностей этой библиотеки и попробуем создать новый Excel файл с формулами и стилями. Скачать проект Вы сможете в конце статьи.

Если Вы еще не знакомы с Apache POI, то рекомендую вкратце ознакомится с ее возможностями и способами ее подключения в проект по этой ссылке.

Внимание, код не очень красив и оптимизирован. Я хотел просто продемонстрировать возможности этой удобной библиотеки.

Для начала давайте создадим простой xls файл и запишем в него какие-то данные. А далее будем применять к нему стили и добавлять формулы.

Для удобной работы с данными нам потребуется дополнительный класс, который будет представлять собой модель данных, которую мы будем записывать в файл:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

package ua.com.prologistic.model;

public class DataModel {

    private String name;

    private String surname;

    private String city;

    private Double salary;

    public DataModel() {

    }

    public DataModel(String name, String surname, String city, Double salary) {

        this.name = name;

        this.surname = surname;

        this.city = city;

        this.salary = salary;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getSurname() {

        return surname;

    }

    public void setSurname(String surname) {

        this.surname = surname;

    }

    public String getCity() {

        return city;

    }

    public void setCity(String city) {

        this.city = city;

    }

    public Double getSalary() {

        return salary;

    }

    public void setSalary(Double salary) {

        this.salary = salary;

    }

}

Как видим, это простой класс с полями для имени, фамилии, города и зарплаты какого-то человека.

Ниже представлен листинг класса, в котором создается сам Excel файл:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

package ua.com.prologistic.excel;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Row;

import ua.com.prologistic.model.DataModel;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.ParseException;

import java.util.ArrayList;

import java.util.List;

public class ExcelWorker {

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

        // создание самого excel файла в памяти

        HSSFWorkbook workbook = new HSSFWorkbook();

        // создание листа с названием «Просто лист»

        HSSFSheet sheet = workbook.createSheet(«Просто лист»);

        // заполняем список какими-то данными

        List<DataModel> dataList = fillData();

        // счетчик для строк

        int rowNum = 0;

        // создаем подписи к столбцам (это будет первая строчка в листе Excel файла)

        Row row = sheet.createRow(rowNum);

        row.createCell(0).setCellValue(«Имя»);

        row.createCell(1).setCellValue(«Фамилия»);

        row.createCell(2).setCellValue(«Город»);

        row.createCell(3).setCellValue(«Зарплата»);

        // заполняем лист данными

        for (DataModel dataModel : dataList) {

            createSheetHeader(sheet, ++rowNum, dataModel);

        }

        // записываем созданный в памяти Excel документ в файл

        try (FileOutputStream out = new FileOutputStream(new File(«F:\Apache POI Excel File.xls»))) {

            workbook.write(out);

        } catch (IOException e) {

            e.printStackTrace();

        }

        System.out.println(«Excel файл успешно создан!»);

    }

    // заполнение строки (rowNum) определенного листа (sheet)

    // данными  из dataModel созданного в памяти Excel файла

    private static void createSheetHeader(HSSFSheet sheet, int rowNum, DataModel dataModel) {

        Row row = sheet.createRow(rowNum);

        row.createCell(0).setCellValue(dataModel.getName());

        row.createCell(1).setCellValue(dataModel.getSurname());

        row.createCell(2).setCellValue(dataModel.getCity());

        row.createCell(3).setCellValue(dataModel.getSalary());

    }

    // заполняем список рандомными данными

    // в реальных приложениях данные будут из БД или интернета

    private static List<DataModel> fillData() {

        List<DataModel> dataModels = new ArrayList<>();

        dataModels.add(new DataModel(«Howard», «Wolowitz», «Massachusetts», 90000.0));

        dataModels.add(new DataModel(«Leonard», «Hofstadter», «Massachusetts», 95000.0));

        dataModels.add(new DataModel(«Sheldon», «Cooper», «Massachusetts», 120000.0));

        return dataModels;

    }

}

Обратите внимание, что мы использовали try with resources — одну из особенностей Java 7. А это значит, что нам не нужно беспокоится о закрытии файла вручную. В Java 7 конструкцию try-catch-finally можно не использовать, так как ей на смену пришла try with resources, которая сама закрывает открытые файлы или потоки без вашего вмешательства.

После запуска приведенной выше программы, в корне проекта создастся файл с названием Apache POI Excel File.xls. Давайте посмотрим на его содержимое:

Apache POI Excel File.xls

Лист Excel файла называется «Просто лист», как мы и называли, а данные расположены правильно.

Добавление стилей в Excel документ на Java

Теперь давайте поупражняемся с обновлением файла, а именно добавлением стилей. Например, выделим имя столбцов из первой строки жирным.

Для этого нам понадобится еще один метод setBoldStyle():

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

private static void setBoldStyle() throws IOException {

        // получаем файл с диска

        FileInputStream file = new FileInputStream(new File(«F:\Apache POI Excel File.xls»));

        // считываем его в память

        HSSFWorkbook workbook = new HSSFWorkbook(file);

        // говорим, что хотим работать с первым листом

        HSSFSheet sheet = workbook.getSheetAt(0);

        // создаем шрифт

        HSSFFont font = workbook.createFont();

        // указываем, что хотим его видеть жирным

        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        // создаем стиль для ячейки

        HSSFCellStyle style = workbook.createCellStyle();

        // и применяем к этому стилю жирный шрифт

        style.setFont(font);

        // получаем первую строку листа excel файла

        Row row = sheet.getRow(0);

        // проходим по всем ячейкам этой строки

        for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {

            // применяем созданный выше стиль к каждой ячейке

            row.getCell(i).setCellStyle(style);

        }

        // получаем доступ к excel файлу и обновляем его

        try (FileOutputStream out = new FileOutputStream(new File(«F:\Apache POI Excel File.xls»))) {

            workbook.write(out);

        } catch (IOException e) {

            e.printStackTrace();

        }

        System.out.println(«Excel файл успешно обновлен!»);

    }

Как видите, мы просто обходим все ячейки первой строки и применяем к ней стиль BOLD.

Результат выполнения этого кода представлен ниже:

excel file updated

С помощью Apache POI это делается быстро и удобно.

Добавление формул в Excel документ на Java

Теперь попробуем разобраться с добавлением формул с помощью Apache POI.

Apache POI не позволяет устанавливать в ячейки значения из знаком равно: «=».

Пример:

Можно делать так:

cell.setCellFormula(«D2+D3+D4»);

и вот так:

cell.setCellFormula(«SUM(D2:D4)»);

Но если Вы попробуете написать так:

cell.setCellFormula(«=D2*D3*D4»);

то вылетит exception с сообщением о недопустимой операции. Apache POI не позволяет работать с формулами таким образом.

И так, в теории мы подкованы, теперь добавим простую формулу. Для этого напишем еще один метод setFormula():

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

private static void setFormula() throws IOException {

        // получаем файл с диска

        FileInputStream file = new FileInputStream(new File(«F:\Apache POI Excel File.xls»));

        // считываем его в память

        HSSFWorkbook workbook = new HSSFWorkbook(file);

        // говорим, что хотим работать с первым листом

        HSSFSheet sheet = workbook.getSheetAt(0);

        // создаем 5ю строку листа excel файла

        // (сейчас она null, так как в ней нет никаких данных)

        Row row = sheet.createRow(4);

        // идем в 4ю ячейку строки и устанавливаем

        // формулу подсчета зарплат (столбец D)

        Cell sum = row.createCell(3);

        sum.setCellFormula(«D2+D3+D4»);

        // создаем шрифт

        HSSFFont font = workbook.createFont();

        // указываем, что хотим его видеть красным

        font.setColor(Font.COLOR_RED);

        // создаем стиль для ячейки

        HSSFCellStyle style = workbook.createCellStyle();

        // и применяем к этому стилю жирный шрифт

        style.setFont(font);

        sum.setCellStyle(style);

        // получаем доступ к excel файлу и обновляем его

        try (FileOutputStream out = new FileOutputStream(new File(«F:\Apache POI Excel File.xls»))) {

            workbook.write(out);

        } catch (IOException e) {

            e.printStackTrace();

        }

        System.out.println(«Excel файл успешно обновлен!»);

    }

В нем мы открываем файл, добавляем к нему еще одну строку с ячейкой для подсчета суммы зарплаты физиков. И да, выделяем ее красным цветом.

Результат выполнения представленного выше метода:

excel file updated with formula

Урок по созданию нового Excel файла в Java с помощью Apache POI подошел к концу. Скачать рабочий проект можно по этой ссылке.

Подробнее о считывании Excel файлов Вы найдете здесь.

Подписывайтесь на обновления и получайте новые уроки по Java и Android сразу на почту!

In this Java Apache POI tutorial we learn step by step to implement a Java program to export data from Java objects to an Excel file using the Apache POI library.

Table of contents

  1. Add Apache POI dependencies to Java project
  2. Create New Entity Class
  3. Implement Excel File Exporter Class
  4. How to use ExcelFileExporter class to export Excel file

Add Apache POI dependencies to Java project

First step, we add the Apache POI dependencies to the Java project.

If you use Gradle build project, add the following dependencies to the build.gradle file.

implementation group: 'org.apache.poi', name: 'poi', version: '5.2.2'
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.2'

If you use Maven build project, add the following dependencies to the pom.xml file.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.2</version>
</dependency>

Create New Entity Class

For example, we want to implement a Java application to export a list of contacts data to an Excel file. At this step we implement a new Java class named Contact to represent the contact information which needs to be exported.

Contact.java

public class Contact {
    private String firstName;
    private String lastName;
    private String email;

    public Contact(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Implement Excel File Exporter Class

At this step we implement a new Java class named ExcelFileExporter with the following steps to export an Excel file from given data.

  • Create a new Workbook object which represent an Excel workbook.
  • Create a new Sheet object which represent an Excel worksheet.
  • Create a new header Row from given header texts. At this step we use CellStyle class to define header foreground color and border.
  • Create new data rows from given list of Contact objects. At this step we use CellStyle class to define cell border for data rows.
  • Using Sheet.autoSizeColumn(int column) method to adjusts column width to fit the contents for three first columns.
  • Using the Workbook.write(OutputStream stream) method to write Excel file with given name.

ExcelFileExporter.java

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Excel File Exporter
 */
public class ExcelFileExporter {

    public void exportExcelFile(List<Contact> contacts, String[] headers, String fileName) {
        // create a new Workbook
        Workbook workbook = new XSSFWorkbook();

        // Create a new Sheet named "Contacts"
        Sheet sheet = workbook.createSheet("Contacts");

        // Create header row
        createHeaderRow(workbook, sheet, headers);

        // Create rows
        for(int i = 0; i < contacts.size(); i++) {
            // row index equals i + 1 because the first row of Excel file is the header row.
            int rowIndex = i + 1;
            createNewRow(workbook, sheet, rowIndex, contacts.get(i));
        }

        // Adjusts 3 columns to set the width to fit the contents.
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);
        sheet.autoSizeColumn(2);

        // Write to file
        try (OutputStream outputStream = new FileOutputStream(fileName)) {
            workbook.write(outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Create header row
     * @param workbook the Workbook object
     * @param sheet the Sheet object
     * @param headers the headers text
     */
    private void createHeaderRow(Workbook workbook, Sheet sheet, String[] headers) {
        Row headerRow = sheet.createRow(0);
        CellStyle headerCellStyle = workbook.createCellStyle();
        headerCellStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.index);
        headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerCellStyle.setBorderTop(BorderStyle.THIN);
        headerCellStyle.setTopBorderColor(IndexedColors.BLACK.index);
        headerCellStyle.setBorderRight(BorderStyle.THIN);
        headerCellStyle.setRightBorderColor(IndexedColors.BLACK.index);
        headerCellStyle.setBorderBottom(BorderStyle.THIN);
        headerCellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
        headerCellStyle.setBorderLeft(BorderStyle.THIN);
        headerCellStyle.setLeftBorderColor(IndexedColors.BLACK.index);

        for(int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerCellStyle);
        }
    }

    /**
     * Create a new row
     * @param workbook the Workbook object
     * @param sheet the Sheet object
     * @param rowIndex the index of row to create
     * @param contact the Contact object which represent information to write to row.
     */
    private void createNewRow(Workbook workbook, Sheet sheet, int rowIndex, Contact contact) {
        Row row = sheet.createRow(rowIndex);
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setTopBorderColor(IndexedColors.BLACK.index);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setRightBorderColor(IndexedColors.BLACK.index);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.index);

        Cell cell = row.createCell(0);
        cell.setCellValue(contact.getFirstName());
        cell.setCellStyle(cellStyle);

        cell = row.createCell(1);
        cell.setCellValue(contact.getLastName());
        cell.setCellStyle(cellStyle);

        cell = row.createCell(2);
        cell.setCellValue(contact.getEmail());
        cell.setCellStyle(cellStyle);
    }

}

How to use ExcelFileExporter class to export Excel file

At this final step we implement a Main class and use the ExcelFileExporter above to export Excel file named contacts.xlsx as the following Java code.

Main.java

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String... args) {
        // List of contacts to export to Excel file.
        List<Contact> contacts = new ArrayList<>();
        contacts.add(new Contact("Rayhan", "Harmon", "rayhan@simplesolution.dev"));
        contacts.add(new Contact("Paddy", "Sheridan", "paddy@simplesolution.dev"));
        contacts.add(new Contact("Clara", "Callaghan", "clara@simplesolution.dev"));
        contacts.add(new Contact("Josephine", "Amos", "josephine@simplesolution.dev"));
        contacts.add(new Contact("Sheikh", "Tucker", "sheikh@simplesolution.dev"));

        // Header text
        String[] headers = new String[] {"First Name", "Last Name", "Email"};

        // File name
        String fileName = "contacts.xlsx";

        // Export Excel file
        ExcelFileExporter excelFileExporter = new ExcelFileExporter();
        excelFileExporter.exportExcelFile(contacts, headers, fileName);
    }
}

Execute the the above Java application we have the output contacts.xlsx file as below.

Java Create Excel File .xlsx using Apache POI

Happy Coding 😊

Related Articles

Java Apache POI Tutorial

Spring Boot Download Excel File Export from MySQL Database

Spring Boot Web Application Download Excel File

Java Read Excel File using Apache POI

Java Read Excel Workbook from File using Apache POI

Java Read Excel Workbook from InputStream using Apache POI

Java Read Password Protected Excel File using Apache POI

Java How to Iterate over Sheets Rows and Cells of Excel file using Apache POI

Java Add Rows to Existing Excel File using Apache POI

Java Add Sheet to Existing Excel File using Apache POI

Java Remove Sheet from Existing Excel File using Apache POI

Java Create Formula Excel Cells using Apache POI

Понравилась статья? Поделить с друзьями:
  • Create excel file create excel document
  • Create excel drop down lists
  • Create excel apache poi
  • Create drop down menus excel
  • Create database with excel