DEV Community

loading...

Create Excel and convert it to PDF by free Java API

eiceblue profile image E-iceblue Product Family ・2 min read

Introduction
I will introduce my whole solution in two parts. Firstly, I need to create Excel file on a server, and then convert this Excel file to pdf in Java applications.

Detailed steps for the first part: Create an Excel file using Spire.XLS for Java. Spire.XLS for Java is a feature rich API that supports creating, manipulating, converting and printing Excel files in Java applications.

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

public class CreateExcel {
    public static void main(String[] args) throws Exception {

            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Get the first worksheet
            Worksheet sheet = workbook.getWorksheets().get(0);
            //Set name for the first worksheet
            sheet.setName("Data Sheet");

            //Create a CellStyle for header cells
            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);

            //Create a CellStyle for data cells
            CellStyle style2 = workbook.getStyles().addStyle("Data Style");
            style2.getFont().setSize(10f);
            style2.getFont().setColor(Color.BLACK);

            //Add data and apply style for header cells
            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 and apply style for data cells
            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 the resultant file
            workbook.saveToFile("CreateExcel.xlsx", FileFormat.Version2013);
        }
    }

Sample Excel

The second part shows using Spire.Xls to convert excel to PDF. It only need three lines of code to convert the Excel file to PDF in Java applications.

import com.spire.xls.*;

public class ExceltoPDF {
    public static void main(String[] args) throws Exception {
        //Load the sample Excel file
        Workbook wb = new Workbook();
        wb.loadFromFile("CreateExcel.xlsx");

        //Save to PDF
        wb.saveToFile("ToPDF.pdf",FileFormat.PDF);
    }
}

Screenshot of the created pdf file:
Excel to PDF

Besides convert the whole excel workbook to PDF, Spire.XLS for Java also supports to convert the special worksheet to PDF.

import com.spire.xls.*;

public class ExceltoPDF {
    public static void main(String[] args) throws Exception {
        //Load the sample Excel file
        Workbook wb = new Workbook();
        wb.loadFromFile("CreateExcel.xlsx");

        //Get the second worksheet
        Worksheet sheet = wb.getWorksheets().get(1);

        //Save to PDF
        wb.saveToFile("ToPDF.pdf",FileFormat.PDF);
    }
}

Discussion (0)

pic
Editor guide