{"id":926,"date":"2012-02-05T00:26:15","date_gmt":"2012-02-04T23:26:15","guid":{"rendered":"https:\/\/bob-team.de\/wordpress\/?p=926"},"modified":"2018-05-28T13:01:44","modified_gmt":"2018-05-28T11:01:44","slug":"apache-poi-xssf-und-hssf","status":"publish","type":"post","link":"https:\/\/bob-team.de\/wordpress\/2012\/02\/05\/apache-poi-xssf-und-hssf\/","title":{"rendered":"Apache POI: XSSF und HSSF"},"content":{"rendered":"<p><a href=\"http:\/\/poi.apache.org\/spreadsheet\/index.html\"><em>&#8222;HSSF is the POI Project&#8217;s pure Java implementation of the Excel &#8217;97(-2007) file format. XSSF is the POI Project&#8217;s pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.&#8220;<\/em> <\/a><!--more--><\/p>\n<p><strong>POI-XSSF<\/strong><\/p>\n<pre lang=\"java\">import java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.File;\r\nimport org.apache.poi.hssf.util.HSSFColor.LAVENDER;\r\nimport org.apache.poi.xssf.usermodel.XSSFCell;\r\nimport org.apache.poi.xssf.usermodel.XSSFCellStyle;\r\nimport org.apache.poi.xssf.usermodel.XSSFFont;\r\nimport org.apache.poi.xssf.usermodel.XSSFRichTextString;\r\nimport org.apache.poi.xssf.usermodel.XSSFRow;\r\nimport org.apache.poi.xssf.usermodel.XSSFSheet;\r\nimport org.apache.poi.xssf.usermodel.XSSFWorkbook;\r\npublic class XSSFExcelGenerator {\r\n\tprotected void buildExcelDocument(String fileName) {\r\n\t\tFileOutputStream fileOutputStream = null;\r\n\t\ttry {\r\n\t\t\tfileOutputStream = new FileOutputStream(fileName);\r\n\t\t\t\/\/ Create Sheet.\r\n\t\t\tXSSFWorkbook xssfWorkbook = new XSSFWorkbook();\r\n\t\t\tXSSFSheet sheet = xssfWorkbook.createSheet(\"SampleExcelSheet\");\r\n\t\t\t\/\/ Font setting for sheet.\r\n\t\t\tXSSFFont font = xssfWorkbook.createFont();\r\n\t\t\tfont.setBoldweight((short) 700);\r\n\t\t\tsheet.setDefaultColumnWidth(30);\r\n\t\t\t\/\/ Create Styles for sheet.\r\n\t\t\tXSSFCellStyle headerStyle = xssfWorkbook.createCellStyle();\r\n\t\t\theaderStyle.setFillForegroundColor(LAVENDER.index);\r\n\t\t\theaderStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);\r\n\t\t\theaderStyle.setFont(font);\r\n\t\t\tXSSFCellStyle dataStyle = xssfWorkbook.createCellStyle();\r\n\t\t\tdataStyle.setWrapText(true);\r\n\t\t\t\/\/ Create Header Row\r\n\t\t\tXSSFRow headerRow = sheet.createRow(0);\r\n\t\t\t\/\/ Write row for header\r\n\t\t\tXSSFCell headerCell1 = headerRow.createCell(0);\r\n\t\t\theaderCell1.setCellStyle(headerStyle);\r\n\t\t\theaderCell1.setCellValue(\"Employee Name\");\r\n\t\t\tXSSFCell headerCell2 = headerRow.createCell(1);\r\n\t\t\theaderCell2.setCellStyle(headerStyle);\r\n\t\t\theaderCell2.setCellValue(\"Designation\");\r\n\t\t\tXSSFCell headerCell3 = headerRow.createCell(2);\r\n\t\t\theaderCell3.setCellStyle(headerStyle);\r\n\t\t\theaderCell3.setCellValue(\"Country\");\r\n\t\t\t\/\/ Create First Data Row\r\n\t\t\tXSSFRow dataRow = sheet.createRow(1);\r\n\t\t\t\/\/ Write data in data row\r\n\t\t\tXSSFCell cell1 = dataRow.createCell(0);\r\n\t\t\tcell1.setCellStyle(dataStyle);\r\n\t\t\tcell1.setCellValue(new XSSFRichTextString(\"Sandeep\"));\r\n\t\t\tXSSFCell cell2 = dataRow.createCell(1);\r\n\t\t\tcell2.setCellStyle(dataStyle);\r\n\t\t\tcell2.setCellValue(new XSSFRichTextString(\"Software Engineer\"));\r\n\t\t\tXSSFCell cell3 = dataRow.createCell(2);\r\n\t\t\tcell3.setCellStyle(dataStyle);\r\n\t\t\tcell3.setCellValue(new XSSFRichTextString(\"India\"));\r\n\t\t\t\/\/ write in excel\r\n\t\t\txssfWorkbook.write(fileOutputStream);\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t} finally {\r\n\t\t\ttry {\r\n\t\t\t\tif (fileOutputStream != null) {\r\n\t\t\t\t\tfileOutputStream.close();\r\n\t\t\t\t}\r\n\t\t\t} catch (IOException ex) {\r\n\t\t\t\tex.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tnew XSSFExcelGenerator().buildExcelDocument(\"C:\/temp\/excel.xlsx\");\r\n\t}\r\n}<\/pre>\n<p><strong>POI-HSSF<\/strong><\/p>\n<pre lang=\"java\">import java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.File;\r\nimport org.apache.poi.hssf.util.HSSFColor.LAVENDER;\r\nimport org.apache.poi.hssf.usermodel.HSSFCell;\r\nimport org.apache.poi.hssf.usermodel.HSSFCellStyle;\r\nimport org.apache.poi.hssf.usermodel.HSSFFont;\r\nimport org.apache.poi.hssf.usermodel.HSSFRichTextString;\r\nimport org.apache.poi.hssf.usermodel.HSSFRow;\r\nimport org.apache.poi.hssf.usermodel.HSSFSheet;\r\nimport org.apache.poi.hssf.usermodel.HSSFWorkbook;\r\npublic class HSSFExcelGenerator {\r\n\tprotected void buildExcelDocument(String fileName) {\r\n\t\tFileOutputStream fileOutputStream = null;\r\n\t\ttry {\r\n\t\t\tfileOutputStream = new FileOutputStream(fileName);\r\n\t\t\t\/\/ Create Sheet.\r\n\t\t\tHSSFWorkbook hssfWorkbook = new HSSFWorkbook();\r\n\t\t\tHSSFSheet sheet = hssfWorkbook.createSheet(\"SampleExcelSheet\");\r\n\t\t\t\/\/ Font setting for sheet.\r\n\t\t\tHSSFFont font = hssfWorkbook.createFont();\r\n\t\t\tfont.setBoldweight((short) 700);\r\n\t\t\tsheet.setDefaultColumnWidth(30);\r\n\t\t\t\/\/ Create Styles for sheet.\r\n\t\t\tHSSFCellStyle headerStyle = hssfWorkbook.createCellStyle();\r\n\t\t\theaderStyle.setFillForegroundColor(LAVENDER.index);\r\n\t\t\theaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);\r\n\t\t\theaderStyle.setFont(font);\r\n\t\t\tHSSFCellStyle dataStyle = hssfWorkbook.createCellStyle();\r\n\t\t\tdataStyle.setWrapText(true);\r\n\t\t\t\/\/ Create Header Row\r\n\t\t\tHSSFRow headerRow = sheet.createRow(0);\r\n\t\t\t\/\/ Write row for header\r\n\t\t\tHSSFCell headerCell1 = headerRow.createCell(0);\r\n\t\t\theaderCell1.setCellStyle(headerStyle);\r\n\t\t\theaderCell1.setCellValue(\"Employee Name\");\r\n\t\t\tHSSFCell headerCell2 = headerRow.createCell(1);\r\n\t\t\theaderCell2.setCellStyle(headerStyle);\r\n\t\t\theaderCell2.setCellValue(\"Designation\");\r\n\t\t\tHSSFCell headerCell3 = headerRow.createCell(2);\r\n\t\t\theaderCell3.setCellStyle(headerStyle);\r\n\t\t\theaderCell3.setCellValue(\"Country\");\r\n\t\t\t\/\/ Create First Data Row\r\n\t\t\tHSSFRow dataRow = sheet.createRow(1);\r\n\t\t\t\/\/ Write data in data row\r\n\t\t\tHSSFCell cell1 = dataRow.createCell(0);\r\n\t\t\tcell1.setCellStyle(dataStyle);\r\n\t\t\tcell1.setCellValue(new HSSFRichTextString(\"Sandeep\"));\r\n\t\t\tHSSFCell cell2 = dataRow.createCell(1);\r\n\t\t\tcell2.setCellStyle(dataStyle);\r\n\t\t\tcell2.setCellValue(new HSSFRichTextString(\"Software Engineer\"));\r\n\t\t\tHSSFCell cell3 = dataRow.createCell(2);\r\n\t\t\tcell3.setCellStyle(dataStyle);\r\n\t\t\tcell3.setCellValue(new HSSFRichTextString(\"India\"));\r\n\t\t\t\/\/ write in excel\r\n\t\t\thssfWorkbook.write(fileOutputStream);\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t} finally {\r\n\t\t\ttry {\r\n\t\t\t\tif (fileOutputStream != null) {\r\n\t\t\t\t\tfileOutputStream.close();\r\n\t\t\t\t}\r\n\t\t\t} catch (IOException ex) {\r\n\t\t\t\tex.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tnew HSSFExcelGenerator().buildExcelDocument(\"C:\/temp\/excel.xls\");\r\n\t}\r\n}<\/pre>\n<p>siehe auch: <a href=\"https:\/\/dzone.com\/articles\/introduction-to-apache-poi-library\">Introduction to the Apache POI Library<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8222;HSSF is the POI Project&#8217;s pure Java implementation of the Excel &#8217;97(-2007) file format. XSSF is the POI Project&#8217;s pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.&#8220;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-926","post","type-post","status-publish","format-standard","hentry","category-java","entry"],"_links":{"self":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/posts\/926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/comments?post=926"}],"version-history":[{"count":7,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/posts\/926\/revisions"}],"predecessor-version":[{"id":2328,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/posts\/926\/revisions\/2328"}],"wp:attachment":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/media?parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}