Java Write Excel File

Chapter: Apache POI Last Updated: 11-09-2016 18:27:40 UTC

Program:

            /* ............... START ............... */
                
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
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 JavaWriteExcelDemo {

	public static void main(String[] args) {

		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("Employee Data");

		Map<String, Object[]> data = new TreeMap<String, Object[]>();
		data.put("1", new Object[] { "ID", "NAME", "LASTNAME" });
		data.put("2", new Object[] { 1, "Amit", "Shukla" });
		data.put("3", new Object[] { 2, "Lokesh", "Gupta" });
		data.put("4", new Object[] { 3, "John", "Adwards" });
		data.put("5", new Object[] { 4, "Brian", "Schultz" });

		Set<String> keyset = data.keySet();
		int rownum = 0;
		for (String key : keyset) {
			Row row = sheet.createRow(rownum++);
			Object[] objArr = data.get(key);
			int cellnum = 0;
			for (Object obj : objArr) {
				Cell cell = row.createCell(cellnum++);
				if (obj instanceof String)
					cell.setCellValue((String) obj);
				else if (obj instanceof Integer)
					cell.setCellValue((Integer) obj);
			}
		}
		try {
			
			FileOutputStream out = new FileOutputStream(new File("JavaScanExcelDemo.xlsx"));
			workbook.write(out);
			out.close();
			System.out.println("JavaScanExcelDemo.xlsx written successfully on disk.");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
                /* ............... END ............... */
        

Tags

Write Excel File , Java, Apache POI

Similar Programs Chapter Last Updated
Java Add Image To Slide Apache POI 22-07-2017
Java Blank PPT Example Apache POI 22-07-2017
Java Apache POI Workbooks Apache POI 11-09-2016

1