Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.io.Closeable;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- public class excelSheet{
- public static void main(String[] args) throws IOException {
- writeFileUsingPOI();
- }
- public static void writeFileUsingPOI() throws IOException
- {
- //create blank workbook
- XSSFWorkbook workbook = new XSSFWorkbook();
- //Create a blank sheet
- XSSFSheet sheet = workbook.createSheet("excelOne");
- ArrayList<Object[]> data=new ArrayList<Object[]>();
- data.add(new String[]{"one","two","three"});
- data.add(new Object[]{"IN","wkwk",10000});
- data.add(new Object[]{"LN","wews",40000});
- data.add(new Object[]{"GM","aall",20000});
- data.add(new Object[]{"PR","sdoss",30000});
- //Iterate over data and write to sheet
- int rownum = 0;
- for (Object[] countries : data)
- {
- Row row = sheet.createRow(rownum++);
- int cellnum = 0;
- for (Object obj : countries)
- {
- Cell cell = row.createCell(cellnum++);
- if(obj instanceof String)
- cell.setCellValue((String)obj);
- else if(obj instanceof Double)
- cell.setCellValue((Double)obj);
- else if(obj instanceof Integer)
- cell.setCellValue((Integer)obj);
- }
- }
- try
- {
- //Write the workbook in file system
- FileOutputStream out = new FileOutputStream(new File("newfile.xlsx"));
- workbook.write(out);
- out.close();
- System.out.println("CountriesDetails.xlsx has been created successfully");
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally {
- ((Closeable) workbook).close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement