2015-10-04 35 views
-2

对不起。 嘿,我是新来的。 现在我正在使用Netbeans 7.2.1在Java应用程序中处理我的项目。 现在我想用我的数据库中的数据创建iTextPDF的报告。Java使用iText制作自定义报告PDF

所以这里是我的示例数据,它看起来像这样。

CREATE TABLE `smartphone` (
`No` int(2) NOT NULL AUTO_INCREMENT, 
`merk` varchar(20) NOT NULL, 
`type` varchar(40) NOT NULL, 
`price` int(10) NOT NULL, 
PRIMARY KEY (`No`) 
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; 

INSERT INTO `smartphone` (`No`,`merk`,`type`,`price`) VALUES 
(1,'samsung','Galaxy S5',600), 
(2,'Xiaomi','Redmi 1s',150), 
(3,'LG','G3',450), 
(4,'Samsung','Galaxy S6',1000), 
(5,'Xiaomi','Mi4i',250), 
(6,'Xiaomi','Redmi Note',200), 
(7,'Apple','iPhone 5s',500), 
(8,'Apple','iPhone 4s',250); 

现在,根据这些数据,我想这样我的PDF文件创建一个表。 这里就是我希望的输出:

**No** | **merk/type** | **Price** 
    1  **Samsung** 
      Galaxy S5    600 
      Galaxy S6    1000 
    2  **Xiaomi** 
      Redmi 1s    150 
      Redmi Mi4i   250 
      Redmi Note   200 
    3  **LG** 
      G3     450 
    4  **Apple** 
      iPhone 5s    500 
      iPhone 4s    300 

有任何人都可以在Netbeans的Java代码做这个?希望你能帮助我。

非常感谢你提前。对不起,如果我的英语不好,抱歉,如果我的话看起来不礼貌。

谢谢

编辑: 所以这里是我做了,到目前为止,我真正的项目的脚本。对不整洁的脚本感到抱歉。

import java.io.FileOutputStream; 
import java.io.*; 
import java.util.*; 
import java.sql.*; 
import com.itextpdf.text.*; 
import com.itextpdf.text.Font.FontFamily; 
import com.itextpdf.text.pdf.*; 

public class jdbc_pdf_report { 

public static void main(String[] args) throws Exception { 

    /* Create Connection objects */ 

    Connection conn = Config.getConfig(); 
    Statement stmt = conn.createStatement(); 
    /* Define the SQL query */ 
    ResultSet query_set = stmt.executeQuery("SELECT * FROM DATAUSULAN WHERE TAHUN=2016 AND VALIDASI='1'"); 
    /* Step-2: Initialize PDF documents - logical objects */ 
    Document my_pdf_report = new Document(PageSize.LEGAL.rotate()); 
    PdfWriter.getInstance(my_pdf_report, new FileOutputStream("D:/pdf_report_from_sql_using_java.pdf")); 
    my_pdf_report.open(); 
    my_pdf_report.add(new Paragraph("Laporan Usulan Kecamatan")); 
    //we have four columns in our table 
    float[] columnWidths = {1.5f, 7f, 5f, 5f, 3f, 4f, 5f, 4f, 4f, 4f}; 
    PdfPTable my_report_table = new PdfPTable(columnWidths); 
    my_report_table.setSpacingBefore(10); 
    my_report_table.setWidthPercentage(90f); 
    //create a cell object 
    PdfPCell table_cell; 
    Font fontHeader = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, BaseColor.BLACK); 
    Font font = new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL, BaseColor.BLACK); 
    // create header cell 
    for (int i = 1; i <= 10; i++) { 
     table_cell = new PdfPCell(new Phrase("" + i, font)); 
     my_report_table.addCell(table_cell); 
    } 
    GreekList greekList = new GreekList(); 
    PdfPTable nestedTable = new PdfPTable(1); 
    while (query_set.next()) { 

     String no_kegiatan = query_set.getString("NO_KEGIATAN"); 
     table_cell = new PdfPCell(new Phrase(no_kegiatan, font));; 
     Phrase phrase = new Phrase(); 
     my_report_table.addCell(table_cell); 
     String urusan = query_set.getString("URUSAN"); 
     table_cell = new PdfPCell(new Phrase(urusan, font)); 
     table_cell.setColspan(9); 
     my_report_table.addCell(table_cell); 
     String program = query_set.getString("PROGRAM"); 
     table_cell = new PdfPCell(new Phrase(urusan+"\n "+program, font)); 
     my_report_table.addCell(table_cell); 
     String nama_kegiatan = query_set.getString("NAMA_KEGIATAN"); 
     table_cell = new PdfPCell(new Phrase(nama_kegiatan, font)); 
     my_report_table.addCell(table_cell); 
     String volume = query_set.getString("VOLUME"); 
     table_cell = new PdfPCell(new Phrase(volume, font)); 
     my_report_table.addCell(table_cell); 
     String lokasi_kegiatan = query_set.getString("LOKASI_KEGIATAN"); 
     table_cell = new PdfPCell(new Phrase(lokasi_kegiatan, font)); 
     my_report_table.addCell(table_cell); 
     String jenis_kegiatan = query_set.getString("JENIS_KEGIATAN"); 
     table_cell = new PdfPCell(new Phrase(jenis_kegiatan, font)); 
     my_report_table.addCell(table_cell); 
     String apbd_kab = query_set.getString("APBD_KAB"); 
     table_cell = new PdfPCell(new Phrase(apbd_kab, font)); 
     my_report_table.addCell(table_cell); 
     String apbd_prov = query_set.getString("APBD_PROV"); 
     table_cell = new PdfPCell(new Phrase(apbd_prov, font)); 
     my_report_table.addCell(table_cell); 
     String apbn = query_set.getString("APBN"); 
     table_cell = new PdfPCell(new Phrase(apbn, font)); 
     my_report_table.addCell(table_cell); 
     String tahun = query_set.getString("TAHUN"); 
     table_cell = new PdfPCell(new Phrase(tahun, font)); 
     my_report_table.addCell(table_cell); 
    } 
    /* Attach report table to PDF */ 
    my_pdf_report.add(my_report_table); 
    my_pdf_report.close(); 

    /* Close all DB related objects */ 
    query_set.close(); 
    stmt.close(); 
    conn.close(); 

    } 

}

