如何从由jasper报告到PDF的jsp页面中获取表格中给出的数据

时间:2015-10-20 10:22:26

标签: javascript jsp jasper-reports

enter image description here

enter image description here

当我点击"添加行"按钮,创建一个新行,同样当我点击"添加行"按钮3次,创建3个新行,如下所示

enter image description here

然后我们将数据添加到表格中,然后点击"提交"按钮它是在浏览器中显示数据所以一切都很好,我的问题是如何从jsp页面发送表中给出的数据(可能是3行数据或4行数据甚至100行数据) (包含jasper报告)到PDF

在eclipse中,jsp代码如下所示。应该写什么更改或代码,以便将表成功导出为PDF

quotation.jsp

    <script language="javascript">
                // Add row to the HTML table

                function addRow() {    
                    var table = document.getElementById('my_table'); //html table
                    var rowCount = table.rows.length; //no. of rows in table
                    var columnCount =  table.rows[0].cells.length; //no. of columns in table          
                    var row = table.insertRow(rowCount); //insert a row            

                    var cell1 = row.insertCell(0); //create a new cell           
                    var element1 = document.createElement("input"); //create a new element           
                    element1.type = "checkbox"; //set the element type 
                    element1.setAttribute('id', 'newCheckbox'); //set the id attribute         
                    cell1.appendChild(element1); //append element to cell

                    var cell2 = row.insertCell(1);            
                    var element2 = document.createElement("input");            
                    element2.type = "text"; 
                    element2.setAttribute('id', 'newInput'); //set the id attribute
                    element2.setAttribute('name', 'sl'+rowCount);
                    element2.setAttribute('style', 'width: 50px');
                    cell2.appendChild(element2);      

                    var cell3 = row.insertCell(2);            
                    var element3 = document.createElement("input");            
                    element3.type = "text"; 
                    element3.setAttribute('id', 'newInput'); //set the id attribute
                    element3.setAttribute('name', 'item'+rowCount);
                    cell3.appendChild(element3);         

                    var cell4 = row.insertCell(3);            
                    var element4 = document.createElement("input");            
                    element4.type = "textarea"; 
                    element4.setAttribute('rows', '4');
                    element4.setAttribute('cols','20');
                    element4.setAttribute('id', 'newInput'); //set the id attribute
                    element4.setAttribute('name', 'discription'+rowCount);
                    cell4.appendChild(element4);


                    var cell5 = row.insertCell(4);            
                    var element5 = document.createElement("input");            
                    element5.type = "text"; 
                    element5.setAttribute('id', 'newInput'); //set the id attribute
                    element5.setAttribute('name', 'quantity'+rowCount);
                    element5.setAttribute('style', 'width: 50px');
                    cell5.appendChild(element5);

                    var cell6 = row.insertCell(5);            
                    var element6 = document.createElement("input");            
                    element6.type = "text"; 
                    element6.setAttribute('id', 'newInput'); //set the id attribute
                    element6.setAttribute('name', 'price'+rowCount);
                    cell6.appendChild(element6);

                    var cell7 = row.insertCell(6);            
                    var element7 = document.createElement("input");            
                    element7.type = "text"; 
                    element7.setAttribute('id', 'newInput'); //set the id attribute
                    element7.setAttribute('name', 'CST'+rowCount);
                    element7.setAttribute('style', 'width: 50px');
                    cell7.appendChild(element7);




                    var cell8 = row.insertCell(7); //create a new cell           
                    var element8 = document.createElement("input"); //create a new element           
                    element8.type = "checkbox"; //set the element type 
                    element8.setAttribute('id', 'vat5'); //set the id attribute 
                    element8.setAttribute('name','tax'+rowCount);
                    element8.setAttribute('value','vat5');
                    cell8.appendChild(element8);

                    var cell9 = row.insertCell(8); //create a new cell      
                    var element9 = document.createElement("input"); //create a new element           
                    element9.type = "checkbox"; //set the element type 
                    element9.setAttribute('id', 'vat14'); //set the id attribute 
                    element9.setAttribute('name','tax'+rowCount);
                    element9.setAttribute('value','vat14')  ;     
                    cell9.appendChild(element9);

                    var cell10 = row.insertCell(9); //create a new cell      
                    var element10 = document.createElement("input"); //create a new element           
                    element10.type = "checkbox"; //set the element type 
                    element10.setAttribute('id', 'serviceTax'); //set the id attribute 
                    element10.setAttribute('name','tax'+rowCount);    
                    element10.setAttribute('value','serviceTax');
                    cell10.appendChild(element10);


                } 
    </script>
    <body>
