{"id":13334,"date":"2014-05-13T23:26:04","date_gmt":"2014-05-13T17:56:04","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13334"},"modified":"2014-05-14T10:04:08","modified_gmt":"2014-05-14T04:34:08","slug":"generate-excel-sheet-with-formula-embedded-in-it","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/generate-excel-sheet-with-formula-embedded-in-it\/","title":{"rendered":"Generate Excel Sheet with Formula embedded in it"},"content":{"rendered":"<p>Recently in our application, we needed to generate an excel sheet where some of the values for the column needed to be calculated on the basis of values of other columns depending upon an embedded formula in the excel sheet.<\/p>\n<p>To work with excel file we need the following in your BuildConfig.groovy:<\/p>\n<p>[java]<\/p>\n<p>dependencies{<br \/>\ncompile &#8216;org.apache.poi:poi:3.7&#8217;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now lets create an excel sheet :<\/p>\n<p>[java]<br \/>\nHSSFWorkbook workBook = new HSSFWorkbook()<br \/>\n\/\/create sheet with name &#8216;Demo Sheet&#8217; in excel file<br \/>\nHSSFSheet spreadSheet = workBook.createSheet(&quot;Demo Sheet&quot;)<br \/>\n[\/java]<\/p>\n<p>For convenience, lets \u00a0specify width of the columns for our excel sheet :<\/p>\n<p>[java]<br \/>\nspreadSheet.setDefaultColumnWidth(15)<br \/>\n[\/java]<\/p>\n<p>Lets name our columns :<\/p>\n<p>[java]<\/p>\n<p>CellStyle style = createStyle(workBook) \/\/we will get back to this later<br \/>\nHSSFRow row = spreadSheet.createRow(0)  \/\/adds a new Row to the sheet<br \/>\nHSSFCell cell<br \/>\nList columnTitle = [&#8216;Value 1&#8217;, &#8216;Value 2&#8217;, &#8216;Value 3&#8217;,<br \/>\n &#8216;Sum&#8217;, &#8216;Difference&#8217;, &#8216;Multiplication&#8217;,<br \/>\n &#8216;Average&#8217;, &#8216;Max&#8217;, &#8216;Min&#8217;]<br \/>\ncolumnTitle.eachWithIndex { String title, Integer index -&gt;<br \/>\ncell = row.createCell(index) \/\/adds a new cell to Row<br \/>\ncell.setCellValue(new HSSFRichTextString(title)) \/\/defining the heading for this sheet<br \/>\ncell.setCellStyle(style) \/\/ applying style to cell created<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>For adding style to the cells we can create our custom CellStyle:<\/p>\n<p>[java]<br \/>\ndef createStyle(HSSFWorkbook wb) {<br \/>\nCellStyle style = wb.createCellStyle()<br \/>\nFont contentFont = wb.createFont()<br \/>\nstyle.setAlignment(CellStyle.ALIGN_CENTER)<br \/>\nstyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER)<br \/>\nstyle.setFont(contentFont)<br \/>\nstyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM)<br \/>\nstyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM)<br \/>\nstyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM)<br \/>\nstyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM)<br \/>\nreturn style<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>To add data in your excel file:<\/p>\n<p>[java]<br \/>\nInteger rowNum = 2<br \/>\n(1..5).each { Integer no -&gt;<br \/>\nrow = spreadSheet.createRow(rowNum++)<\/p>\n<p>\/\/creating random values for first 3 columns<br \/>\ncell = row.createCell(0)<br \/>\ncell.setCellValue(50 + no)<br \/>\ncell.setCellStyle(style)<br \/>\ncell = row.createCell(1)<br \/>\ncell.setCellValue(20 + no)<br \/>\ncell.setCellStyle(createStyle(workBook))<br \/>\ncell = row.createCell(2)<br \/>\ncell.setCellValue(10 + no)<br \/>\ncell.setCellStyle(style)<\/p>\n<p>\/\/ creating columns with embedded formulas in them<\/p>\n<p>\/\/create a column giving sum of first 3 columns<br \/>\ncell = row.createCell(3)<br \/>\ncell.setCellValue(&#8221;)<br \/>\ncell.setCellStyle(style)<br \/>\ncell.setCellFormula(&quot;A${rowNum}+B${rowNum}+C${rowNum}&quot;)<\/p>\n<p>\/\/subtract second and third column from first column<br \/>\ncell = row.createCell(4)<br \/>\ncell.setCellValue(&#8221;)<br \/>\ncell.setCellStyle(style)<br \/>\ncell.setCellFormula(&quot;A${rowNum}-B${rowNum}-C${rowNum}&quot;)<\/p>\n<p>\/\/multiply first, second and third columns<br \/>\ncell = row.createCell(5)<br \/>\ncell.setCellValue(&#8221;)<br \/>\ncell.setCellStyle(style)<br \/>\ncell.setCellFormula(&quot;A${rowNum}*B${rowNum}*C${rowNum}&quot;)<\/p>\n<p>\/\/calculate average of first 3 columns<br \/>\ncell = row.createCell(6)<br \/>\ncell.setCellValue(&#8221;)<br \/>\ncell.setCellStyle(style)<br \/>\ncell.setCellFormula(&quot;AVERAGE(A${rowNum}:C${rowNum})&quot;)<\/p>\n<p>\/\/calculate max of first 3 columns<br \/>\ncell = row.createCell(7)<br \/>\ncell.setCellValue(&#8221;)<br \/>\ncell.setCellStyle(style)<br \/>\ncell.setCellFormula(&quot;MAX(A${rowNum}:C${rowNum})&quot;)<\/p>\n<p>\/\/calculate Minimum of first 3 columns<br \/>\ncell = row.createCell(8)<br \/>\ncell.setCellValue(&#8221;)<br \/>\ncell.setCellStyle(style)<br \/>\ncell.setCellFormula(&quot;MIN(A${rowNum}:C${rowNum})&quot;)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Here we are creating 5 rows, with first 3 columns as random values generated and the values for rest of the columns are generated on the basis of embedded formula in each of them.<\/p>\n<p>Now lets add code to download this excel in our action :<\/p>\n<p>[java]<br \/>\nbyte[] sourceExcel = workBook?.bytes<br \/>\nresponse?.setContentType(&quot;application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;)<br \/>\nresponse?.setHeader &quot;Content-disposition&quot;, &quot;attachment filename=DemoFile.xls&quot;<br \/>\nresponse?.contentLength = sourceExcel?.length<br \/>\nresponse?.outputStream &lt;&lt; sourceExcel<br \/>\n[\/java]<\/p>\n<p>Now, we should be able to download the excel file with embedded formulas in it, if we change the values of any of the first 3 columns, we should see the results of our other columns change!<\/p>\n<p>Hope this helps!<\/p>\n<p>Tanu Siwag<\/p>\n<p>Tanu[at]intelligrape[dot][com]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently in our application, we needed to generate an excel sheet where some of the values for the column needed to be calculated on the basis of values of other columns depending upon an embedded formula in the excel sheet. To work with excel file we need the following in your BuildConfig.groovy: [java] dependencies{ compile [&hellip;]<\/p>\n","protected":false},"author":74,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"gallery","meta":{"iawp_total_views":6},"categories":[7],"tags":[1431,1429,1430,4840],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13334"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/74"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13334"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13334\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}