但输出是不是我所期望。

+3

嗨,欢迎来到Stack Overflow。 “有没有人可以在Netbeans中使用Java代码做这件事?”不,这不是SO的工作原理。请张贴您的尝试,并告诉我们您尝试了什么,什么不起作用,为什么,您期望什么以及发生了什么。 – BackSlash

+0

请写下你在NetBeans中自己解决问题的方法。谢谢。 –

+1

作为提示:您想创建一个表格结构。因此,查找创建表的iText使用示例(使用“PdfPTable”类)。 – mkl

回答

0

在Netbeans中,选择“New Class”并开始编写代码。使用标准JDBC代码访问数据库(例如,执行查询SELECT * FROM smartphone),然后将记录添加到PdfPTable

对于SQL代码,例如见Java Oracle jdbc SELECT statement

对于PDF的代码,你可以在这里获得灵感:ArrayToTable

public void createPdf(String dest) throws IOException, DocumentException { 
    Document document = new Document(); 
    PdfWriter.getInstance(document, new FileOutputStream(dest)); 
    document.open(); 
    PdfPTable table = new PdfPTable(4); 
    table.setWidthPercentage(100); 
    List<List<String>> dataset = getData(); 
    for (List<String> record : dataset) { 
     for (String field : record) { 
      table.addCell(field); 
     } 
    } 
    document.add(table); 
    document.close(); 
} 

如果你想使用这个片段,你需要写getData()方法,它会返回一个List(您的完整结果集)List对象(结果集中的记录)String值(您需要将每个字段转换为String)。每个开发人员都知道如何做到这一点,但如果您不这样做,请不要改变您的问题或不使用评论,但会发布带标题的新问题:如何将ResultSet转换为List<List<String>>