<form name="form" action="quotationdisplay.jsp" method="post">
    <table id="my_table" align="center" border="2" cellpadding="0" cellspacing="0">
                                    <thead><tr>

                                            <th>Select</th>
                                            <th>Sl_no</th>
                                            <th>Item</th>
                                            <th>Description</th>
                                            <th>Quantity</th>
                                            <th>Price/Unit</th>
                                            <th>CST%</th>
                                            <th>VAT5.5</th>
                                            <th>VAT14.5</th>
                                            <th>ServiceTax</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
 <input type="button" value="Add row" name="add" onClick="addRow()" />
</form>
    </body>

quotationdisplay.jsp

<%  
            int sum = 0;
             Connection con = null;
            String row = request.getParameter("rowCount");          

            int rowCount = Integer.parseInt(row);
          System.out.println("rowCount=" + rowCount);
           int i = 1;
           while (i < rowCount) {
               System.out.println("in while i="+i);
               String sli = request.getParameter("sl" + i).trim();
               System.out.println("in while i"+sli);
               String itemi = request.getParameter("item" + i).trim();
               System.out.println("in while i"+itemi);
               String discriptioni = request.getParameter("discription" + i).trim();
               System.out.println("in while i"+discriptioni);
                String quantityi = request.getParameter("quantity" + i).trim();
                System.out.println("in while iquantityi="+quantityi);
                String pricei = request.getParameter("price" + i).trim();             
                System.out.println("in while i pricei ="+pricei);
               int p = Integer.parseInt(pricei);
                int q = Integer.parseInt(quantityi);
                int t = p * q;
               sum = sum + t;
               i = i + 1;
           }
       %>
 <tr id="table">
                <td>

                    <table id="my_table" align="center" style="border: 1px solid " cellpadding="0" cellspacing="0" width="100%">
                        <thead>
                            <tr>
                                <th style="border: 1px solid">Sl.no</th>
                                <th style="border: 1px solid">Item</th>
                                <th style="border: 1px solid">Description</th>
                                <th style="border: 1px solid">Quantity</th>
                                <th style="border: 1px solid">Price/Unit</th>
                                <th style="border: 1px solid">Total Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <%
                                int j = 1;
                                while (j < rowCount) {%>
                            <tr>
                                <td style="border: 1px solid"><%= request.getParameter("sl" + j)%>

                                </td>
                                <td style="border: 1px solid"><%= request.getParameter("item" + j)%></td>
                                <td style="border: 1px solid"><%= request.getParameter("discription" + j)%></td>
                                <%int q = Integer.parseInt(request.getParameter("quantity" + j));%>
                                <td style="border: 1px solid"><%= q%></td>
                                <% int p = Integer.parseInt(request.getParameter("price" + j));%>
                                <td style="border: 1px solid"><%= p%></td>
                                <td style="border: 1px solid"><%= (q * p)%></td>
                                <%j++;%>
                            </tr>
                            <%}%>
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr id="total">
                <td style="text-align: right;border: 1px solid #000">
                    Total: <%= sum%>
                </td>
            </tr>
            <tr>
                <td style="border: 1px solid">
                    <%
                        int to = (int) sum;
                        NumToWords w = new NumToWords();
                        String words = w.convert(to);
                        String upper = words.toUpperCase();
                    %>
                    INR : <b><%= upper%> ONLY </b>
                </td>
            </tr>

1 个答案:

答案 0 :(得分:0)

如果他们不在数据库中,您需要设置自己的JRDataSource

通常在java中我们使用JRBeanCollectionDataSource(在列表中传递对象)但在你的情况下我认为最简单的方法是使用JRMapCollectionDataSource

例如

List<Map<String,Object>> ds = new ArrayList<Map<String, Object>> ();
for (all my rows in the table) {
  Map<String,Object> map = new HashMap<String, Object>();
  map.put("col1", getValueFromCol1());
  map.put("col2", getValueFromCol2());
  ecc....
  ds.add(map);
}           
JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(ds);

// TO get the print past the dataSource instead of connection
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, yourParametersMap, dataSource);

在.jrxml中,字段名称为

<field name="col1" class="java.lang.String"/>
<field name="col2" class="java.lang.String"/>
...ecc..

假设您将String传递到地图中,否则您需要调整为相对类。

当然,您的记录数量与ds的大小相同。