Revize d90fc9de
Přidáno uživatelem Vojtěch Danišík před téměř 4 roky(ů)
src/main/java/vldc/aswi/utils/ExporterXLSX.java | ||
---|---|---|
1 | 1 |
package vldc.aswi.utils; |
2 | 2 |
|
3 | 3 |
import org.apache.poi.ss.usermodel.*; |
4 |
import org.apache.poi.ss.util.CellRangeAddress; |
|
4 | 5 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
6 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
7 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell; |
|
5 | 8 |
|
6 |
import java.io.FileNotFoundException;
|
|
9 |
import java.io.File; |
|
7 | 10 |
import java.io.FileOutputStream; |
8 |
import java.io.IOException;
|
|
11 |
import java.util.List;
|
|
9 | 12 |
|
10 | 13 |
/** |
11 |
* Class used for exporting data to XLSX |
|
14 |
* Class used for exporting data to XLSX.
|
|
12 | 15 |
*/ |
13 | 16 |
public class ExporterXLSX { |
14 | 17 |
|
15 | 18 |
/** |
16 |
* !!!DUMMY!!! |
|
17 | 19 |
* Exports data to .xlsx file |
18 |
* @param tableName name of the table |
|
20 |
* @param tableName - name of the table. |
|
21 |
* @param contingencyTableRows - List of contingency table rows. |
|
19 | 22 |
*/ |
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"}; |
|
23 |
public static void export(File tmpFile, String tableName, List<ContingencyTableRow> contingencyTableRows) { |
|
26 | 24 |
|
25 |
// Create workbook. |
|
27 | 26 |
Workbook workbook = new XSSFWorkbook(); |
28 | 27 |
|
28 |
// Create sheet. |
|
29 | 29 |
Sheet sheet = workbook.createSheet(tableName); |
30 | 30 |
|
31 |
// Create font for row names. |
|
31 | 32 |
Font colRowNameFont = workbook.createFont(); |
32 | 33 |
colRowNameFont.setBold(true); |
33 | 34 |
colRowNameFont.setFontHeightInPoints((short) 16); |
35 |
colRowNameFont.setFontName("HELVETICA"); |
|
34 | 36 |
colRowNameFont.setColor(IndexedColors.BLACK.getIndex()); |
35 | 37 |
|
38 |
// Create font for data. |
|
36 | 39 |
Font dataFont = workbook.createFont(); |
40 |
dataFont.setFontName("HELVETICA"); |
|
37 | 41 |
dataFont.setBold(false); |
38 | 42 |
dataFont.setFontHeightInPoints((short) 12); |
39 | 43 |
dataFont.setColor(IndexedColors.BLACK.getIndex()); |
40 | 44 |
|
45 |
// Set row cell style. |
|
41 | 46 |
CellStyle colRowStyle = workbook.createCellStyle(); |
42 | 47 |
colRowStyle.setFont(colRowNameFont); |
48 |
colRowStyle.setAlignment(HorizontalAlignment.CENTER); |
|
43 | 49 |
|
50 |
// Set data cell style. |
|
44 | 51 |
CellStyle dataStyle = workbook.createCellStyle(); |
45 | 52 |
dataStyle.setFont(dataFont); |
53 |
dataStyle.setAlignment(HorizontalAlignment.CENTER); |
|
46 | 54 |
|
47 |
Row descRow = sheet.createRow(0);
|
|
55 |
int rowCounter = 0;
|
|
48 | 56 |
|
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 |
} |
|
57 |
for (ContingencyTableRow row : contingencyTableRows) { |
|
54 | 58 |
|
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); |
|
59 |
Row xlsxRow = sheet.createRow(rowCounter); |
|
60 | 60 |
|
61 |
Cell cell1 = row.createCell(1); |
|
62 |
cell1.setCellValue(data[i + i + 1]); |
|
63 |
cell1.setCellStyle(dataStyle); |
|
61 |
if (row.isHeader()) { |
|
62 |
// Row is header. |
|
63 |
setColRow(sheet, xlsxRow, row.getCells(), colRowStyle); |
|
64 |
} |
|
65 |
else { |
|
66 |
// Row contains data. |
|
67 |
setDataRow(sheet, xlsxRow, row.getCells(), colRowStyle, dataStyle); |
|
68 |
} |
|
64 | 69 |
|
65 |
Cell cell2 = row.createCell(2); |
|
66 |
cell2.setCellValue(data[i + i + 1]); |
|
67 |
cell2.setCellStyle(dataStyle); |
|
70 |
rowCounter++; |
|
68 | 71 |
} |
69 | 72 |
|
70 |
for (int i = 0; i < 3; i++) { |
|
71 |
sheet.autoSizeColumn(i); |
|
72 |
} |
|
73 |
try { |
|
73 | 74 |
|
74 |
//merge of columns, will be used for column headers |
|
75 |
//sheet.addMergedRegion(new CellRangeAddress(0,0,1,2)); |
|
75 |
FileOutputStream out = new FileOutputStream(tmpFile); |
|
76 |
workbook.write(out); |
|
77 |
out.close(); |
|
76 | 78 |
|
77 |
FileOutputStream streamOut = null;
|
|
79 |
} catch (Exception e) {
|
|
78 | 80 |
|
79 |
try { |
|
80 |
streamOut = new FileOutputStream("out.xlsx"); |
|
81 |
} catch (FileNotFoundException e) { |
|
82 |
e.printStackTrace(); |
|
83 | 81 |
} |
82 |
} |
|
84 | 83 |
|
85 |
try { |
|
86 |
workbook.write(streamOut); |
|
87 |
} catch (IOException e) { |
|
88 |
e.printStackTrace(); |
|
89 |
} |
|
84 |
/** |
|
85 |
* Set column row. |
|
86 |
* @param sheet - Sheet. |
|
87 |
* @param row - Row. |
|
88 |
* @param cells - List of contingency table cells. |
|
89 |
* @param colRowStyle - Column row styles. |
|
90 |
*/ |
|
91 |
private static void setColRow(Sheet sheet, Row row, List<ContingencyTableRowCell> cells, CellStyle colRowStyle) { |
|
90 | 92 |
|
91 |
try { |
|
92 |
if (streamOut != null) { |
|
93 |
streamOut.close(); |
|
93 |
int cellCounter = 0; |
|
94 |
|
|
95 |
for (ContingencyTableRowCell cell : cells) { |
|
96 |
|
|
97 |
// Create new xlsx cell. |
|
98 |
Cell xlsxCell = row.createCell(cellCounter); |
|
99 |
xlsxCell.setCellValue(cell.getValue()); |
|
100 |
xlsxCell.setCellStyle(colRowStyle); |
|
101 |
|
|
102 |
// Calculate last cell to be merged with current. |
|
103 |
int colSpan = cell.getColSpan(); |
|
104 |
int newCellPosition = cellCounter + (colSpan - 1); |
|
105 |
|
|
106 |
// If colSpan is higher than 1, then merge cells. |
|
107 |
if (colSpan > 1) { |
|
108 |
sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), cellCounter, newCellPosition)); |
|
109 |
} |
|
110 |
|
|
111 |
// If cell has some value, then autosize current column. |
|
112 |
if (!cell.getValue().equals("")) { |
|
113 |
|
|
114 |
for (int i = cellCounter; i < (newCellPosition + 1); i++) { |
|
115 |
sheet.autoSizeColumn(cellCounter); |
|
116 |
} |
|
94 | 117 |
} |
95 |
} catch (IOException e) { |
|
96 |
e.printStackTrace();
|
|
118 |
|
|
119 |
cellCounter = newCellPosition + 1;
|
|
97 | 120 |
} |
121 |
} |
|
98 | 122 |
|
99 |
try { |
|
100 |
workbook.close(); |
|
101 |
} catch (IOException e) { |
|
102 |
e.printStackTrace(); |
|
123 |
/** |
|
124 |
* Set data row. |
|
125 |
* @param sheet - Sheet. |
|
126 |
* @param row - Row. |
|
127 |
* @param cells - List of contingency table cells. |
|
128 |
* @param colRowStyle - Column row styles. |
|
129 |
* @param dataStyle - Data row styles. |
|
130 |
*/ |
|
131 |
private static void setDataRow(Sheet sheet, Row row, List<ContingencyTableRowCell> cells, CellStyle colRowStyle, CellStyle dataStyle) { |
|
132 |
|
|
133 |
int cellCounter = 0; |
|
134 |
|
|
135 |
for (ContingencyTableRowCell cell : cells) { |
|
136 |
|
|
137 |
// Create new xlsx cell. |
|
138 |
Cell xlsxCell = row.createCell(cellCounter); |
|
139 |
xlsxCell.setCellValue(cell.getValue()); |
|
140 |
|
|
141 |
// First cell is always header, so apply different style. |
|
142 |
if (cellCounter == 0) { |
|
143 |
xlsxCell.setCellStyle(colRowStyle); |
|
144 |
} else { |
|
145 |
xlsxCell.setCellStyle(dataStyle); |
|
146 |
} |
|
147 |
|
|
148 |
sheet.autoSizeColumn(cellCounter); |
|
149 |
cellCounter++; |
|
103 | 150 |
} |
104 | 151 |
} |
105 | 152 |
} |
Také k dispozici: Unified diff
re #8133 #8132 Export contingency table to XLSX + PDF (not fully complete).