Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d1e50c3e

Přidáno uživatelem Michal Linha před více než 4 roky(ů)

re #7782 Created new class ExporterPDF with export to PDF functionality and renamed the former Exporter class to ExporterXLSX, changed the version of a library used for export to XLSX functionality implementation

Zobrazit rozdíly:

pom.xml
105 105
		<dependency>
106 106
			<groupId>org.apache.poi</groupId>
107 107
			<artifactId>poi-ooxml</artifactId>
108
			<version>3.17</version>
108
			<version>4.1.2</version>
109
		</dependency>
110

  
111
		<dependency>
112
			<groupId>com.github.librepdf</groupId>
113
			<artifactId>openpdf</artifactId>
114
			<version>1.3.14</version>
109 115
		</dependency>
110 116
	</dependencies>
111 117

  
src/main/java/vldc/aswi/utils/Exporter.java
1
package vldc.aswi.utils;
2

  
3
import org.apache.poi.ss.formula.functions.Column;
4
import org.apache.poi.ss.usermodel.*;
5
import org.apache.poi.ss.util.CellRangeAddress;
6
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
7

  
8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11

  
12
/**
13
 * Class used for exporting data to different output files
14
 */
15
public class Exporter {
16

  
17
    /**
18
     * !!!DUMMY!!!
19
     * Exports data to .xlsx file
20
     * @param tableName name of the table
21
     */
22
    public static void exportToExcel(String tableName) {
23

  
24
        //temporary dummy implementation, used for testing
25
        String[] columns = new String[] {"studuje", "nestuduje"};
26
        String[] rows = new String[] {"fav", "fst"};
27
        String[] data = new String[] {"1", "2", "3", "4"};
28

  
29
        Workbook workbook = new XSSFWorkbook();
30

  
31
        Sheet sheet = workbook.createSheet(tableName);
32

  
33
        Font colRowNameFont = workbook.createFont();
34
        colRowNameFont.setBold(true);
35
        colRowNameFont.setFontHeightInPoints((short) 16);
36
        colRowNameFont.setColor(IndexedColors.BLACK.getIndex());
37

  
38
        Font dataFont = workbook.createFont();
39
        dataFont.setBold(false);
40
        dataFont.setFontHeightInPoints((short) 12);
41
        dataFont.setColor(IndexedColors.BLACK.getIndex());
42

  
43
        CellStyle colRowStyle = workbook.createCellStyle();
44
        colRowStyle.setFont(colRowNameFont);
45

  
46
        CellStyle dataStyle = workbook.createCellStyle();
47
        dataStyle.setFont(dataFont);
48

  
49
        Row descRow = sheet.createRow(0);
50

  
51
        for (int i = 0; i < 2; i++) {
52
            Cell cell = descRow.createCell(i + 1);
53
            cell.setCellValue(columns[i]);
54
            cell.setCellStyle(colRowStyle);
55
        }
56

  
57
        for (int i = 0; i < 2; i++) {
58
            Row row = sheet.createRow(i + 1);
59
            Cell nameCell = row.createCell(0);
60
            nameCell.setCellValue(rows[i]);
61
            nameCell.setCellStyle(colRowStyle);
62

  
63
            Cell cell1 = row.createCell(1);
64
            cell1.setCellValue(data[i + i + 1]);
65
            cell1.setCellStyle(dataStyle);
66

  
67
            Cell cell2 = row.createCell(2);
68
            cell2.setCellValue(data[i + i + 1]);
69
            cell2.setCellStyle(dataStyle);
70
        }
71

  
72
        for (int i = 0; i < 3; i++) {
73
            sheet.autoSizeColumn(i);
74
        }
75

  
76
        //merge of columns, will be used for column headers
77
        //sheet.addMergedRegion(new CellRangeAddress(0,0,1,2));
78

  
79
        FileOutputStream streamOut = null;
80

  
81
        try {
82
            streamOut = new FileOutputStream("out.xlsx");
83
        } catch (FileNotFoundException e) {
84
            e.printStackTrace();
85
        }
86

  
87
        try {
88
            workbook.write(streamOut);
89
        } catch (IOException e) {
90
            e.printStackTrace();
91
        }
92

  
93
        try {
94
            if (streamOut != null) {
95
                streamOut.close();
96
            }
97
        } catch (IOException e) {
98
            e.printStackTrace();
99
        }
100

  
101
        try {
102
            workbook.close();
103
        } catch (IOException e) {
104
            e.printStackTrace();
105
        }
106
    }
107
}
src/main/java/vldc/aswi/utils/ExporterPDF.java
1
package vldc.aswi.utils;
2

  
3
import com.lowagie.text.Cell;
4
import com.lowagie.text.Document;
5
import com.lowagie.text.Font;
6
import com.lowagie.text.Phrase;
7
import com.lowagie.text.pdf.PdfWriter;
8

  
9
import java.io.FileNotFoundException;
10
import java.io.FileOutputStream;
11

  
12
/**
13
 * Class used for exporting data to PDF files
14
 */
