Excusez-moi. hey, je suis nouveau ici. maintenant je travaille avec mon projet dans Java Application avec Netbeans 7.2.1. maintenant je veux créer un rapport avec iTextPDF à partir de mes données dans la base de données. Donc, voici mes données d'exemple, ça ressemble à ceci:Java Création d'un rapport personnalisé PDF avec iText
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);
Maintenant, à partir de ces données, je veux créer une table dans mon fichier PDF comme ceci. est ici la sortie J'espère:
**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
est-ce que quelqu'un peut faire cela avec le code Java dans Netbeans? J'espère que tu m'aideras.
merci beaucoup d'avance. désolé si mon anglais est mauvais, et désolé si mes mots semblent impolis.
merci
ÉDITÉ: voici donc le script que j'ai fait jusqu'à présent, avec mon vrai projet. désolé pour le script désordonné.
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();
}
}
mais la sortie n'est pas ce que je pense.
Bonjour, bienvenue dans Stack Overflow. 'Y at-il quelqu'un peut faire cela avec du code Java dans Netbeans? Non. Ce n'est pas comment ça fonctionne. S'il vous plaît postez vos tentatives, et dites-nous ce que vous avez essayé, ce qui n'a pas fonctionné, pourquoi, ce que vous attendiez et ce qui s'est passé à la place. – BackSlash
Veuillez écrire ce que vous avez fait pour résoudre le problème par vous-même dans netbeans. Je vous remercie. –
Comme indice: vous voulez créer une structure tabulaire. Ainsi, recherchez des exemples d'utilisation iText créant des tables (avec la classe 'PdfPTable'). – mkl