Friday, 15 March 2019

Java: Apache POI : Writing to Excel File

package poitest;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WritingExcel {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


XSSFWorkbook wb=new XSSFWorkbook(); //new workbook 
XSSFSheet ws=wb.createSheet("dummy");  //Sheet Object

XSSFRow row0=ws.createRow(0); //Row Creation
XSSFRow row1=ws.createRow(1);
XSSFRow row2=ws.createRow(2);


XSSFCell cell0=row0.createCell(0); //Cell Access
XSSFCell cell1=row1.createCell(0);
XSSFCell cell2=row2.createCell(0);

cell0.setCellValue("0"); //Actual writing
cell1.setCellValue("1");
cell2.setCellValue("2");

FileOutputStream fio=new FileOutputStream("H:\\TstArea\\writewithpoi.xlsx");
               //File Creation for writing output

wb.write(fio); //Writing
wb.close(); //Close Workbook

}

}


//MAVEN APACHE POI DEPENDENCIES
<dependencies>
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.0.1</version>
</dependency>
 </dependencies>


No comments:

Post a Comment

Spring Boot : Exception Handler 14