Apache POI: XSSF und HSSF

  • Beitrags-Autor:
  • Beitrags-Kategorie:Java
  • Beitrags-Kommentare:0 Kommentare

„HSSF is the POI Project’s pure Java implementation of the Excel ’97(-2007) file format. XSSF is the POI Project’s pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.“

POI-XSSF

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import org.apache.poi.hssf.util.HSSFColor.LAVENDER;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XSSFExcelGenerator {
	protected void buildExcelDocument(String fileName) {
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(fileName);
			// Create Sheet.
			XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
			XSSFSheet sheet = xssfWorkbook.createSheet("SampleExcelSheet");
			// Font setting for sheet.
			XSSFFont font = xssfWorkbook.createFont();
			font.setBoldweight((short) 700);
			sheet.setDefaultColumnWidth(30);
			// Create Styles for sheet.
			XSSFCellStyle headerStyle = xssfWorkbook.createCellStyle();
			headerStyle.setFillForegroundColor(LAVENDER.index);
			headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
			headerStyle.setFont(font);
			XSSFCellStyle dataStyle = xssfWorkbook.createCellStyle();
			dataStyle.setWrapText(true);
			// Create Header Row
			XSSFRow headerRow = sheet.createRow(0);
			// Write row for header
			XSSFCell headerCell1 = headerRow.createCell(0);
			headerCell1.setCellStyle(headerStyle);
			headerCell1.setCellValue("Employee Name");
			XSSFCell headerCell2 = headerRow.createCell(1);
			headerCell2.setCellStyle(headerStyle);
			headerCell2.setCellValue("Designation");
			XSSFCell headerCell3 = headerRow.createCell(2);
			headerCell3.setCellStyle(headerStyle);
			headerCell3.setCellValue("Country");
			// Create First Data Row
			XSSFRow dataRow = sheet.createRow(1);
			// Write data in data row
			XSSFCell cell1 = dataRow.createCell(0);
			cell1.setCellStyle(dataStyle);
			cell1.setCellValue(new XSSFRichTextString("Sandeep"));
			XSSFCell cell2 = dataRow.createCell(1);
			cell2.setCellStyle(dataStyle);
			cell2.setCellValue(new XSSFRichTextString("Software Engineer"));
			XSSFCell cell3 = dataRow.createCell(2);
			cell3.setCellStyle(dataStyle);
			cell3.setCellValue(new XSSFRichTextString("India"));
			// write in excel
			xssfWorkbook.write(fileOutputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		new XSSFExcelGenerator().buildExcelDocument("C:/temp/excel.xlsx");
	}
}

POI-HSSF

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import org.apache.poi.hssf.util.HSSFColor.LAVENDER;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class HSSFExcelGenerator {
	protected void buildExcelDocument(String fileName) {
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(fileName);
			// Create Sheet.
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
			HSSFSheet sheet = hssfWorkbook.createSheet("SampleExcelSheet");
			// Font setting for sheet.
			HSSFFont font = hssfWorkbook.createFont();
			font.setBoldweight((short) 700);
			sheet.setDefaultColumnWidth(30);
			// Create Styles for sheet.
			HSSFCellStyle headerStyle = hssfWorkbook.createCellStyle();
			headerStyle.setFillForegroundColor(LAVENDER.index);
			headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			headerStyle.setFont(font);
			HSSFCellStyle dataStyle = hssfWorkbook.createCellStyle();
			dataStyle.setWrapText(true);
			// Create Header Row
			HSSFRow headerRow = sheet.createRow(0);
			// Write row for header
			HSSFCell headerCell1 = headerRow.createCell(0);
			headerCell1.setCellStyle(headerStyle);
			headerCell1.setCellValue("Employee Name");
			HSSFCell headerCell2 = headerRow.createCell(1);
			headerCell2.setCellStyle(headerStyle);
			headerCell2.setCellValue("Designation");
			HSSFCell headerCell3 = headerRow.createCell(2);
			headerCell3.setCellStyle(headerStyle);
			headerCell3.setCellValue("Country");
			// Create First Data Row
			HSSFRow dataRow = sheet.createRow(1);
			// Write data in data row
			HSSFCell cell1 = dataRow.createCell(0);
			cell1.setCellStyle(dataStyle);
			cell1.setCellValue(new HSSFRichTextString("Sandeep"));
			HSSFCell cell2 = dataRow.createCell(1);
			cell2.setCellStyle(dataStyle);
			cell2.setCellValue(new HSSFRichTextString("Software Engineer"));
			HSSFCell cell3 = dataRow.createCell(2);
			cell3.setCellStyle(dataStyle);
			cell3.setCellValue(new HSSFRichTextString("India"));
			// write in excel
			hssfWorkbook.write(fileOutputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		new HSSFExcelGenerator().buildExcelDocument("C:/temp/excel.xls");
	}
}

siehe auch: Introduction to the Apache POI Library

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.