我是 Java 新手,我使用 itextpdf 作为我的输出。
现在我对这个问题很慌张。
我的问题是我想以 pdf 格式显示数据库中的结果集。
例如,我将设置一个包含 3 列的表格,
这些是来自数据库的结果集,
姓名
约翰
简
玛丽
桑尼
基尔
所以现在 itextpdf 中的输出应该是这样的
|__列名_|__列名_|_列名__|
|_____约翰_____|_____简_____|_____玛丽___|
|_____桑尼_____|_____基尔_____|___________|
我希望将结果插入每一列,但我不知道该怎么做。
有人吗?如果有人可以指导我,那就太好了。
Document document = new Document(PageSize.A4, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\PURCHASEORDER\\"+see+".pdf"));
document.open();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url+db, user, pass);
Statement st = conn.createStatement();
String zero = dates.getSelectedItem().toString();
String sql = "select name as hehe from names where servedate = '"+zero+"'";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
Rectangle react = writer.getPageSize();
PdfPTable table2 = new PdfPTable(new float[] { 3,3,3});
table2.setTotalWidth(527);
table2.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell cell = new PdfPCell(new Paragraph(""));
cell.setColspan(8);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBackgroundColor(BaseColor.GRAY);
table2.addCell(cell);
table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
while(rs.next()){
String v1 = rs.getString("hehe");
FontFactory.getFont(FontFactory.TIMES_BOLD,14,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
}
table2.setWidthPercentage(100);
document.add(table2);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
document.close();
String pdfFile="D:\\PURCHASEORDER\\"+see+".pdf";
File f = new File(pdfFile);
if (pdfFile.toString().endsWith(".pdf")) {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
} else {
//For cross platform use
Desktop desktop = Desktop.getDesktop();
desktop.open(f);
}
最佳答案
这是错误的:
while(rs.next()){
String v1 = rs.getString("hehe");
FontFactory.getFont(FontFactory.TIMES_BOLD,14,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
}
首先,让我们通过创建一个可以重用的 Font
对象来清理脏代码:
Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK);
其次,让我们解决您向每一行添加相同值的问题:
String v1;
while(rs.next()){
v1 = rs.getString("hehe");
table2.addCell(new Paragraph("TOTAL Number: " + v1, font));
}
最后,我们知道 iText 只会向表中添加完整行,而在您的示例中,Sonny 和 Kiel 只填充一行三行中的两个单元格,因此我们需要完成最后一行:
table2.completeRow();
现在我们可以将 table2
添加到文档中。
其他一些说明:
这是矫枉过正:
PdfPTable table2 = new PdfPTable(new float[] { 3,3,3});
如果您想要一个包含 3 列且每列具有相同宽度的列,则无需传递具有相等值的 float[]
,您可以这样做:
PdfPTable table2 = new PdfPTable(3);
这一行没有意义:
table2.setTotalWidth(527);
这没有意义,因为:
- 您没有锁定宽度,因此
527
的宽度将被忽略。您可以添加table.setLockedWidth(true);
- 您还有
table2.setWidthPercentage(100);
,它将宽度定义为可用宽度的 100%。
您必须选择其中之一:绝对宽度或相对宽度。两者都没有意义。请阅读How to define the width of a cell?
这是完全错误的:
cell.setColspan(8);
您正在定义一个包含3 列 的表格,因此您将colspan 设置为好像有8 列。在具有 3 列的表中,最大 colspan 为 3。
最后:
您是新手,并且正在使用 iText 5。为什么不使用 iText 7,因为您才刚刚开始? iText 7 是对 iText 5 的重写。它有一个不同的、更面向 future 的 API。表格说明here .
关于java - 结果集从数据库到itextpdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41214891/