Java Library for Processing Spreadsheet Documents
Free Java API that enables software programmers to create and modify Microsoft Excel files using Java code.
What is GcExcel-Java?
GcExcel-Java is an open source high-speed Java Excel spreadsheet processing API that enables software programmers to create and modify Excel files using Java commands. The feature-rich API is very easy to use and can be smoothly used on popular operating systems like Windows, MAC, and Linux. The library supports range operations on cells, rows, or columns in Excel documents with ease.
You can easily create your own custom functions and use them. The library also fully supports PDF generation from Excel templates supporting features like sparklines and tables. You can also easily export the workbook/worksheet/range to HTML. The great thing is that you can easily import and export Excel template files on the server-side.
The GcExcel-Java library has included support for several important features related to Spreadsheet creation and management, such as reading existing Excel files, importing spreadsheets, filtering support, calculating data, querying, converting spreadsheets to PDF, adding sorting, conditional formatting, and data validation, insert charts, adding shapes or pictures, adding comments, inserting hyperlinks, themes and much more.
Getting Started with GcExcel-Java
The recommended way to install GcExcel-Java into your project is by using GitHub. Please use the following command for smooth installation
Install GcExcel-Java via GitHub
$ git clone https://github.com/GrapeCity/GcExcel-Java.git
Convert Excel file to PDF
The open source GcExcel-Java library lets software developers convert an Excel file to PDF documents inside their own apps. The library has provided several important features for exporting Excel files such as saving Worksheet to PDF, exporting the outline column of the worksheet to PDF files, Excel borders to PDF, exporting spreadsheets with tables to PDF, and many more.
Export Excel File to PDF via Java API
// Create a new workbook and add worksheets
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);
IWorksheet worksheet1 = workbook.getWorksheets().add();
// Set value and apply styles to the worksheet
worksheet1.getRange("A1").setValue("Sheet1");
worksheet1.getRange("A1").getFont().setName("Wide Latin");
worksheet1.getRange("A1").getFont().setColor(Color.GetRed());
worksheet1.getRange("A1").getInterior().setColor(Color.GetGreen());
// Export Workbook to pdf file, the exported file has two pages.
workbook.save("ConvertWorkbookToPDF.pdf", SaveFileFormat.Pdf);
// Just export a particular worksheet to pdf file
worksheet1.save("ConvertWorksheetToPDF.pdf", SaveFileFormat.Pdf)
Create a New Workbook via Java
The GcExcel-Java library allows software programmers to create a new workbook from scratch with just a couple of lines of Java code inside their apps. It provides all the necessary properties and methods required to create a workbook. After the creation of the spreadsheet, you can easily modify it and can perform complex operations on the data residing in the spreadsheets with ease.
Add Multiple Worksheets via Java API
// Add a worksheet to the workbook.
IWorksheet worksheet1 = workbook.getWorksheets().add();
// Add a new worksheet before worksheet1 and reset its name
IWorksheet worksheet2 = workbook.getWorksheets().addBefore(worksheet1);
worksheet2.setName("MySheet2");
// Add a sheet after worksheet2
workbook.getWorksheets().addAfter(workbook.getWorksheets().get(1));
Create and PivotTable in Spreadsheets
The free library GcExcel-Java has provided functionality for creating and managing Pivot Table using Java commands. Pivot Table is very useful for data summarization and can be used to automatically count, and calculate the total or average of the data stored in a spreadsheet. The library has provided support for several important features such as displaying grand total, modifying pivot fields, setting conditional format, sorting, counting, setting number or text format, and many more.
Create PivotTable in Spreadsheet via Java API
// Source data for PivotCache
Object sourceData = new Object[][]
{
{ "Order ID", "Product", "Category", "Amount", "Date", "Country" },
{ 1, "Carrots", "Vegetables", 4270, new GregorianCalendar(2018, 0, 6),"United States" },
{ 2, "Broccoli", "Vegetables", 8239, new GregorianCalendar(2018, 0, 7),"United Kingdom" },
{ 3, "Banana", "Fruit", 617, new GregorianCalendar(2018, 0, 8),"United States" },
{ 4, "Banana", "Fruit", 8384, new GregorianCalendar(2018, 0, 10),"Canada" },
{ 5, "Beans", "Vegetables", 2626, new GregorianCalendar(2018, 0, 10),"Germany" },
{ 6, "Orange", "Fruit", 3610, new GregorianCalendar(2018, 0, 11),"United States" },
{ 7, "Broccoli", "Vegetables", 9062, new GregorianCalendar(2018, 0, 11),"Australia" },
{ 8, "Banana", "Fruit", 6906, new GregorianCalendar(2018, 0, 16),"New Zealand" },
{ 9, "Apple", "Fruit", 2417, new GregorianCalendar(2018, 0, 16),"France" },
{ 10, "Apple", "Fruit", 7431, new GregorianCalendar(2018, 0, 16),"Canada" },
{ 11, "Banana", "Fruit", 8250, new GregorianCalendar(2018, 0, 16),"Germany" },
{ 12, "Broccoli", "Vegetables", 7012, new GregorianCalendar(2018, 0, 18),"United States" },
{ 13, "Carrots", "Vegetables", 1903, new GregorianCalendar(2018, 0, 20),"Germany" },
{ 14, "Broccoli", "Vegetables", 2824, new GregorianCalendar(2018, 0, 22),"Canada" },
{ 15, "Apple", "Fruit", 6946, new GregorianCalendar(2018, 0, 24),"France" },
};
// Initialize the workBook and fetch the default workSheet
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);
// Assigning data to the range
worksheet.getRange("A1:F16").setValue(sourceData);
worksheet.getRange("A:F").setColumnWidth(15);
// Creating pivot
IPivotCache pivotcache = workbook.getPivotCaches().create(worksheet.getRange("A1:F16"));
IPivotTable pivottable = worksheet.getPivotTables().add(pivotcache,
worksheet.getRange("H7"), "pivottable1");
worksheet.getRange("D2:D16").setNumberFormat("$#,##0.00");
worksheet.getRange("I9:O11").setNumberFormat("$#,##0.00");
worksheet.getRange("H:O").setColumnWidth(12);
Add & Modify Charts to Excel Workbook
The GcExcel-Java has included support for adding and modifying charts inside an Excel Workbook using Java commands. It has provided several functions and methods related to chart management and creation, such as adding data series, customizing chart titles and legends, accessing and customizing a group series, adding or removing data labels, customization of value axes, and category axes with ease.
Create Charts to Worksheets via Java API
// Add Chart
IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 300, 10, 300, 300);
worksheet.getRange("A1:D6").setValue(
new Object[][] { { null, "S1", "S2", "S3" }, { "Item1", 10, 25, 25 }, { "Item2", -51, -36, 27 },
{ "Item3", 52, -85, -30 }, { "Item4", 22, 65, 65 }, { "Item5", 23, 69, 69 } });
// Create Chart
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);