Create an excel file in java by apache poi easy and it is a very powerful API for excel creation.
Step-1: Create a maven project (ExcelGenerator) and Import apache poi dependency in pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
Step-2: Create a class to generate an excel file
package com.demo.client;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelGenerator
{
public static void main( String[] args )
throws FileNotFoundException,
IOException
{
//create new workbook (you can say the excel file)
XSSFWorkbook wb = new XSSFWorkbook();
//create new sheet in excel file with name 'First sheet'
Sheet sheet = wb.createSheet( "First Sheet" );
//create new row in sheet with row number (row start from 0)
XSSFRow row = sheet.createRow( 1 );
// create dummy string array to set in excel sheet row
String[] str = "Component,Category,S. No., Activity Name,Unit of Measure,Current Status".split( "," );
for ( int i = 0; i < str.length; i++ )
{
//create cell in the existng row of the sheet you can asume as column of the row (column start from 0 )
Cell cell = row.createCell( i );
//set the data in the perticular cell
cell.setCellValue( str[i] );
}
//its your local dir path of your syste with file name which you want to create (demo.xlsx)
String filePath = "/home/manjuman/Documents/demo.xlsx";
try (OutputStream fileOut = new FileOutputStream( file ))
{
//generate your excel file
wb.write( fileOut );
System.out.println( "Excel generated" );
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
}
finaly
{
//close your workbook
wb.close();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelGenerator
{
public static void main( String[] args )
throws FileNotFoundException,
IOException
{
//create new workbook (you can say the excel file)
XSSFWorkbook wb = new XSSFWorkbook();
//create new sheet in excel file with name 'First sheet'
Sheet sheet = wb.createSheet( "First Sheet" );
//create new row in sheet with row number (row start from 0)
XSSFRow row = sheet.createRow( 1 );
// create dummy string array to set in excel sheet row
String[] str = "Component,Category,S. No., Activity Name,Unit of Measure,Current Status".split( "," );
for ( int i = 0; i < str.length; i++ )
{
//create cell in the existng row of the sheet you can asume as column of the row (column start from 0 )
Cell cell = row.createCell( i );
//set the data in the perticular cell
cell.setCellValue( str[i] );
}
//its your local dir path of your syste with file name which you want to create (demo.xlsx)
String filePath = "/home/manjuman/Documents/demo.xlsx";
try (OutputStream fileOut = new FileOutputStream( file ))
{
//generate your excel file
wb.write( fileOut );
System.out.println( "Excel generated" );
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
}
finaly
{
//close your workbook
wb.close();
}
}
}
Output file:
demo.xlsx with one sheet (First sheet ) |
Comments
Post a Comment