Code for Writing a xlsx file in java
Written by Ankur Goel
Here is a small code to write xlsx file though java code.
You may need to include some of the jar files like
- xmlbeans-2.3.0.jar
- dom4j-1.1.jar
- poi-ooxml-schemas-3.6-20091214.jar
- poi-ooxml-3.6-20091214.jar
- poi-3.6-20091214.jar
You can google it and download and include it along with the below code.
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
public class Start {
public void WriteData()
{
try{
Workbook[] wbs = new Workbook[] { new XSSFWorkbook() };
for(int i=0; i
{
Workbook wb = wbs[i];
Sheet s = wb.createSheet();
Row r1=s.createRow(0);
Cell c1=r1.createCell(0);
c1.setCellValue("helo");
c1=r1.createCell(1);
c1.setCellValue((double)1);
c1=r1.createCell(2);
c1.setCellValue((double)3);
c1=r1.createCell(3);
c1.setCellFormula("B1+C1");
String filename = "workbook.xlsx";
FileOutputStream out = new FileOutputStream(filename);
wb.write(out);
out.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Start Object=new Start();
Object.WriteData();
}
}
You can run the above code and a xlsx file is generated in the local directory. You will find some sample values you can then modify the code as required.
If you need any help then you can leave a comment or mail at This e-mail address is being protected from spambots. You need JavaScript enabled to view it
















Comments