· 9 years ago · Sep 30, 2016, 02:00 AM
1seleccionar.addChoosableFileFilter(new FileNameExtensionFilter("Documento PDF (*.pdf)", "pdf"));
2 seleccionar.addChoosableFileFilter(new FileNameExtensionFilter("Libro de Excel (*.xlsx)", "xlsx"));
3 seleccionar.setAcceptAllFileFilterUsed(false);
4
5 if (seleccionar.showDialog(null, "Exportar Archivo") == JFileChooser.APPROVE_OPTION) {
6
7 String nameExtension = seleccionar.getFileFilter().getDescription();
8
9 // Exportar a PDF
10 if (nameExtension.equals("Documento PDF (*.pdf)")) {
11 try {
12 // We create the document and set the file name.
13 // Creamos el documento e indicamos el nombre del fichero.
14 Document document = new Document();
15 try {
16 PdfWriter.getInstance(document, new FileOutputStream(seleccionar.getSelectedFile() + ".pdf"));
17 } catch (FileNotFoundException fileNotFoundException) {
18
19 }
20 document.open();
21
22 // First page (Primera página)
23 Anchor anchor = new Anchor();
24 anchor.setName("");
25
26 // Second parameter is the number of the chapter (El segundo parámetro es el número del capÃÂtulo).
27 Chapter catPart = new Chapter(new Paragraph(anchor), 1);
28
29 Paragraph subPara = new Paragraph("");
30 Section subCatPart = catPart.addSection(subPara);
31 subCatPart.add(new Paragraph(""));
32
33 // Create the table (Creamos la tabla)
34 PdfPTable table = new PdfPTable(jTable1.getColumnCount());
35
36 // Now we fill the rows of the PdfPTable (Ahora llenamos las filas de PdfPTable)
37 PdfPCell columnHeader;
38 // Fill table columns header
39 // Rellenamos las cabeceras de las columnas de la tabla.
40 for (int column = 0; column < jTable1.getColumnCount(); column++) {
41 columnHeader = new PdfPCell(new Phrase(jTable1.getColumnName(column)));
42 columnHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
43 table.addCell(columnHeader);
44 }
45 table.setHeaderRows(1);
46 // Fill table rows (rellenamos las filas de la tabla).
47 for (int row = 0; row < jTable1.getRowCount(); row++) {
48 for (int column = 0; column < jTable1.getColumnCount(); column++) {
49 table.addCell(jTable1.getValueAt(row, column).toString());
50 }
51 }
52 subCatPart.add(table);
53
54 document.add(catPart);
55
56 document.close();
57 JOptionPane.showMessageDialog(this.jTable1, "Your PDF file has been generated!(¡Se ha generado tu hoja PDF!)",
58 "RESULTADO", JOptionPane.INFORMATION_MESSAGE);
59 } catch (DocumentException documentException) {
60 System.out.println("The file not exists (Se ha producido un error al generar un documento): " + documentException);
61 JOptionPane.showMessageDialog(this.jTable1, "The file not exists (Se ha producido un error al generar un documento): " + documentException,
62 "ERROR", JOptionPane.ERROR_MESSAGE);
63 }
64 }
65 // Exportar a EXCEL
66 if (nameExtension.equals("Libro de Excel (*.xlsx)")) {
67 File archivo;
68 archivo = seleccionar.getSelectedFile();
69 int cantFila = jTable1.getRowCount();
70 int cantColumna = jTable1.getColumnCount();
71 Workbook wb;
72 wb = new XSSFWorkbook();
73 Sheet hoja = wb.createSheet(" ");
74
75 try {
76 for (int i = 0; i < cantFila; i++) {
77 Row fila = hoja.createRow(i + 1);
78 for (int j = 0; j < cantColumna; j++) {
79 Cell celda = fila.createCell(j);
80 if (i == -1) {
81 celda.setCellValue(String.valueOf(jTable1.getColumnName(j)));
82 } else {
83 celda.setCellValue(String.valueOf(jTable1.getValueAt(i, j)));
84 }
85 wb.write(new FileOutputStream(archivo + ".xlsx"));
86 }
87 }
88 JOptionPane.showMessageDialog(null, "Exportacion exitosa");
89 } catch (Exception e) {
90 JOptionPane.showMessageDialog(null, "Vuelve a intentarlo");
91 }
92 }
93 }
94
95boolean active=false;
96 while(!active)
97 {
98 if (seleccionar.showDialog(null, "Exportar Archivo") == JFileChooser.APPROVE_OPTION) {
99 String extension = seleccionar.getSelectedFile().getAbsolutePath().split("\.(?=[^\.]+$)")[1];
100 switch(extension){
101 case "pdf" : System.out.println("EXPORT PDF");active=true;break;
102 case "xlsx" : System.out.println("EXPORT EXCEL");active=true;break;
103 default:
104 active = JOptionPane.showConfirmDialog(null, "EXTENSIÓN NO VÃLIDA , DESEA SELECCIONAR UNA EXTENSIÓN DIFERENTE?"
105 ,"MENSAJE ! ", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION;
106 }
107 }
108 else
109 active = JOptionPane.showConfirmDialog(null, "NO SELECCIONO UN ARCHIVO, DESEA SELECCIONAR?"
110 ,"MENSAJE ! ", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION;
111 }