{"id":335,"date":"2017-06-22T11:38:56","date_gmt":"2017-06-22T11:38:56","guid":{"rendered":"http:\/\/www.codeinsightacademy.com\/blog\/?p=335"},"modified":"2017-06-22T11:44:30","modified_gmt":"2017-06-22T11:44:30","slug":"apache-poi-poor-obfuscation-implementation-file-system","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/java\/apache-poi-poor-obfuscation-implementation-file-system\/","title":{"rendered":"Apache POI (Poor Obfuscation Implementation File System)"},"content":{"rendered":"<p>Prerequisite<br \/>\nDownload all poi jar files<br \/>\n<a href=\"https:\/\/poi.apache.org\/download.html\">https:\/\/poi.apache.org\/download.html<\/a><\/p>\n<hr \/>\n<ol>\n<li>\u00a0Terms\n<ul>\n<li>Workbook &#8211; The file contains number of sheets<\/li>\n<li>Sheet &#8211; Matrix contains rows and cell<\/li>\n<li>Row &#8211; Record of an entity in one row<\/li>\n<li>Cell &#8211; A single cell which hold value<\/li>\n<\/ul>\n<\/li>\n<li>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-title=\"Read Data using POI (xls)\" data-enlighter-group=\"Read Data using POI (xls)\">JFileChooser fileChooser = new JFileChooser();\r\n        int returnValue = fileChooser.showDialog(null, \"Select File\");\r\n        \r\n        if(returnValue == JFileChooser.APPROVE_OPTION) {\r\n            Workbook workbook = new HSSFWorkbook(new FileInputStream(fileChooser.getSelectedFile()));\r\n            \r\n            Sheet sheet = workbook.getSheetAt(0);\r\n            \r\n            for(Iterator&lt;Row&gt; rit = sheet.rowIterator(); rit.hasNext();){\r\n                for(Iterator&lt;Cell&gt; cit = rit.next().cellIterator(); cit.hasNext();){\r\n                    System.out.println(cit.next());\r\n                }\r\n                System.out.println(\"\");\r\n            }\r\n        } else {\r\n            System.out.println(\"Invalid output\");\r\n        }<\/pre>\n<\/li>\n<li>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-group=\"Read Data from Excel (xlsx)\" data-enlighter-title=\"Read Data from Excel (xlsx)\">JFileChooser fileChooser = new JFileChooser();\r\n        int returnValue = fileChooser.showDialog(null, \"Select File\");\r\n        \r\n        if(returnValue == JFileChooser.APPROVE_OPTION) {\r\n            FileInputStream excelFile = new FileInputStream(fileChooser.getSelectedFile());\r\n            Workbook workbook = new XSSFWorkbook(excelFile);\r\n            \r\n            Sheet sheet = workbook.getSheetAt(0);\r\n            \r\n            for(Iterator&lt;Row&gt; rit = sheet.rowIterator(); rit.hasNext();) {\r\n                for(Iterator&lt;Cell&gt; cit = rit.next().cellIterator(); cit.hasNext();) {\r\n                    System.out.print(cit.next() + \"\\t\" + cit.next());\r\n                }\r\n                System.out.println(\"\");\r\n            }\r\n        }<\/pre>\n<\/li>\n<li>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-group=\"Write into Excel File\" data-enlighter-title=\"Write into Excel File\"> Workbook workbook = new HSSFWorkbook();\r\n        \r\n        Sheet sheet1 = workbook.createSheet(\"movies\");\r\n\/\/        Sheet sheet2 = workbook.createSheet(\"Test Cases\");\r\n\/\/        Sheet sheet3 = workbook.createSheet(WorkbookUtil.createSafeSheetName(\"$*(^&amp;?\"));\r\n        \r\n        Row row = sheet1.createRow(0);\r\n        \r\n\/\/        Cell cell = row.createCell(4);\r\n\/\/        cell.setCellValue(\"Hello World\");\r\n\/\/        Cell cell2 = row.createCell(3);\r\n\/\/        cell2.setCellValue(\"terminator\");\r\n\/\/        System.out.println(cell.getRichStringCellValue().toString());\r\n\/\/        System.out.println(cell2.getRichStringCellValue().toString());\r\n\r\n        Cell cell1 = row.createCell(0);\r\n        Cell cell2 = row.createCell(1);\r\n        Cell cell3 = row.createCell(2);\r\n        Cell cell4 = row.createCell(3);\r\n        Cell cell5 = row.createCell(4);\r\n        \r\n        cell1.setCellValue(5);\r\n        cell2.setCellValue(\"+\");\r\n        cell3.setCellValue(6);\r\n        cell4.setCellValue(\"=\");\r\n        cell5.setCellFormula(\"A1+C1\");\r\n        \r\n        try {\r\n            FileOutputStream outputStream = new FileOutputStream(\"test1.xlsx\");\r\n            workbook.write(outputStream);\r\n            outputStream.close();\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }<\/pre>\n<\/li>\n<li>\u00a0Complete Program\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-group=\"ApachePOIDemo.java\" data-enlighter-title=\"ApachePOIDemo.java\">import java.io.FileInputStream;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.util.Iterator;\r\nimport javax.swing.JFileChooser;\r\nimport org.apache.poi.hssf.usermodel.HSSFWorkbook;\r\nimport org.apache.poi.ss.usermodel.Workbook;\r\nimport org.apache.poi.ss.usermodel.Sheet;\r\nimport org.apache.poi.ss.util.WorkbookUtil;\r\nimport org.apache.poi.ss.usermodel.Row;\r\nimport org.apache.poi.ss.usermodel.Cell;\r\nimport org.apache.poi.xssf.usermodel.XSSFWorkbook;\r\n\r\n\/**\r\n *\r\n * @author Shailesh Sonare\r\n *\/\r\npublic class ApachePOIDemo {\r\n\r\n    \/**\r\n     * @param args the command line arguments\r\n     *\/\r\n    public static void main(String[] shailesh) throws IOException {\r\n        \r\n        \/*\r\n        \r\n        Workbook workbook = new HSSFWorkbook();\r\n        \r\n        Sheet sheet1 = workbook.createSheet(\"movies\");\r\n\/\/        Sheet sheet2 = workbook.createSheet(\"Test Cases\");\r\n\/\/        Sheet sheet3 = workbook.createSheet(WorkbookUtil.createSafeSheetName(\"$*(^&amp;?\"));\r\n        \r\n        Row row = sheet1.createRow(0);\r\n        \r\n\/\/        Cell cell = row.createCell(4);\r\n\/\/        cell.setCellValue(\"Hello World\");\r\n\/\/        Cell cell2 = row.createCell(3);\r\n\/\/        cell2.setCellValue(\"terminator\");\r\n\/\/        System.out.println(cell.getRichStringCellValue().toString());\r\n\/\/        System.out.println(cell2.getRichStringCellValue().toString());\r\n\r\n        Cell cell1 = row.createCell(0);\r\n        Cell cell2 = row.createCell(1);\r\n        Cell cell3 = row.createCell(2);\r\n        Cell cell4 = row.createCell(3);\r\n        Cell cell5 = row.createCell(4);\r\n        \r\n        cell1.setCellValue(5);\r\n        cell2.setCellValue(\"+\");\r\n        cell3.setCellValue(6);\r\n        cell4.setCellValue(\"=\");\r\n        cell5.setCellFormula(\"A1+C1\");\r\n        \r\n        try {\r\n            FileOutputStream outputStream = new FileOutputStream(\"test1.xlsx\");\r\n            workbook.write(outputStream);\r\n            outputStream.close();\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n        *\/\r\n        \/*\r\n        JFileChooser fileChooser = new JFileChooser();\r\n        int returnValue = fileChooser.showDialog(null, \"Select File\");\r\n        \r\n        if(returnValue == JFileChooser.APPROVE_OPTION) {\r\n            Workbook workbook = new HSSFWorkbook(new FileInputStream(fileChooser.getSelectedFile()));\r\n            \r\n            Sheet sheet = workbook.getSheetAt(0);\r\n            \r\n            for(Iterator&lt;Row&gt; rit = sheet.rowIterator(); rit.hasNext();){\r\n                for(Iterator&lt;Cell&gt; cit = rit.next().cellIterator(); cit.hasNext();){\r\n                    System.out.println(cit.next());\r\n                }\r\n                System.out.println(\"\");\r\n            }\r\n        } else {\r\n            System.out.println(\"Invalid output\");\r\n        }\r\n        *\/\r\n        \r\n        JFileChooser fileChooser = new JFileChooser();\r\n        int returnValue = fileChooser.showDialog(null, \"Select File\");\r\n        \r\n        if(returnValue == JFileChooser.APPROVE_OPTION) {\r\n            FileInputStream excelFile = new FileInputStream(fileChooser.getSelectedFile());\r\n            Workbook workbook = new XSSFWorkbook(excelFile);\r\n            \r\n            Sheet sheet = workbook.getSheetAt(0);\r\n            \r\n            for(Iterator&lt;Row&gt; rit = sheet.rowIterator(); rit.hasNext();) {\r\n                for(Iterator&lt;Cell&gt; cit = rit.next().cellIterator(); cit.hasNext();) {\r\n                    System.out.print(cit.next() + \"\\t\" + cit.next());\r\n                }\r\n                System.out.println(\"\");\r\n            }\r\n        }\r\n        \r\n    }    \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisite Download all poi jar files https:\/\/poi.apache.org\/download.html \u00a0Terms Workbook &#8211; The file contains number of sheets Sheet &#8211; Matrix contains rows and cell Row &#8211; Record of an entity in one row Cell &#8211; A single cell which hold value JFileChooser fileChooser = new JFileChooser(); int returnValue = fileChooser.showDialog(null, &#8220;Select File&#8221;); if(returnValue == JFileChooser.APPROVE_OPTION) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/335"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":8,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":343,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions\/343"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}