gpt4 book ai didi

javascript - 如何从包含jasper报告的jsp页面获取表中给出的数据到PDF

转载 作者:行者123 更新时间:2023-11-28 06:47:52 28 4
gpt4 key购买 nike

enter image description here

enter image description here

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

enter image description here

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

在eclipse中jsp代码如下所示。我应该进行哪些更改或编写哪些代码才能将表格成功导出为 PDF

报价.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>

报价显示.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>

最佳答案

是的,如果它们不在数据库中,您需要设置自己的 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 的大小相同。

关于javascript - 如何从包含jasper报告的jsp页面获取表中给出的数据到PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33234096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com