Projekt

Obecné

Profil

Stáhnout (2.93 KB) Statistiky
| Větev: | Revize:
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
}
(4-4/5)