|
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 |
}
|
re #7781 Created package utils for utility classes and created class Exporter with export to xlsx functionality