· 9 years ago · Sep 29, 2016, 08:24 PM
1if (seleccionar.showDialog(null, "Exportar Archivo") == JFileChooser.APPROVE_OPTION) {
2
3 // Exportar a PDF
4 try {
5 // We create the document and set the file name.
6 // Creamos el documento e indicamos el nombre del fichero.
7 Document document = new Document();
8 try {
9 PdfWriter.getInstance(document, new FileOutputStream(seleccionar.getSelectedFile() + ".pdf"));
10 } catch (FileNotFoundException fileNotFoundException) {
11
12 }
13 document.open();
14
15 // First page (Primera página)
16 Anchor anchor = new Anchor("Table export to PDF (Exportamos la tabla a PDF)", categoryFont);
17 anchor.setName("Table export to PDF (Exportamos la tabla a PDF)");
18
19 // Second parameter is the number of the chapter (El segundo parámetro es el número del capÃtulo).
20 Chapter catPart = new Chapter(new Paragraph(anchor), 1);
21
22 Paragraph subPara = new Paragraph("", subCategoryFont);
23 Section subCatPart = catPart.addSection(subPara);
24 subCatPart.add(new Paragraph(""));
25
26 // Create the table (Creamos la tabla)
27 PdfPTable table = new PdfPTable(jTable1.getColumnCount());
28
29 // Now we fill the rows of the PdfPTable (Ahora llenamos las filas de PdfPTable)
30 PdfPCell columnHeader;
31 // Fill table columns header
32 // Rellenamos las cabeceras de las columnas de la tabla.
33 for (int column = 0; column < jTable1.getColumnCount(); column++) {
34 columnHeader = new PdfPCell(new Phrase(jTable1.getColumnName(column)));
35 columnHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
36 table.addCell(columnHeader);
37 }
38 table.setHeaderRows(1);
39 // Fill table rows (rellenamos las filas de la tabla).
40 for (int row = 0; row < jTable1.getRowCount(); row++) {
41 for (int column = 0; column < jTable1.getColumnCount(); column++) {
42 table.addCell(jTable1.getValueAt(row, column).toString());
43 }
44 }
45 subCatPart.add(table);
46
47 document.add(catPart);
48
49 document.close();
50 JOptionPane.showMessageDialog(this.jTable1, "Your PDF file has been generated!(¡Se ha generado tu hoja PDF!)",
51 "RESULTADO", JOptionPane.INFORMATION_MESSAGE);
52 } catch (DocumentException documentException) {
53 System.out.println("The file not exists (Se ha producido un error al generar un documento): " + documentException);
54 JOptionPane.showMessageDialog(this.jTable1, "The file not exists (Se ha producido un error al generar un documento): " + documentException,
55 "ERROR", JOptionPane.ERROR_MESSAGE);
56 }
57
58 // Exportar a EXCEL
59 int cantFila = jTable1.getRowCount();
60 int cantColumna = jTable1.getColumnCount();
61 Workbook wb;
62 wb = new XSSFWorkbook();
63 Sheet hoja = wb.createSheet(" ");
64
65 try {
66 for (int i = 0; i < cantFila; i++) {
67 Row fila = hoja.createRow(i + 1);
68 for (int j = 0; j < cantColumna; j++) {
69 Cell celda = fila.createCell(j);
70 if (i == -1) {
71 celda.setCellValue(String.valueOf(jTable1.getColumnName(j)));
72 }else {
73 celda.setCellValue(String.valueOf(jTable1.getValueAt(i, j)));
74 }
75 wb.write(new FileOutputStream(archivo + ".xlsx"));
76 }
77 }
78 JOptionPane.showMessageDialog(null, "Exportacion exitosa");
79 } catch (Exception e) {
80 JOptionPane.showMessageDialog(null, "Vuelve a intentarlo");
81 }
82 } else {
83 }