- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
从数据库中获取记录后,我在一些列表中添加了数据,并为它们设置了一些 session 变量,以便我可以通过使用 session 的 get(key) 方法以另一种方法访问,我成功地这样做了。现在我想要的是我想通过在行中设置此列表值来创建动态记录,但我无法这样做。它创建文件但没有显示记录。下面是我的代码:
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;
}
}
我编辑了我的代码,在其中我得到了字符串形式的结果,但所有记录都显示在单个单元格中。我想要新单元格中的每条记录。我附上了它的外观图片。 没有错误显示。请帮我解决我的问题。
最佳答案
试试这个:
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++;
}
关于java - 如何创建动态记录行以使用列表创建 xls 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502950/
今天我在一个 Java 应用程序中看到了几种不同的加载文件的方法。 文件:/ 文件:// 文件:/// 这三个 URL 开头有什么区别?使用它们的首选方式是什么? 非常感谢 斯特凡 最佳答案 file
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我有一个 javascript 文件,并且在该方法中有一个“测试”方法,我喜欢调用 C# 函数。 c# 函数与 javascript 文件不在同一文件中。 它位于 .cs 文件中。那么我该如何管理 j
需要检查我使用的文件/目录的权限 //filePath = path of file/directory access denied by user ( in windows ) File fil
我在一个目录中有很多 java 文件,我想在我的 Intellij 项目中使用它。但是我不想每次开始一个新项目时都将 java 文件复制到我的项目中。 我知道我可以在 Visual Studio 和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有 3 个组件的 Twig 文件: 文件 1: {# content-here #} 文件 2: {{ title-here }} {# content-here #}
我得到了 mod_ldap.c 和 mod_authnz_ldap.c 文件。我需要使用 Linux 命令的 mod_ldap.so 和 mod_authnz_ldap.so 文件。 最佳答案 从 c
我想使用PIE在我的项目中使用 IE7。 但是我不明白的是,我只能在网络服务器上使用 .htc 文件吗? 我可以在没有网络服务器的情况下通过浏览器加载的本地页面中使用它吗? 我在 PIE 的文档中看到
我在 CI 管道中考虑这一点,我应该首先构建和测试我的应用程序,结果应该是一个 docker 镜像。 我想知道使用构建环境在构建服务器上构建然后运行测试是否更常见。也许为此使用构建脚本。最后只需将 j
using namespace std; struct WebSites { string siteName; int rank; string getSiteName() {
我是 Linux 新手,目前正在尝试使用 ginkgo USB-CAN 接口(interface) 的 API 编程功能。为了使用 C++ 对 API 进行编程,他们提供了库文件,其中包含三个带有 .
我刚学C语言,在实现一个程序时遇到了问题将 test.txt 文件作为程序的输入。 test.txt 文件的内容是: 1 30 30 40 50 60 2 40 30 50 60 60 3 30 20
如何连接两个tcpdump文件,使一个流量在文件中出现一个接一个?具体来说,我想“乘以”一个 tcpdump 文件,这样所有的 session 将一个接一个地按顺序重复几次。 最佳答案 mergeca
我有一个名为 input.MP4 的文件,它已损坏。它来自闭路电视摄像机。我什么都试过了,ffmpeg , VLC 转换,没有运气。但是,我使用了 mediainfo和 exiftool并提取以下信息
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
给定一个 gzip 文件 Z,如果我将其解压缩为 Z',有什么办法可以重新压缩它以恢复完全相同的 gzip 文件 Z?在粗略阅读了 DEFLATE 格式后,我猜不会,因为任何给定的文件都可能在 DEF
我必须从数据库向我的邮件 ID 发送一封带有附件的邮件。 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Adventure Works Admin
我有一个大的 M4B 文件和一个 CUE 文件。我想将其拆分为多个 M4B 文件,或将其拆分为多个 MP3 文件(以前首选)。 我想在命令行中执行此操作(OS X,但如果需要可以使用 Linux),而
快速提问。我有一个没有实现文件的类的项目。 然后在 AppDelegate 我有: #import "AppDelegate.h" #import "SomeClass.h" @interface A
我是一名优秀的程序员,十分优秀!