2016-12-19 41 views
2

我是新来的java和我使用itextpdf作为我的输出。
现在我很害怕这个问题。

从数据库到itextpdf的结果集

我的问题是,我想显示PDF格式的数据库结果集与扭曲。

例如,我将设置一个表3列,
,这些都是从数据库中的结果,

名称
约翰

玛丽
桑尼
基尔

所以现在itextpdf中的输出应该像这样被查看

| __ columnname _ | __ columnname_ | _columnname__ |
| _____ John ______ | _____ Jane _____ | _____ Mary ____ |
| _____ Sonny _____ | _____ Kiel _____ | _____________ |


我想结果被插入每一列,我不知道如何做到这一点。

有人吗?如果有人能指导我,这将是很好的。

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); 
} 
+0

你看过使用java提供的PreparedStatement和ResultSet类吗? – Aaron

+0

是的,我已经知道他们,我不知道的唯一部分是,我如何显示他们,每个结果集将被插入每列。 –

回答

0

你为什么不使用ResultSetMetaData的选项来获得字段数连续,重复和标签或管道分隔每个值。在行的末尾追加换行符。 hasNext结果集将会遍历所有执行相同操作的记录。 将整个事物保存在列表中。使用该列表写入PDF。

OpenCsv | SQL | Writes only the header and not the table content

1

这是错误的:

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的只增加完整行的表,在你的榜样,桑尼和基尔只能填写两个单元中的三个一排,因此,我们需要完成最后一行:

table2.completeRow(); 

现在我们可以将table2添加到文档中。

其他一些言论:

这是矫枉过正:

PdfPTable table2 = new PdfPTable(new float[] { 3,3,3}); 

如果你想与3列,每个具有相同的宽度一列,这是没有必要通过用相等值一float[] ,你可以做:

PdfPTable table2 = new PdfPTable(3); 

此行没有任何意义:

table2.setTotalWidth(527); 

这是没有意义的,因为:

  1. 你不锁的宽度,因此的527宽度将被忽略。您可以添加table.setLockedWidth(true);
  2. 您还有table2.setWidthPercentage(100);,它将宽度定义为可用宽度的100%。

你必须选择一个或另一个:绝对宽度或相对宽度。两者都没有意义。请阅读How to define the width of a cell?

这是完全错误

cell.setColspan(8); 

要定义3列表,因此你设定的合并单元格,就好像有8列。在与3列的表,最大列跨度为3

最后:

你是一个新手,你利用iText 5.为什么不使用iText的7,因为你刚刚开始? iText 7是对iText 5的改写。它具有不同的,更具前瞻性的API。表格解释为here

+0

好的,非常感谢你,我会试试这个。对不起,我真的很新鲜。现在正在研究itext7。 –