Java creates an Excel document

Spire.XLS for Java A set of Excel with abundant functions. Supports creation, editing, conversion and printing of Excel files in Java applications. In this paper, I will show you how to create an Excel file and add data using Spire.XLS for Java.

Tools used: Free Spire.XLS for Java (free version)

https://www.e-iceblue.com/Introduce/free-xls-for-java.html

Getting and installing Jar files:

Method 1: Download the jar file bag through the homepage. After downloading, unzip the file and install the Spire.xls.jar file under the lib folder into your Java program.

https://www.e-iceblue.com/Download/xls-for-java-free.html

Method 2: Introduced by installing maven warehouse.

https://www.e-iceblue.com/Tutorials/Licensing/How-to-install-Spire.PDF-for-Java-from-Maven-Repository.html


import com.spire.xls.*;
import java.awt.*;

public class CreateExcel {
    public static void main(String[] args){
        
        //New Workbook example
        Workbook workbook = new Workbook();

        //Get the first sheet.(The new workbook has 3 sheets by default)
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Set the name of the first sheet
        sheet.setName("Data Sheet");

        //Create the first cell style for the column
        CellStyle style1 = workbook.getStyles().addStyle("Header Style");
        style1.getFont().setSize(12f);
        style1.getFont().setColor(Color.BLACK);
        style1.getFont().isBold(true);
        style1.setHorizontalAlignment(HorizontalAlignType.Center);
        style1.setVerticalAlignment(VerticalAlignType.Center);

        //Set data cell style
        CellStyle style2 = workbook.getStyles().addStyle("Data Style");
        style2.getFont().setSize(10f);
        style2.getFont().setColor(Color.BLACK);

        //Add data to the first cell of the column and apply the style
        for (int column=1; column<5; column++)
        {
            CellRange header =sheet.getCellRange(1,column);
            header.setValue("Column " + column );
            header.setStyle(style1);
            header.setColumnWidth(15f);
        }

        //Add data to the data cell and apply the style
        for (int row=2; row<11; row++)
        {
            for (int column=1; column<5; column++)
            {
                CellRange cell = sheet.getCellRange(row, column);
                cell.setValue("Data " + row + ", " + column);
                cell.setStyle(style2);
            }
        }

        //Save result file
        workbook.saveToFile("CreateExcel.xlsx", FileFormat.Version2013);
    }
}

Effect diagram: Sample.png

Recommended Posts

Java creates an Excel document
Java creates a Word document
Java sets Excel document properties
Java creates a pie chart in Excel
Java 11 document summary
Java compressed PDF document
[java] throw an exception
Print Java Excel Worksheet
About an instance of java
Save Java PDF in Excel
Java merge & unmerge Excel cell
Let's operate Excel with Java! !!
Importing Excel data in Java 2
Java turns Excel into PDF
Import Excel data in Java
Java addition excel data validation
Importing Excel data in Java 3
Create an immutable class with JAVA
I sent an email in Java
Java Excel Insertion and Image Extraction
Java learning memo (creating an array)
[Java] Declare and initialize an array
Create an excel file with poi
Try an If expression in Java
Add watermark to Java to PDF document
I made an annotation in Java.
EXCEL file update sample with JAVA
Formatting an enum using formatter-maven-plugin (Java)
[Java] Add WordArt to Word document
Run an application made with Java8 with Java6
Java adds and reads Excel formulas
Run an external process in Java