1
|
package vldc.aswi.utils;
|
2
|
|
3
|
import org.apache.poi.ss.usermodel.*;
|
4
|
import org.apache.poi.ss.util.CellRangeAddress;
|
5
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
6
|
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
|
7
|
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell;
|
8
|
|
9
|
import java.io.File;
|
10
|
import java.io.FileOutputStream;
|
11
|
import java.util.List;
|
12
|
|
13
|
/**
|
14
|
* Class used for exporting data to XLSX.
|
15
|
*/
|
16
|
public class ExporterXLSX {
|
17
|
|
18
|
/**
|
19
|
* Exports data to .xlsx file
|
20
|
* @param tableName - name of the table.
|
21
|
* @param contingencyTableRows - List of contingency table rows.
|
22
|
*/
|
23
|
public static void export(File tmpFile, String tableName, List<ContingencyTableRow> contingencyTableRows) {
|
24
|
|
25
|
// Create workbook.
|
26
|
Workbook workbook = new XSSFWorkbook();
|
27
|
|
28
|
// Create sheet.
|
29
|
Sheet sheet = workbook.createSheet(tableName);
|
30
|
|
31
|
// Create font for row names.
|
32
|
Font colRowNameFont = workbook.createFont();
|
33
|
colRowNameFont.setBold(true);
|
34
|
colRowNameFont.setFontHeightInPoints((short) 16);
|
35
|
colRowNameFont.setFontName("HELVETICA");
|
36
|
colRowNameFont.setColor(IndexedColors.BLACK.getIndex());
|
37
|
|
38
|
// Create font for data.
|
39
|
Font dataFont = workbook.createFont();
|
40
|
dataFont.setFontName("HELVETICA");
|
41
|
dataFont.setBold(false);
|
42
|
dataFont.setFontHeightInPoints((short) 12);
|
43
|
dataFont.setColor(IndexedColors.BLACK.getIndex());
|
44
|
|
45
|
// Set row cell style.
|
46
|
CellStyle colRowStyle = workbook.createCellStyle();
|
47
|
colRowStyle.setFont(colRowNameFont);
|
48
|
colRowStyle.setAlignment(HorizontalAlignment.CENTER);
|
49
|
|
50
|
// Set data cell style.
|
51
|
CellStyle dataStyle = workbook.createCellStyle();
|
52
|
dataStyle.setFont(dataFont);
|
53
|
dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
54
|
|
55
|
int rowCounter = 0;
|
56
|
|
57
|
for (ContingencyTableRow row : contingencyTableRows) {
|
58
|
|
59
|
Row xlsxRow = sheet.createRow(rowCounter);
|
60
|
|
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
|
}
|
69
|
|
70
|
rowCounter++;
|
71
|
}
|
72
|
|
73
|
try {
|
74
|
|
75
|
FileOutputStream out = new FileOutputStream(tmpFile);
|
76
|
workbook.write(out);
|
77
|
out.close();
|
78
|
|
79
|
} catch (Exception e) {
|
80
|
|
81
|
}
|
82
|
}
|
83
|
|
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) {
|
92
|
|
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
|
}
|
117
|
}
|
118
|
|
119
|
cellCounter = newCellPosition + 1;
|
120
|
}
|
121
|
}
|
122
|
|
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++;
|
150
|
}
|
151
|
}
|
152
|
}
|