Thursday, August 9, 2018

Convert HTML Table into EXCEL , MS Word , PDF in Java

Report generation in different formats is troublesome in most of the projects. However, there still is an easier way to do for experienced engineers. Here I will recommend a jar which can directly convert any HTML Table with CSS into 3 Main report formats (EXCEL, WORD, PDF). This project saves developers tons of efforts to manually write a utility class to parse the XPath in Apache POI or Doc4j and generate reports. here is the address :


Here is the code example :


public static void main(String[] args) throws FileNotFoundException, IOException {
  
 String html = IOUtils.toString(new FileInputStream("./Sample.html"));
  
 new ExcelExporter().exportHtml(html, new File("./report.xlsx"));
}

If you are using this jar, 3 points need to be noted :

1. CSS Style must be defined in the <Style> tag.


<style type="text/css">
.pNewCounter {
  background: #FFC000;
 }

table, tbody, tfoot, thead, tr, th, td, footer, header {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
 }
</style>


                    2. Date format data must have data-date-cell-format attribute                              


   <tr>
          <td>Creation Date</td>
   <td data-date-cell-format="dd/MM/yyyy">06/08/2018</td>
   </tr>


3. Text Data must have data-text-cell attribute



    <tr>
   <td>Offer Expiry Date</td>
   <td data-date-cell-format="dd/MM/yyyy">14/09/2018</td> 
  <td data-date-cell-format="dd/MM/yyyy">14/09/2018</td>
    </tr>
 
 
    <tr>
   <td>Offered Amount</td>
   <td data-text-cell='true' >3,500,000.00</td>
                <td data-text-cell='true' ><strong>3,600,000.00</strong></td>
   </tr>
 
 
    <tr>
   <td>Offered Amount (%) </td>
   <td data-text-cell='true' >35.000%</td>
                <td data-text-cell='true'><strong>36.000%</strong></td>
    </tr>


Sample HTML :

https://github.com/callow/html-exporter/blob/master/Sample.html






No comments:

Post a Comment

Add Loading Spinner for web request.

when web page is busily loading. normally we need to add a spinner for the user to kill their waiting impatience. Here, 2 steps we need to d...