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:
Recommended Posts