-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportData.java
More file actions
159 lines (144 loc) · 6.46 KB
/
Copy pathExportData.java
File metadata and controls
159 lines (144 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ModelClass;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.TableModel;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
*
* @author AJpag
*/
public class ExportData extends javax.swing.JFrame {
private File archivo;
private JFileChooser abrirArchivo = new JFileChooser();
private TableModel tableModel;
private static final Font categoryFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
private static final Font subcategoryFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
public void exportarDataExcel(JTable table, String[] titles, int[] colum, String pagina) {
abrirArchivo.setFileFilter(new FileNameExtensionFilter("Excel", "xls"));
abrirArchivo.setDialogTitle("Especifique un archivo para guardar");
int respuesta = abrirArchivo.showSaveDialog(this);
WritableSheet excelSheet = null;
WritableWorkbook excelWorkbook = null;
if (respuesta == JFileChooser.APPROVE_OPTION) {
archivo = new File(abrirArchivo.getSelectedFile().getPath() + ".xls");
try {
excelWorkbook = Workbook.createWorkbook(archivo);
excelWorkbook.createSheet(pagina, 0);
excelSheet = excelWorkbook.getSheet(0);
tableModel = table.getModel();
int cols = tableModel.getColumnCount();
int filas = tableModel.getRowCount();
int fila = 1;
int column = 0;
for (int i = 0; i < titles.length; i++) {
Label title = new Label(i, 0, titles[i]);
excelSheet.addCell(title);
}
for (int i = 0; i < filas; i++) {
for (int j = 0; j < cols; j++) {
boolean valor = true;
for (int k = 0; k < colum.length; k++) {
if (j == colum[k]) {
valor = false;
}
}
if (valor) {
Label datos = new Label(column, fila, tableModel.getValueAt(i, j).toString());
excelSheet.addCell(datos);
column++;
}
}
fila++;
column = 0;
}
excelWorkbook.write();
excelWorkbook.close();
} catch (IOException ex) {
} catch (WriteException ex) {
}
}
}
public void exportarDataPdf(JTable tables, String[] titles, int[] colum, String pagina) {
abrirArchivo.setFileFilter(new FileNameExtensionFilter("PDF", "pdf"));
abrirArchivo.setDialogTitle("Especifique un archivo para guardar");
int respuesta = abrirArchivo.showSaveDialog(this);
if (respuesta == JFileChooser.APPROVE_OPTION) {
archivo = new File(abrirArchivo.getSelectedFile().getPath() + ".pdf");
// Creamos el documento e indicamos el nombre del fichero.
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(archivo));
document.open();
// Añadimos los metadatos del PDF
document.addKeywords("Java, PDF");
document.addAuthor("Código PDHN");
document.addCreator("Código PDHN");
// Primera página
Anchor anchor = new Anchor(pagina, categoryFont);
// El segundo parámetro es el número del capítulo.
Chapter catPart = new Chapter(new Paragraph(anchor), 1);
Paragraph subPara = new Paragraph("Realizado por PDHN", subcategoryFont);
Section subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("Lista de productos"));
// Creamos la tabla
PdfPTable table = new PdfPTable(titles.length);
// Ahora llenamos las filas de Table
PdfPCell columnHeader;
// Rellenamos las cabeceras de las columnas de la tabla.
for (int column = 0; column < titles.length; column++){
columnHeader = new PdfPCell(new Phrase(titles[column]));
table.addCell(columnHeader);
}
table.setHeaderRows(1);
// rellenamos las filas de la tabla.
for (int row = 0; row < tables.getRowCount(); row++){
for (int column = 0; column < tables.getColumnCount(); column++){
boolean valor = true;
for (int a = 0; a < colum.length; a++) {
if (column == colum[a]) {
valor = false;
}
}
if (valor) {
table.addCell(tables.getValueAt(row, column).toString());
}
}
}
subCatPart.add(table);
document.add(catPart);
document.close();
} catch (FileNotFoundException ex) {
System.out.println("No se encontró el fichero para generar el pdf" + ex.getMessage());
} catch (DocumentException ex) {
System.out.println("No se encontró el fichero para generar el pdf" + ex.getMessage());
}
}
}
}