如何创建动态记录行以使用列表创建 xls 文件
how to create dynamic rows of records for creating xls file using list
从数据库中获取记录后,我在一些列表中添加了数据,并为它们设置了一些会话变量,以便我可以通过使用会话的 get(key) 方法以另一种方法访问,我成功地做到了所以。现在我想要的是我想通过在行中设置此列表值来创建动态记录,但我无法执行 so.It 创建文件但没有显示记录。下面是我的代码:
package com.ca.actions;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import com.ca.database.Database;
import com.ca.pojo.Event;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class DataForGeneralReportsAction extends ActionSupport implements
Preparable, SessionAware {
private List<String> eventsGeneral = new ArrayList<String>();
private List<String> companiesGeneral = new ArrayList<String>();
private SessionMap<String, Object> sessionMapGeneral;
List<String> eventIdList = new ArrayList<String>();
List<String> eventNameList = new ArrayList<String>();
List<String> eventVenueList = new ArrayList<String>();
List<String> eventTimeList = new ArrayList<String>();
List<String> companyNameList = new ArrayList<String>();
List<String> totalAmountList = new ArrayList<String>();
List<String> receivedAmountList = new ArrayList<String>();
List<String> balanceAmountList = new ArrayList<String>();
List<String> eventTdsList = new ArrayList<String>();
List<String> paymentDateList = new ArrayList<String>();
List<String> chequeDdList = new ArrayList<String>();
private String eventGeneral = null;
private String companyGeneral = null;
List<Event> dataForGeneralReports;
public List<String> getEventIdList() {
return eventIdList;
}
public void setEventIdList(List<String> eventIdList) {
this.eventIdList = eventIdList;
}
public List<String> getEventNameList() {
return eventNameList;
}
public void setEventNameList(List<String> eventNameList) {
this.eventNameList = eventNameList;
}
public List<String> getEventVenueList() {
return eventVenueList;
}
public void setEventVenueList(List<String> eventVenueList) {
this.eventVenueList = eventVenueList;
}
public List<String> getEventTimeList() {
return eventTimeList;
}
public void setEventTimeList(List<String> eventTimeList) {
this.eventTimeList = eventTimeList;
}
public List<String> getCompanyNameList() {
return companyNameList;
}
public void setCompanyNameList(List<String> companyNameList) {
this.companyNameList = companyNameList;
}
public List<String> getTotalAmountList() {
return totalAmountList;
}
public void setTotalAmountList(List<String> totalAmountList) {
this.totalAmountList = totalAmountList;
}
public List<String> getReceivedAmountList() {
return receivedAmountList;
}
public void setReceivedAmountList(List<String> receivedAmountList) {
this.receivedAmountList = receivedAmountList;
}
public List<String> getBalanceAmountList() {
return balanceAmountList;
}
public void setBalanceAmountList(List<String> balanceAmountList) {
this.balanceAmountList = balanceAmountList;
}
public List<String> getEventTdsList() {
return eventTdsList;
}
public void setEventTdsList(List<String> eventTdsList) {
this.eventTdsList = eventTdsList;
}
public List<String> getPaymentDateList() {
return paymentDateList;
}
public void setPaymentDateList(List<String> paymentDateList) {
this.paymentDateList = paymentDateList;
}
public List<String> getChequeDdList() {
return chequeDdList;
}
public void setChequeDdList(List<String> chequeDdList) {
this.chequeDdList = chequeDdList;
}
public SessionMap<String, Object> getSessionMapGeneral() {
return sessionMapGeneral;
}
public void setSessionMapGeneral(
SessionMap<String, Object> sessionMapGeneral) {
this.sessionMapGeneral = sessionMapGeneral;
}
public String getEventGeneral() {
return eventGeneral;
}
public void setEventGeneral(String eventGeneral) {
this.eventGeneral = eventGeneral;
}
public String getCompanyGeneral() {
return companyGeneral;
}
public void setCompanyGeneral(String companyGeneral) {
this.companyGeneral = companyGeneral;
}
public List<Event> getDataForGeneralReports() {
return dataForGeneralReports;
}
public void setDataForGeneralReports(List<Event> dataForGeneralReports) {
this.dataForGeneralReports = dataForGeneralReports;
}
public List<String> getEventsGeneral() {
return eventsGeneral;
}
public void setEventsGeneral(List<String> eventsGeneral) {
this.eventsGeneral = eventsGeneral;
}
public List<String> getCompaniesGeneral() {
return companiesGeneral;
}
public void setCompaniesGeneral(List<String> companiesGeneral) {
this.companiesGeneral = companiesGeneral;
}
public DataForGeneralReportsAction() {
// TODO Auto-generated constructor stub
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
Connection con = null;
try {
con = new Database().Get_Connection();
// load companies
PreparedStatement ps = con
.prepareStatement("SELECT DISTINCT company_name FROM event");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
companiesGeneral.add(rs.getString("company_name"));
}
// load events
ps = con.prepareStatement("SELECT DISTINCT event_name FROM event");
rs = ps.executeQuery();
while (rs.next()) {
eventsGeneral.add(rs.getString("event_name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
}
@Override
public String execute() throws Exception {
Connection con = null;
try {
con = new Database().Get_Connection();
// load the table. The first time the table is loaded completely
String sql = "SELECT EVENT_ID, EVENT_NAME, COMPANY_NAME,EVENT_VENUE,TOTAL_AMOUNT,RECEIVED_AMOUNT,EVENT_TDS,BALANCE_AMOUNT,CHEQUE_DD_NO,"
+ "date_format(PAYMENT_DATE,'%d/%m/%Y') as dateAsPayment,EVENT_TIME "
+ "FROM event";
String where = "";
// if instead this action has been called from the JSP page,
// the result is filtered on event and company:
if (eventGeneral != null && companyGeneral != null) {
where = " WHERE event_name = ? AND company_name = ?";
}
// load companies
PreparedStatement ps = con.prepareStatement(sql + where);
if (where.length() > 0) {
ps.setString(1, eventGeneral);
ps.setString(2, companyGeneral);
}
dataForGeneralReports = new ArrayList<Event>();
ResultSet rs = ps.executeQuery();
int i, j = 0;
while (rs.next()) {
dataForGeneralReports.add(new Event(rs.getString("EVENT_ID"),
rs.getString("EVENT_NAME"), rs
.getString("COMPANY_NAME"), rs
.getString("EVENT_VENUE"), rs
.getString("EVENT_TIME"), rs
.getString("TOTAL_AMOUNT"), rs
.getString("RECEIVED_AMOUNT"), rs
.getString("CHEQUE_DD_NO"), rs
.getString("dateAsPayment"), rs
.getString("BALANCE_AMOUNT"), rs
.getString("EVENT_TDS")));
eventIdList.add(rs.getString("EVENT_ID"));
eventNameList.add(rs.getString("EVENT_NAME"));
companyNameList.add(rs.getString("COMPANY_NAME"));
eventVenueList.add(rs.getString("EVENT_VENUE"));
eventTimeList.add(rs.getString("EVENT_TIME"));
totalAmountList.add(rs.getString("TOTAL_AMOUNT"));
receivedAmountList.add(rs.getString("RECEIVED_AMOUNT"));
chequeDdList.add(rs.getString("CHEQUE_DD_NO"));
paymentDateList.add(rs.getString("dateAsPayment"));
eventTdsList.add(rs.getString("EVENT_TDS"));
balanceAmountList.add(rs.getString("BALANCE_AMOUNT"));
}
sessionMapGeneral.put("eventIdPdf", eventIdList);
sessionMapGeneral.put("eventNamePdf", eventNameList);
sessionMapGeneral.put("companyNamePdf", companyNameList);
sessionMapGeneral.put("eventVenuePdf", eventVenueList);
sessionMapGeneral.put("eventTimePdf", eventTimeList);
sessionMapGeneral.put("totalAmountPdf", totalAmountList);
sessionMapGeneral.put("receivedAmountPdf", receivedAmountList);
sessionMapGeneral.put("chequeDdPdf", chequeDdList);
sessionMapGeneral.put("paymentDatePdf", paymentDateList);
sessionMapGeneral.put("eventTdsPdf", eventTdsList);
sessionMapGeneral.put("balanceAmountPdf", balanceAmountList);
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
return SUCCESS;
}
public String generatePdfGeneral() throws Exception {
System.out.println(sessionMapGeneral.get("eventIdPdf"));
Document document = new Document(PageSize.A4_LANDSCAPE, 50, 50, 50, 50);
float[] columnWidths = { 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D:\GeneralReports.pdf"));
PdfPTable table = new PdfPTable(11);
table.setSpacingBefore(25);
table.setWidthPercentage(100);
table.setSpacingAfter(25);
table.setWidths(columnWidths);
PdfPCell c1 = new PdfPCell(new Phrase("Event ID "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Name "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Time"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Venue"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Company Name"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Total Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Received Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cheque/DD Number"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Payment Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event TDS"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Balance Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
PdfPCell cell = new PdfPCell();
List<String> list = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list) {
cell.addElement(new Paragraph(item));
}
PdfPCell cell1 = new PdfPCell();
List<String> list1 = (List<String>) sessionMapGeneral
.get("eventNamePdf");
for (String item : list1) {
cell1.addElement(new Paragraph(item));
}
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell();
List<String> list2 = (List<String>) sessionMapGeneral
.get("eventTimePdf");
for (String item : list2) {
cell2.addElement(new Paragraph(item));
}
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell();
List<String> list3 = (List<String>) sessionMapGeneral
.get("eventVenuePdf");
for (String item : list1) {
cell3.addElement(new Paragraph(item));
}
table.addCell(cell3);
PdfPCell cell4 = new PdfPCell();
List<String> list4 = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list4) {
cell4.addElement(new Paragraph(item));
}
table.addCell(cell4);
PdfPCell cell5 = new PdfPCell();
List<String> list5 = (List<String>) sessionMapGeneral
.get("companyNamePdf");
for (String item : list5) {
cell5.addElement(new Paragraph(item));
}
table.addCell(cell5);
PdfPCell cell6 = new PdfPCell();
List<String> list6 = (List<String>) sessionMapGeneral
.get("totalAmountPdf");
for (String item : list6) {
cell6.addElement(new Paragraph(item));
}
table.addCell(cell6);
PdfPCell cell7 = new PdfPCell();
List<String> list7 = (List<String>) sessionMapGeneral
.get("receivedAmountPdf");
for (String item : list7) {
cell7.addElement(new Paragraph(item));
}
table.addCell(cell7);
PdfPCell cell8 = new PdfPCell();
List<String> list8 = (List<String>) sessionMapGeneral
.get("chequeDdPdf");
for (String item : list8) {
cell8.addElement(new Paragraph(item));
}
table.addCell(cell8);
PdfPCell cell9 = new PdfPCell();
List<String> list9 = (List<String>) sessionMapGeneral
.get("paymentDatePdf");
for (String item : list9) {
cell9.addElement(new Paragraph(item));
}
table.addCell(cell9);
PdfPCell cell10 = new PdfPCell();
List<String> list10 = (List<String>) sessionMapGeneral
.get("eventTdsPdf");
for (String item : list10) {
cell10.addElement(new Paragraph(item));
}
table.addCell(cell10);
PdfPCell cell11 = new PdfPCell();
List<String> list11 = (List<String>) sessionMapGeneral
.get("balanceAmountPdf");
for (String item : list11) {
cell11.addElement(new Paragraph(item));
}
table.addCell(cell11);
document.open();
document.add(table);
document.close();
return "success";
}
public String generateGeneralXls() throws Exception {
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell(0).setCellValue("Event ID");
rowhead.createCell(1).setCellValue("Event Name");
rowhead.createCell(2).setCellValue("Event Time");
rowhead.createCell(3).setCellValue("Event Venue");
rowhead.createCell(4).setCellValue("Company Name");
rowhead.createCell(5).setCellValue("Total Amount");
rowhead.createCell(6).setCellValue("Received Amount");
rowhead.createCell(7).setCellValue("Payment Date");
rowhead.createCell(8).setCellValue("Cheque/DD No.");
rowhead.createCell(9).setCellValue("Event TDS");
rowhead.createCell(10).setCellValue("Balance Amount");
FileOutputStream fileOut;
fileOut = new FileOutputStream("D:\Samplmgjkm.xls");
// HSSFRow row1 = sheet.createRow((short) 1);
System.out.println(sessionMapGeneral.size());
for (int i = 1; i <= sessionMapGeneral.size(); i++) {
HSSFRow row1 = sheet.createRow((short) i);
row1.createCell(i-1).setCellValue(
sessionMapGeneral.get("eventIdPdf").toString());
row1.createCell(i).setCellValue(
sessionMapGeneral.get("eventNamePdf").toString());
}
/*
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
*/
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@Override
public void setSession(Map<String, Object> map) {
// TODO Auto-generated method stub
sessionMapGeneral = (SessionMap) map;
}
}
我编辑了我的代码,在其中我得到了字符串形式的结果,但所有记录都显示在单个单元格中。我希望新 cell.I 中的每条记录都附上它的外观图片。
没有错误显示。请帮我解决我的问题。
试试这个:
FileOutputStream fileOut;
fileOut = new FileOutputStream("D:\Samplmgjkm.xls");
int nextRow = 1;
while(rs.next()){
HSSFRow r = sheet.getRow(nextRow);
if (r == null) {
r = sheet.createRow(nextRow);
}
HSSFCell c = r.getCell(1, Row.CREATE_NULL_AS_BLANK);
c.setCellValue(rs.getString(1));
HSSFCell c2 = r.getCell(2, Row.CREATE_NULL_AS_BLANK);
c2.setCellValue(rs.getString(2));
nextRow++;
}
我已经检查了你的代码并做了一些如下的编辑,所以请参考下面的代码,你会得到解决方案。
List<String> eventIdPdf = (List<String>) sessionMapGeneral.get("eventNamePdf");
List<String> eventNamePdf = (List<String>) sessionMapGeneral.get("eventNamePdf");
for (int i = 1; i <= sessionMapGeneral.size(); i++) {
HSSFRow row1 = sheet.createRow((short) i);
row1.createCell(i-1).setCellValue(eventIdPdf.get(i).toString());
row1.createCell(i).setCellValue(eventNamePdf.get(i).toString());
}
当您在一个变量(如 eventIdPdf)中获取字符串列表时,根据您在代码中的使用,您是在一个单元格中编写整个列表,因此您在一个单元格中获取整个列表,现在检查上面的方法,以便它会一一显示答案。
希望这会奏效..
你的第一个问题在这里:
row1.createCell(i-1).setCellValue(
sessionMapGeneral.get("eventIdPdf").toString());
因为 sessionMapGeneral.get("eventIdPdf").toString()
returns 整个 eventIdList,或者更准确地说,eventIdList 的所有值都是一个字符串,所以这个表达式的结果看起来像
"[value1, value2, ... , valueN]
"
这个(很长的)结果完全进入 row1.createCell(i-1)
。但实际上,Cell(i-1)
应该只得到 "value1"
。然后我们转到
你的第二个问题:
for (int i = 1; i <= sessionMapGeneral.size(); i++)
您只遍历 sessionMapGeneral
,但是遍历像 table 或矩阵这样的二维结构需要两个循环(嵌套循环)。请注意,您的 sessionMapGeneral's
元素是键值对,其中每个键都是一个字符串,例如"eventIdPdf"
,每个值都是一个列表,例如eventIdList
。因此,在第一个循环中,您应该访问 sessionMapGeneral
的值(列表)以获取存储在那里的数据。然后,在第二个循环中,您可以遍历在第一个循环中获得的列表,以获取每个数据项并将其设置到 table.
所需的单元格中
下一个小问题是,您无法通过索引访问 SessionMap's
元素,但有一种方法可以访问 "beat the system" :) - 这里是
你的解决方案:(请注意评论)
//modify your key-Strings in order to be able to sort them later easily
sessionMapGeneral.put("01_eventIdPdf", eventIdList);
sessionMapGeneral.put("02_eventNamePdf", eventNameList);
sessionMapGeneral.put("03_companyNamePdf", companyNameList);
sessionMapGeneral.put("04_eventVenuePdf", eventVenueList);
sessionMapGeneral.put("05_eventTimePdf", eventTimeList);
sessionMapGeneral.put("06_totalAmountPdf", totalAmountList);
sessionMapGeneral.put("07_receivedAmountPdf", receivedAmountList);
sessionMapGeneral.put("08_chequeDdPdf", chequeDdList);
sessionMapGeneral.put("09_paymentDatePdf", paymentDateList);
sessionMapGeneral.put("10_eventTdsPdf", eventTdsList);
sessionMapGeneral.put("11_balanceAmountPdf", balanceAmountList);
...
fileOut = new FileOutputStream("D:\Samplmgjkm.xls");
// get all the key-Strings from sessionMapGeneral and store them in an ArrayList in order to be able to access the elements by index
List<String> keys = new ArrayList<String>(sessionMapGeneral.keySet());
// and sort them, so that they suit to your "rowhead"
Collections.sort(keys);
// iterate through the keys and fill out the xls table column by column:
for (int key = 0; key < keys.size(); key++) {
//hold the current List from sessionMapGeneral for the current column in a variable
List<String> currentList = (List<String>) (sessionMapGeneral.get(keys.get(key)));
// iterate through the current ArrayList (column) and fill out the cells of this column row by row:
for (int row = 0; row < currentList.size(); row++) {
// hold the current row in a variable
HSSFRow currentRow = sheet.getRow(row+1);
// create a row only once to avoid deleting the data set earlier and set the value into the cell
if (currentRow == null) {
currentRow = sheet.createRow((short) row+1); // I don't understand why do you cast it to short. To save 16 bit per row?
currentRow.createCell(key).setCellValue(currentList.get(row));
} else {
currentRow.createCell(key).setCellValue(currentList.get(row));
}
}
// to make the width of columns looking well, add this line here:
sheet.autoSizeColumn(key);
}
第一个 poposal : 使用事件 object.You 构建一个你可以使用的事件对象列表并放入 SessionMap 中,如果你愿意,使用列表构建另一个列表事件)
这里假设事件对象中有 getter:
public String generateGeneralXls() throws Exception {
try {
String titles="Event ID,Event Name,Event Time,Event Venue,Company Name,Total Amount,Received Amount,Payment Date,Cheque/DD No,Event TDS,Balance Amount";
String[]arrTiltes=titles.split(",");
FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
int row=0;
HSSFRow rowTitle = worksheet.createRow(row);
// set titles
for(int i=0;i<arrTiltes.length;i++){
HSSFCell cellTitle=rowTitle.createCell(i);
cellTitle.setCellValue(arrTiltes[i]);
}
//setting values
row++;
for(Event dataValue:dataForGeneralReports){
HSSFRow rowValue = worksheet.createRow(row);
HSSFCell cell0 = rowValue.createCell(0);
cell0.setCellValue(dataValue.getEventID());
HSSFCell cell1 = rowValue.createCell(1);
cell1.setCellValue(dataValue.getEventName());
HSSFCell cell2 = rowValue.createCell(2);
cell2.setCellValue(dataValue.getEventTime());
HSSFCell cell3 = rowValue.createCell(3);
cell3.setCellValue(dataValue.getEventVenue());
HSSFCell cell4 = rowValue.createCell(4);
cell4.setCellValue(dataValue.getCompanyName());
HSSFCell cell5 = rowValue.createCell(5);
cell5.setCellValue(dataValue.getTotalAmount());
HSSFCell cell6 = rowValue.createCell(6);
cell6.setCellValue(dataValue.getReceivedAmount());
HSSFCell cell7 = rowValue.createCell(7);
cell7.setCellValue(dataValue.getPaymentDate());
HSSFCell cell8 = rowValue.createCell(8);
cell8.setCellValue(dataValue.getChequeDDNo());
HSSFCell cell9 = rowValue.createCell(9);
cell9.setCellValue(dataValue.getEventTDS());
HSSFCell cell10 = rowValue.createCell(10);
cell10.setCellValue(dataValue.getBalanceAmount());
row++;
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
您可以对 PDF 使用相同的想法。
请小心您的 PDF 代码,我怀疑您没有预期的结果。
您忘记在 table 上添加单元格(代码中的第一个单元格)
你有 12 个单元格,但是你声明了 11 个:
我建议你删除
的“阻止”
PdfPCell cell4
第二个提案:如果你真的想使用你的列表,那么遍历每个列表以获得每个列表中的第一个值,每个列表的第二个值等等..
示例假设所有列表具有相同的大小(来自您的结果集):
public String generateGeneralXlsOther() throws Exception {
try {
String titles="eventIdPdf,eventNamePdf,companyNamePdf,eventVenuePdf,eventTimePdf,totalAmountPdf,receivedAmountPdf,receivedAmountPdf,chequeDdPdf,paymentDatePdf,eventTdsPdf,balanceAmountPdf";
String[]arrTiltes=titles.split(",");
FileOutputStream fileOut = new FileOutputStream("poi-testOtehr.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
HSSFRow rowTitle = worksheet.createRow(0);
// set titles
for(int i=0;i<arrTiltes.length;i++){
HSSFCell cellTitle=rowTitle.createCell(i);
cellTitle.setCellValue(arrTiltes[i]);
}
int size=((List<String>) sessionMapGeneral.get(arrTiltes[0])).size();
for(int row=0;row<size;row++){
HSSFRow rowValue = worksheet.createRow(row+1);
int cell=0;
for(int i=0;i<arrTiltes.length;i++){
List<String> theList=(List<String>) sessionMapGeneral.get(arrTiltes[i]);
HSSFCell cell0 = rowValue.createCell(cell);
cell0.setCellValue(theList.get(row));
cell++;
}
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
从数据库中获取记录后,我在一些列表中添加了数据,并为它们设置了一些会话变量,以便我可以通过使用会话的 get(key) 方法以另一种方法访问,我成功地做到了所以。现在我想要的是我想通过在行中设置此列表值来创建动态记录,但我无法执行 so.It 创建文件但没有显示记录。下面是我的代码:
package com.ca.actions;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import com.ca.database.Database;
import com.ca.pojo.Event;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class DataForGeneralReportsAction extends ActionSupport implements
Preparable, SessionAware {
private List<String> eventsGeneral = new ArrayList<String>();
private List<String> companiesGeneral = new ArrayList<String>();
private SessionMap<String, Object> sessionMapGeneral;
List<String> eventIdList = new ArrayList<String>();
List<String> eventNameList = new ArrayList<String>();
List<String> eventVenueList = new ArrayList<String>();
List<String> eventTimeList = new ArrayList<String>();
List<String> companyNameList = new ArrayList<String>();
List<String> totalAmountList = new ArrayList<String>();
List<String> receivedAmountList = new ArrayList<String>();
List<String> balanceAmountList = new ArrayList<String>();
List<String> eventTdsList = new ArrayList<String>();
List<String> paymentDateList = new ArrayList<String>();
List<String> chequeDdList = new ArrayList<String>();
private String eventGeneral = null;
private String companyGeneral = null;
List<Event> dataForGeneralReports;
public List<String> getEventIdList() {
return eventIdList;
}
public void setEventIdList(List<String> eventIdList) {
this.eventIdList = eventIdList;
}
public List<String> getEventNameList() {
return eventNameList;
}
public void setEventNameList(List<String> eventNameList) {
this.eventNameList = eventNameList;
}
public List<String> getEventVenueList() {
return eventVenueList;
}
public void setEventVenueList(List<String> eventVenueList) {
this.eventVenueList = eventVenueList;
}
public List<String> getEventTimeList() {
return eventTimeList;
}
public void setEventTimeList(List<String> eventTimeList) {
this.eventTimeList = eventTimeList;
}
public List<String> getCompanyNameList() {
return companyNameList;
}
public void setCompanyNameList(List<String> companyNameList) {
this.companyNameList = companyNameList;
}
public List<String> getTotalAmountList() {
return totalAmountList;
}
public void setTotalAmountList(List<String> totalAmountList) {
this.totalAmountList = totalAmountList;
}
public List<String> getReceivedAmountList() {
return receivedAmountList;
}
public void setReceivedAmountList(List<String> receivedAmountList) {
this.receivedAmountList = receivedAmountList;
}
public List<String> getBalanceAmountList() {
return balanceAmountList;
}
public void setBalanceAmountList(List<String> balanceAmountList) {
this.balanceAmountList = balanceAmountList;
}
public List<String> getEventTdsList() {
return eventTdsList;
}
public void setEventTdsList(List<String> eventTdsList) {
this.eventTdsList = eventTdsList;
}
public List<String> getPaymentDateList() {
return paymentDateList;
}
public void setPaymentDateList(List<String> paymentDateList) {
this.paymentDateList = paymentDateList;
}
public List<String> getChequeDdList() {
return chequeDdList;
}
public void setChequeDdList(List<String> chequeDdList) {
this.chequeDdList = chequeDdList;
}
public SessionMap<String, Object> getSessionMapGeneral() {
return sessionMapGeneral;
}
public void setSessionMapGeneral(
SessionMap<String, Object> sessionMapGeneral) {
this.sessionMapGeneral = sessionMapGeneral;
}
public String getEventGeneral() {
return eventGeneral;
}
public void setEventGeneral(String eventGeneral) {
this.eventGeneral = eventGeneral;
}
public String getCompanyGeneral() {
return companyGeneral;
}
public void setCompanyGeneral(String companyGeneral) {
this.companyGeneral = companyGeneral;
}
public List<Event> getDataForGeneralReports() {
return dataForGeneralReports;
}
public void setDataForGeneralReports(List<Event> dataForGeneralReports) {
this.dataForGeneralReports = dataForGeneralReports;
}
public List<String> getEventsGeneral() {
return eventsGeneral;
}
public void setEventsGeneral(List<String> eventsGeneral) {
this.eventsGeneral = eventsGeneral;
}
public List<String> getCompaniesGeneral() {
return companiesGeneral;
}
public void setCompaniesGeneral(List<String> companiesGeneral) {
this.companiesGeneral = companiesGeneral;
}
public DataForGeneralReportsAction() {
// TODO Auto-generated constructor stub
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
Connection con = null;
try {
con = new Database().Get_Connection();
// load companies
PreparedStatement ps = con
.prepareStatement("SELECT DISTINCT company_name FROM event");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
companiesGeneral.add(rs.getString("company_name"));
}
// load events
ps = con.prepareStatement("SELECT DISTINCT event_name FROM event");
rs = ps.executeQuery();
while (rs.next()) {
eventsGeneral.add(rs.getString("event_name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
}
@Override
public String execute() throws Exception {
Connection con = null;
try {
con = new Database().Get_Connection();
// load the table. The first time the table is loaded completely
String sql = "SELECT EVENT_ID, EVENT_NAME, COMPANY_NAME,EVENT_VENUE,TOTAL_AMOUNT,RECEIVED_AMOUNT,EVENT_TDS,BALANCE_AMOUNT,CHEQUE_DD_NO,"
+ "date_format(PAYMENT_DATE,'%d/%m/%Y') as dateAsPayment,EVENT_TIME "
+ "FROM event";
String where = "";
// if instead this action has been called from the JSP page,
// the result is filtered on event and company:
if (eventGeneral != null && companyGeneral != null) {
where = " WHERE event_name = ? AND company_name = ?";
}
// load companies
PreparedStatement ps = con.prepareStatement(sql + where);
if (where.length() > 0) {
ps.setString(1, eventGeneral);
ps.setString(2, companyGeneral);
}
dataForGeneralReports = new ArrayList<Event>();
ResultSet rs = ps.executeQuery();
int i, j = 0;
while (rs.next()) {
dataForGeneralReports.add(new Event(rs.getString("EVENT_ID"),
rs.getString("EVENT_NAME"), rs
.getString("COMPANY_NAME"), rs
.getString("EVENT_VENUE"), rs
.getString("EVENT_TIME"), rs
.getString("TOTAL_AMOUNT"), rs
.getString("RECEIVED_AMOUNT"), rs
.getString("CHEQUE_DD_NO"), rs
.getString("dateAsPayment"), rs
.getString("BALANCE_AMOUNT"), rs
.getString("EVENT_TDS")));
eventIdList.add(rs.getString("EVENT_ID"));
eventNameList.add(rs.getString("EVENT_NAME"));
companyNameList.add(rs.getString("COMPANY_NAME"));
eventVenueList.add(rs.getString("EVENT_VENUE"));
eventTimeList.add(rs.getString("EVENT_TIME"));
totalAmountList.add(rs.getString("TOTAL_AMOUNT"));
receivedAmountList.add(rs.getString("RECEIVED_AMOUNT"));
chequeDdList.add(rs.getString("CHEQUE_DD_NO"));
paymentDateList.add(rs.getString("dateAsPayment"));
eventTdsList.add(rs.getString("EVENT_TDS"));
balanceAmountList.add(rs.getString("BALANCE_AMOUNT"));
}
sessionMapGeneral.put("eventIdPdf", eventIdList);
sessionMapGeneral.put("eventNamePdf", eventNameList);
sessionMapGeneral.put("companyNamePdf", companyNameList);
sessionMapGeneral.put("eventVenuePdf", eventVenueList);
sessionMapGeneral.put("eventTimePdf", eventTimeList);
sessionMapGeneral.put("totalAmountPdf", totalAmountList);
sessionMapGeneral.put("receivedAmountPdf", receivedAmountList);
sessionMapGeneral.put("chequeDdPdf", chequeDdList);
sessionMapGeneral.put("paymentDatePdf", paymentDateList);
sessionMapGeneral.put("eventTdsPdf", eventTdsList);
sessionMapGeneral.put("balanceAmountPdf", balanceAmountList);
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
return SUCCESS;
}
public String generatePdfGeneral() throws Exception {
System.out.println(sessionMapGeneral.get("eventIdPdf"));
Document document = new Document(PageSize.A4_LANDSCAPE, 50, 50, 50, 50);
float[] columnWidths = { 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D:\GeneralReports.pdf"));
PdfPTable table = new PdfPTable(11);
table.setSpacingBefore(25);
table.setWidthPercentage(100);
table.setSpacingAfter(25);
table.setWidths(columnWidths);
PdfPCell c1 = new PdfPCell(new Phrase("Event ID "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Name "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Time"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Venue"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Company Name"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Total Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Received Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cheque/DD Number"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Payment Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event TDS"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Balance Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
PdfPCell cell = new PdfPCell();
List<String> list = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list) {
cell.addElement(new Paragraph(item));
}
PdfPCell cell1 = new PdfPCell();
List<String> list1 = (List<String>) sessionMapGeneral
.get("eventNamePdf");
for (String item : list1) {
cell1.addElement(new Paragraph(item));
}
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell();
List<String> list2 = (List<String>) sessionMapGeneral
.get("eventTimePdf");
for (String item : list2) {
cell2.addElement(new Paragraph(item));
}
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell();
List<String> list3 = (List<String>) sessionMapGeneral
.get("eventVenuePdf");
for (String item : list1) {
cell3.addElement(new Paragraph(item));
}
table.addCell(cell3);
PdfPCell cell4 = new PdfPCell();
List<String> list4 = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list4) {
cell4.addElement(new Paragraph(item));
}
table.addCell(cell4);
PdfPCell cell5 = new PdfPCell();
List<String> list5 = (List<String>) sessionMapGeneral
.get("companyNamePdf");
for (String item : list5) {
cell5.addElement(new Paragraph(item));
}
table.addCell(cell5);
PdfPCell cell6 = new PdfPCell();
List<String> list6 = (List<String>) sessionMapGeneral
.get("totalAmountPdf");
for (String item : list6) {
cell6.addElement(new Paragraph(item));
}
table.addCell(cell6);
PdfPCell cell7 = new PdfPCell();
List<String> list7 = (List<String>) sessionMapGeneral
.get("receivedAmountPdf");
for (String item : list7) {
cell7.addElement(new Paragraph(item));
}
table.addCell(cell7);
PdfPCell cell8 = new PdfPCell();
List<String> list8 = (List<String>) sessionMapGeneral
.get("chequeDdPdf");
for (String item : list8) {
cell8.addElement(new Paragraph(item));
}
table.addCell(cell8);
PdfPCell cell9 = new PdfPCell();
List<String> list9 = (List<String>) sessionMapGeneral
.get("paymentDatePdf");
for (String item : list9) {
cell9.addElement(new Paragraph(item));
}
table.addCell(cell9);
PdfPCell cell10 = new PdfPCell();
List<String> list10 = (List<String>) sessionMapGeneral
.get("eventTdsPdf");
for (String item : list10) {
cell10.addElement(new Paragraph(item));
}
table.addCell(cell10);
PdfPCell cell11 = new PdfPCell();
List<String> list11 = (List<String>) sessionMapGeneral
.get("balanceAmountPdf");
for (String item : list11) {
cell11.addElement(new Paragraph(item));
}
table.addCell(cell11);
document.open();
document.add(table);
document.close();
return "success";
}
public String generateGeneralXls() throws Exception {
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell(0).setCellValue("Event ID");
rowhead.createCell(1).setCellValue("Event Name");
rowhead.createCell(2).setCellValue("Event Time");
rowhead.createCell(3).setCellValue("Event Venue");
rowhead.createCell(4).setCellValue("Company Name");
rowhead.createCell(5).setCellValue("Total Amount");
rowhead.createCell(6).setCellValue("Received Amount");
rowhead.createCell(7).setCellValue("Payment Date");
rowhead.createCell(8).setCellValue("Cheque/DD No.");
rowhead.createCell(9).setCellValue("Event TDS");
rowhead.createCell(10).setCellValue("Balance Amount");
FileOutputStream fileOut;
fileOut = new FileOutputStream("D:\Samplmgjkm.xls");
// HSSFRow row1 = sheet.createRow((short) 1);
System.out.println(sessionMapGeneral.size());
for (int i = 1; i <= sessionMapGeneral.size(); i++) {
HSSFRow row1 = sheet.createRow((short) i);
row1.createCell(i-1).setCellValue(
sessionMapGeneral.get("eventIdPdf").toString());
row1.createCell(i).setCellValue(
sessionMapGeneral.get("eventNamePdf").toString());
}
/*
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
* row1.createCell(1).setCellValue(
* sessionMapGeneral.get("eventNamePdf").toString());
*/
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@Override
public void setSession(Map<String, Object> map) {
// TODO Auto-generated method stub
sessionMapGeneral = (SessionMap) map;
}
}
我编辑了我的代码,在其中我得到了字符串形式的结果,但所有记录都显示在单个单元格中。我希望新 cell.I 中的每条记录都附上它的外观图片。
试试这个:
FileOutputStream fileOut;
fileOut = new FileOutputStream("D:\Samplmgjkm.xls");
int nextRow = 1;
while(rs.next()){
HSSFRow r = sheet.getRow(nextRow);
if (r == null) {
r = sheet.createRow(nextRow);
}
HSSFCell c = r.getCell(1, Row.CREATE_NULL_AS_BLANK);
c.setCellValue(rs.getString(1));
HSSFCell c2 = r.getCell(2, Row.CREATE_NULL_AS_BLANK);
c2.setCellValue(rs.getString(2));
nextRow++;
}
我已经检查了你的代码并做了一些如下的编辑,所以请参考下面的代码,你会得到解决方案。
List<String> eventIdPdf = (List<String>) sessionMapGeneral.get("eventNamePdf");
List<String> eventNamePdf = (List<String>) sessionMapGeneral.get("eventNamePdf");
for (int i = 1; i <= sessionMapGeneral.size(); i++) {
HSSFRow row1 = sheet.createRow((short) i);
row1.createCell(i-1).setCellValue(eventIdPdf.get(i).toString());
row1.createCell(i).setCellValue(eventNamePdf.get(i).toString());
}
当您在一个变量(如 eventIdPdf)中获取字符串列表时,根据您在代码中的使用,您是在一个单元格中编写整个列表,因此您在一个单元格中获取整个列表,现在检查上面的方法,以便它会一一显示答案。
希望这会奏效..
你的第一个问题在这里:
row1.createCell(i-1).setCellValue(
sessionMapGeneral.get("eventIdPdf").toString());
因为 sessionMapGeneral.get("eventIdPdf").toString()
returns 整个 eventIdList,或者更准确地说,eventIdList 的所有值都是一个字符串,所以这个表达式的结果看起来像
"[value1, value2, ... , valueN]
"
这个(很长的)结果完全进入 row1.createCell(i-1)
。但实际上,Cell(i-1)
应该只得到 "value1"
。然后我们转到
你的第二个问题:
for (int i = 1; i <= sessionMapGeneral.size(); i++)
您只遍历 sessionMapGeneral
,但是遍历像 table 或矩阵这样的二维结构需要两个循环(嵌套循环)。请注意,您的 sessionMapGeneral's
元素是键值对,其中每个键都是一个字符串,例如"eventIdPdf"
,每个值都是一个列表,例如eventIdList
。因此,在第一个循环中,您应该访问 sessionMapGeneral
的值(列表)以获取存储在那里的数据。然后,在第二个循环中,您可以遍历在第一个循环中获得的列表,以获取每个数据项并将其设置到 table.
所需的单元格中
下一个小问题是,您无法通过索引访问 SessionMap's
元素,但有一种方法可以访问 "beat the system" :) - 这里是
你的解决方案:(请注意评论)
//modify your key-Strings in order to be able to sort them later easily
sessionMapGeneral.put("01_eventIdPdf", eventIdList);
sessionMapGeneral.put("02_eventNamePdf", eventNameList);
sessionMapGeneral.put("03_companyNamePdf", companyNameList);
sessionMapGeneral.put("04_eventVenuePdf", eventVenueList);
sessionMapGeneral.put("05_eventTimePdf", eventTimeList);
sessionMapGeneral.put("06_totalAmountPdf", totalAmountList);
sessionMapGeneral.put("07_receivedAmountPdf", receivedAmountList);
sessionMapGeneral.put("08_chequeDdPdf", chequeDdList);
sessionMapGeneral.put("09_paymentDatePdf", paymentDateList);
sessionMapGeneral.put("10_eventTdsPdf", eventTdsList);
sessionMapGeneral.put("11_balanceAmountPdf", balanceAmountList);
...
fileOut = new FileOutputStream("D:\Samplmgjkm.xls");
// get all the key-Strings from sessionMapGeneral and store them in an ArrayList in order to be able to access the elements by index
List<String> keys = new ArrayList<String>(sessionMapGeneral.keySet());
// and sort them, so that they suit to your "rowhead"
Collections.sort(keys);
// iterate through the keys and fill out the xls table column by column:
for (int key = 0; key < keys.size(); key++) {
//hold the current List from sessionMapGeneral for the current column in a variable
List<String> currentList = (List<String>) (sessionMapGeneral.get(keys.get(key)));
// iterate through the current ArrayList (column) and fill out the cells of this column row by row:
for (int row = 0; row < currentList.size(); row++) {
// hold the current row in a variable
HSSFRow currentRow = sheet.getRow(row+1);
// create a row only once to avoid deleting the data set earlier and set the value into the cell
if (currentRow == null) {
currentRow = sheet.createRow((short) row+1); // I don't understand why do you cast it to short. To save 16 bit per row?
currentRow.createCell(key).setCellValue(currentList.get(row));
} else {
currentRow.createCell(key).setCellValue(currentList.get(row));
}
}
// to make the width of columns looking well, add this line here:
sheet.autoSizeColumn(key);
}
第一个 poposal : 使用事件 object.You 构建一个你可以使用的事件对象列表并放入 SessionMap 中,如果你愿意,使用列表构建另一个列表事件)
这里假设事件对象中有 getter:
public String generateGeneralXls() throws Exception {
try {
String titles="Event ID,Event Name,Event Time,Event Venue,Company Name,Total Amount,Received Amount,Payment Date,Cheque/DD No,Event TDS,Balance Amount";
String[]arrTiltes=titles.split(",");
FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
int row=0;
HSSFRow rowTitle = worksheet.createRow(row);
// set titles
for(int i=0;i<arrTiltes.length;i++){
HSSFCell cellTitle=rowTitle.createCell(i);
cellTitle.setCellValue(arrTiltes[i]);
}
//setting values
row++;
for(Event dataValue:dataForGeneralReports){
HSSFRow rowValue = worksheet.createRow(row);
HSSFCell cell0 = rowValue.createCell(0);
cell0.setCellValue(dataValue.getEventID());
HSSFCell cell1 = rowValue.createCell(1);
cell1.setCellValue(dataValue.getEventName());
HSSFCell cell2 = rowValue.createCell(2);
cell2.setCellValue(dataValue.getEventTime());
HSSFCell cell3 = rowValue.createCell(3);
cell3.setCellValue(dataValue.getEventVenue());
HSSFCell cell4 = rowValue.createCell(4);
cell4.setCellValue(dataValue.getCompanyName());
HSSFCell cell5 = rowValue.createCell(5);
cell5.setCellValue(dataValue.getTotalAmount());
HSSFCell cell6 = rowValue.createCell(6);
cell6.setCellValue(dataValue.getReceivedAmount());
HSSFCell cell7 = rowValue.createCell(7);
cell7.setCellValue(dataValue.getPaymentDate());
HSSFCell cell8 = rowValue.createCell(8);
cell8.setCellValue(dataValue.getChequeDDNo());
HSSFCell cell9 = rowValue.createCell(9);
cell9.setCellValue(dataValue.getEventTDS());
HSSFCell cell10 = rowValue.createCell(10);
cell10.setCellValue(dataValue.getBalanceAmount());
row++;
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
您可以对 PDF 使用相同的想法。 请小心您的 PDF 代码,我怀疑您没有预期的结果。
您忘记在 table 上添加单元格(代码中的第一个单元格)
你有 12 个单元格,但是你声明了 11 个:
我建议你删除
的“阻止”
PdfPCell cell4
第二个提案:如果你真的想使用你的列表,那么遍历每个列表以获得每个列表中的第一个值,每个列表的第二个值等等..
示例假设所有列表具有相同的大小(来自您的结果集):
public String generateGeneralXlsOther() throws Exception {
try {
String titles="eventIdPdf,eventNamePdf,companyNamePdf,eventVenuePdf,eventTimePdf,totalAmountPdf,receivedAmountPdf,receivedAmountPdf,chequeDdPdf,paymentDatePdf,eventTdsPdf,balanceAmountPdf";
String[]arrTiltes=titles.split(",");
FileOutputStream fileOut = new FileOutputStream("poi-testOtehr.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
HSSFRow rowTitle = worksheet.createRow(0);
// set titles
for(int i=0;i<arrTiltes.length;i++){
HSSFCell cellTitle=rowTitle.createCell(i);
cellTitle.setCellValue(arrTiltes[i]);
}
int size=((List<String>) sessionMapGeneral.get(arrTiltes[0])).size();
for(int row=0;row<size;row++){
HSSFRow rowValue = worksheet.createRow(row+1);
int cell=0;
for(int i=0;i<arrTiltes.length;i++){
List<String> theList=(List<String>) sessionMapGeneral.get(arrTiltes[i]);
HSSFCell cell0 = rowValue.createCell(cell);
cell0.setCellValue(theList.get(row));
cell++;
}
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}