[英]Fetch Data from recordset and store it in list
我正在嘗試讀取和 excel 文件並生成 pdf 報告
我面臨的問題是我能夠從 excel 文件中獲取記錄但無法將其存儲在 ArrayList/List 中
當我打印列表時,它只顯示 Excel 文件的最后一條記錄
@ManagedBean
@RequestScoped
public class testbean {
private List<test> Testlist = new ArrayList<test>();
String loc1 = UploadDownloadFileServlet.location;
public List<test> getTestlist() throws FilloException {
test t = new test();
Fillo fillo = new Fillo();
Connection connection = fillo.getConnection(loc1);
String strQuery = "select * from Sheet1";
Recordset recordset = connection.executeQuery(strQuery);
System.out.println("path "+loc1);
int k = recordset.getCount();
System.out.println(k);
while (recordset.next()) {
t.setNAME(recordset.getField("NAME"));
t.setSURNAME(recordset.getField("SURNAME"));
t.setCITY(recordset.getField("CITY"));
System.out.println(i);
}
Testlist.add(t);
recordset.close();
connection.close();
return Testlist;
}
public void setLstPersonas(List<test> Testlist) {
this.Testlist = Testlist;
}
public void exportarPDF(ActionEvent actionEvent) throws JRException, IOException, FilloException{
File jasper = new File(FacesContext.getCurrentInstance().getExternalContext().getRealPath("/newReport.jasper"));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasper.getPath(),null, new JRBeanCollectionDataSource(this.getTestlist()));
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.addHeader("Content-disposition","attachment; filename=jsfReporte.pdf");
ServletOutputStream stream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, stream);
stream.flush();
stream.close();
FacesContext.getCurrentInstance().responseComplete();
}
}
EXCEL中的數據
NAME SURNAME CITY
JP SHAH MUM
RAHUL BOM
ROHIT RAUL DEL
PARTH BAN
JAY PIKLE BEN
PDF 輸出
NAME SURNAME CITY
JAY PIKLE BEN
JAY PIKLE BEN
JAY PIKLE BEN
JAY PIKLE BEN
JAY PIKLE BEN
你應該把 Testlist.add(t); 在你的 while 循環中
改變這個
while (recordset.next()) {
t.setNAME(recordset.getField("NAME"));
t.setSURNAME(recordset.getField("SURNAME"));
t.setCITY(recordset.getField("CITY"));
System.out.println(i);
}
Testlist.add(t);
對此
while (recordset.next()) {
t.setNAME(recordset.getField("NAME"));
t.setSURNAME(recordset.getField("SURNAME"));
t.setCITY(recordset.getField("CITY"));
System.out.println(i);
// this line should be inside the loop
Testlist.add(t);
}
它是一個列表列表。 TestList 正在進行多項測試。 在向測試添加值之前,不要在公共場合初始化測試,而是在 Whileloop 內初始化。 並將測試添加到 whileloop 中的測試列表。
我有類似的問題。我的是地圖列表。就像你挖掘的最后一條記錄覆蓋以前的記錄並添加新記錄一樣。 當我在 while 循環中初始化地圖時,它得到了解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.