{"id":398,"date":"2015-05-16T10:27:08","date_gmt":"2015-05-16T09:27:08","guid":{"rendered":"http:\/\/www.pixnbgames.com\/blog\/?p=398"},"modified":"2015-08-21T23:51:55","modified_gmt":"2015-08-21T22:51:55","slug":"how-to-ms-excel-files-from-java-with-apache-poi","status":"publish","type":"post","link":"https:\/\/www.pixnbgames.com\/blog\/java\/how-to-ms-excel-files-from-java-with-apache-poi\/","title":{"rendered":"How to generate MS Excel files from Java with Apache POI"},"content":{"rendered":"<p style=\"text-align: justify;\">Commonly it&#8217;s necessary to manage Microsoft Excel files from Java applications. It can be usefull, for example, to export \/ import data, report generation, etc. <a title=\"Apache POI\" href=\"https:\/\/poi.apache.org\" target=\"_blank\">Apache POI<\/a> is a library from Apache fundation, which has become a de facto standard for this type of tasks. In this article we will see an example of how to generate a simple excel file with Apache POI.<\/p>\n<h2 style=\"text-align: justify;\">Add project dependency<\/h2>\n<p style=\"text-align: justify;\">Using a Maven project, we can add the POI dependency like this:<\/p>\n<pre style=\"padding-left: 30px;\" lang=\"xml\">&lt;dependencies&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.apache.poi&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;poi&lt;\/artifactId&gt;\r\n        &lt;version&gt;3.12-beta1&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;<\/pre>\n<p style=\"text-align: justify;\">If we are not using Maven, we have to download the library from <a title=\"Apache POI Download\" href=\"https:\/\/poi.apache.org\/download.html\">here<\/a> and add it manually to our project.<\/p>\n<h2 style=\"text-align: justify;\">Excel Format<\/h2>\n<p><a title=\"Apache POI Spread Sheet\" href=\"https:\/\/poi.apache.org\/spreadsheet\/index.html\">As we can see on its website<\/a>, apache POI give us two APIs:<\/p>\n<ul>\n<li><strong>HSSF:<\/strong>\u00a0it allows to generate files compatible with MS Office 97<\/li>\n<li><strong>XSSF:<\/strong>\u00a0it allows to generate files compatible with MS Office\u00a02007<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">In this case, to generate files with maximum compatibility, we use the API HSSF.<\/p>\n<h2>Generating the\u00a0Excel book<\/h2>\n<p>The way to work with Apache POI will be always the same:<\/p>\n<ol>\n<li>Create a workbook<\/li>\n<li>Create sheets in the workbook<\/li>\n<li>Create rows in the sheets<\/li>\n<li>Create cells in the rows<\/li>\n<li>Set content for cells<\/li>\n<\/ol>\n<p>Let&#8217;s see how to do that in Java code:<\/p>\n<p><strong>1. Create the workbook:<\/strong><\/p>\n<pre lang=\"Java\">HSSFWorkbook workbook = new HSSFWorkbook();<\/pre>\n<p><strong><br \/>\n2. Create a sheet:<\/strong><\/p>\n<pre lang=\"Java\">HSSFSheet sheet = workbook.createSheet(\"My Sheet\");<\/pre>\n<p><strong><br \/>\n3. Create a row:<\/strong><\/p>\n<pre lang=\"Java\">HSSFRow row = sheet.createRow(0);<\/pre>\n<p><strong><br \/>\n4. Create a cell:<\/strong><\/p>\n<pre lang=\"Java\">HSSFCell cell = row.createCell(0);<\/pre>\n<p><strong><br \/>\n5. Set cell&#8217;s content:<\/strong><\/p>\n<pre lang=\"Java\">cell.setCellValue(\"Hello, World!\");<\/pre>\n<p>&nbsp;<\/p>\n<h2>Save the generated Excel<\/h2>\n<p style=\"text-align: justify;\">We can save the generated excel in a file by passing to it the output stream:<\/p>\n<pre lang=\"Java\">OutputStream out = new FileOutputStream(\"src\/main\/resources\/SimpleExcel.xls\");\r\nworkbook.write(out);<\/pre>\n<p>&nbsp;<\/p>\n<h2>Source code<\/h2>\n<p style=\"text-align: justify;\">You can download the source code used in this article from <a href=\"https:\/\/github.com\/angelnavarro\/Apache-POI-Examples\" target=\"_blank\">my GitHub repository<\/a>, project <em>Poi-SimpleExcelGenerator<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<h2>More information<\/h2>\n<p style=\"text-align: justify;\">Apache POI allows to set styles, formulas, formats, etc. to the generated content. I will cover this topics on later articles. Meanwhile, you can get all information from the <a href=\"https:\/\/poi.apache.org\/spreadsheet\/examples.html\" target=\"_blank\">official documentation<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Commonly it&#8217;s necessary to manage Microsoft Excel files from Java applications. It can be usefull, for example, to export \/ import data, report generation, etc. Apache POI is a library from Apache fundation, which has become a de facto standard <a class=\"more-link\" href=\"https:\/\/www.pixnbgames.com\/blog\/java\/how-to-ms-excel-files-from-java-with-apache-poi\/\">Seguir leyendo <span class=\"screen-reader-text\">  How to generate MS Excel files from Java with Apache POI<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38,37,36],"tags":[40,41,39],"language":[4],"_links":{"self":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/398"}],"collection":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":5,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":403,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions\/403"}],"wp:attachment":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/tags?post=398"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/language?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}