15
public class ExporterPDF {
16

  
17
    /**
18
     * !!!DUMMY!!!
19
     * Exports data to .pdf file
20
     * @param tableName name of the table
21
     */
22
    public static void export(String tableName) throws FileNotFoundException {
23
        Document doc = new Document();
24

  
25
        PdfWriter.getInstance(doc, new FileOutputStream("out.pdf"));
26

  
27
        Font font = new Font(com.lowagie.text.Font.COURIER, 14);
28
        com.lowagie.text.Table table = new com.lowagie.text.Table(3);
29

  
30
        Cell cell = new Cell(new Phrase(tableName, font));
31
        cell.setColspan(2);
32
        cell.setHeader(true);
33

  
34
        Cell cell2 = new Cell(new Phrase("SUM", font));
35
        cell2.setColspan(1);
36
        cell2.setHeader(true);
37

  
38
        table.addCell(cell);
39
        table.addCell(cell2);
40

  
41
        table.addCell(new Phrase("Val1", font));
42
        table.addCell(new Phrase("Val2", font));
43
        table.addCell(new Phrase("", font));
44

  
45
        table.addCell("1");
46
        table.addCell("2");
47
        table.addCell("3");
48

  
49
        table.addCell("4");
50
        table.addCell("5");
51
        table.addCell("9");
52

  
53
        doc.open();
54
        doc.add(table);
55

  
56
        doc.close();
57
    }
58
}
src/main/java/vldc/aswi/utils/ExporterXLSX.java
1
package vldc.aswi.utils;
2

  
3
import org.apache.poi.ss.usermodel.*;
4
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
5

  
6
import java.io.FileNotFoundException;
7
import java.io.FileOutputStream;
8
import java.io.IOException;
9

  
10
/**
11
 * Class used for exporting data to XLSX
12
 */
13
public class ExporterXLSX {
14

  
15
    /**
16
     * !!!DUMMY!!!
17
     * Exports data to .xlsx file
18
     * @param tableName name of the table
19
     */
20
    public static void export(String tableName) {
21

  
22
        //temporary dummy implementation, used for testing
23
        String[] columns = new String[] {"studuje", "nestuduje"};
24
        String[] rows = new String[] {"fav", "fst"};
25
        String[] data = new String[] {"1", "2", "3", "4"};
26

  
27
        Workbook workbook = new XSSFWorkbook();
28

  
29
        Sheet sheet = workbook.createSheet(tableName);
30

  
31
        Font colRowNameFont = workbook.createFont();
32
        colRowNameFont.setBold(true);
33
        colRowNameFont.setFontHeightInPoints((short) 16);
34
        colRowNameFont.setColor(IndexedColors.BLACK.getIndex());
35

  
36
        Font dataFont = workbook.createFont();
37
        dataFont.setBold(false);
38
        dataFont.setFontHeightInPoints((short) 12);
39
        dataFont.setColor(IndexedColors.BLACK.getIndex());
40

  
41
        CellStyle colRowStyle = workbook.createCellStyle();
42
        colRowStyle.setFont(colRowNameFont);
43

  
44
        CellStyle dataStyle = workbook.createCellStyle();
45
        dataStyle.setFont(dataFont);
46

  
47
        Row descRow = sheet.createRow(0);
48

  
49
        for (int i = 0; i < 2; i++) {
50
            Cell cell = descRow.createCell(i + 1);
51
            cell.setCellValue(columns[i]);
52
            cell.setCellStyle(colRowStyle);
53
        }
54

  
55
        for (int i = 0; i < 2; i++) {
56
            Row row = sheet.createRow(i + 1);
57
            Cell nameCell = row.createCell(0);
58
            nameCell.setCellValue(rows[i]);
59
            nameCell.setCellStyle(colRowStyle);
60

  
61
            Cell cell1 = row.createCell(1);
62
            cell1.setCellValue(data[i + i + 1]);
63
            cell1.setCellStyle(dataStyle);
64

  
65
            Cell cell2 = row.createCell(2);
66
            cell2.setCellValue(data[i + i + 1]);
67
            cell2.setCellStyle(dataStyle);
68
        }
69

  
70
        for (int i = 0; i < 3; i++) {
71
            sheet.autoSizeColumn(i);
72
        }
73

  
74
        //merge of columns, will be used for column headers
75
        //sheet.addMergedRegion(new CellRangeAddress(0,0,1,2));
76

  
77
        FileOutputStream streamOut = null;
78

  
79
        try {
80
            streamOut = new FileOutputStream("out.xlsx");
81
        } catch (FileNotFoundException e) {
82
            e.printStackTrace();
83
        }
84

  
85
        try {
86
            workbook.write(streamOut);
87
        } catch (IOException e) {
88
            e.printStackTrace();
89
        }
90

  
91
        try {
92
            if (streamOut != null) {
93
                streamOut.close();
94
            }
95
        } catch (IOException e) {
96
            e.printStackTrace();
97
        }
98

  
99
        try {
100
            workbook.close();
101
        } catch (IOException e) {
102
            e.printStackTrace();
103
        }
104
    }
105
}

Také k dispozici: Unified diff