Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (seleccionar.showDialog(null, "Exportar Archivo") == JFileChooser.APPROVE_OPTION) {
- // Exportar a PDF
- try {
- // We create the document and set the file name.
- // Creamos el documento e indicamos el nombre del fichero.
- Document document = new Document();
- try {
- PdfWriter.getInstance(document, new FileOutputStream(seleccionar.getSelectedFile() + ".pdf"));
- } catch (FileNotFoundException fileNotFoundException) {
- }
- document.open();
- // First page (Primera página)
- Anchor anchor = new Anchor("Table export to PDF (Exportamos la tabla a PDF)", categoryFont);
- anchor.setName("Table export to PDF (Exportamos la tabla a PDF)");
- // Second parameter is the number of the chapter (El segundo parámetro es el número del capítulo).
- Chapter catPart = new Chapter(new Paragraph(anchor), 1);
- Paragraph subPara = new Paragraph("", subCategoryFont);
- Section subCatPart = catPart.addSection(subPara);
- subCatPart.add(new Paragraph(""));
- // Create the table (Creamos la tabla)
- PdfPTable table = new PdfPTable(jTable1.getColumnCount());
- // Now we fill the rows of the PdfPTable (Ahora llenamos las filas de PdfPTable)
- PdfPCell columnHeader;
- // Fill table columns header
- // Rellenamos las cabeceras de las columnas de la tabla.
- for (int column = 0; column < jTable1.getColumnCount(); column++) {
- columnHeader = new PdfPCell(new Phrase(jTable1.getColumnName(column)));
- columnHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
- table.addCell(columnHeader);
- }
- table.setHeaderRows(1);
- // Fill table rows (rellenamos las filas de la tabla).
- for (int row = 0; row < jTable1.getRowCount(); row++) {
- for (int column = 0; column < jTable1.getColumnCount(); column++) {
- table.addCell(jTable1.getValueAt(row, column).toString());
- }
- }
- subCatPart.add(table);
- document.add(catPart);
- document.close();
- JOptionPane.showMessageDialog(this.jTable1, "Your PDF file has been generated!(¡Se ha generado tu hoja PDF!)",
- "RESULTADO", JOptionPane.INFORMATION_MESSAGE);
- } catch (DocumentException documentException) {
- System.out.println("The file not exists (Se ha producido un error al generar un documento): " + documentException);
- JOptionPane.showMessageDialog(this.jTable1, "The file not exists (Se ha producido un error al generar un documento): " + documentException,
- "ERROR", JOptionPane.ERROR_MESSAGE);
- }
- // Exportar a EXCEL
- int cantFila = jTable1.getRowCount();
- int cantColumna = jTable1.getColumnCount();
- Workbook wb;
- wb = new XSSFWorkbook();
- Sheet hoja = wb.createSheet(" ");
- try {
- for (int i = 0; i < cantFila; i++) {
- Row fila = hoja.createRow(i + 1);
- for (int j = 0; j < cantColumna; j++) {
- Cell celda = fila.createCell(j);
- if (i == -1) {
- celda.setCellValue(String.valueOf(jTable1.getColumnName(j)));
- }else {
- celda.setCellValue(String.valueOf(jTable1.getValueAt(i, j)));
- }
- wb.write(new FileOutputStream(archivo + ".xlsx"));
- }
- }
- JOptionPane.showMessageDialog(null, "Exportacion exitosa");
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Vuelve a intentarlo");
- }
- } else {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement