PoiUtils.java
8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.bsth.service.schedule.utils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.xssf.usermodel.*;
import java.awt.Color;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* POI操作的until方法。
*/
public class PoiUtils {
/**
* 使用poi读取cell中的值
* @param cell
* @return
*/
public static String getStringValueFromCell(Cell cell) {
SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
DecimalFormat decimalFormat = new DecimalFormat("#.#");
String cellValue = "";
if(cell == null) {
return cellValue;
}
else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
cellValue = cell.getStringCellValue();
}
else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
if(HSSFDateUtil.isCellDateFormatted(cell)) {
double d = cell.getNumericCellValue();
Date date = HSSFDateUtil.getJavaDate(d);
cellValue = sFormat.format(date);
}
else {
cellValue = decimalFormat.format((cell.getNumericCellValue()));
}
}
else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
cellValue = "";
}
else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
cellValue = String.valueOf(cell.getBooleanCellValue());
}
else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) {
cellValue = "";
}
else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
cellValue = cell.getCellFormula().toString();
}
return cellValue;
}
public static XSSFCell createStringXSSFCell(
XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value,
Color backgroundColor) {
return createXSSFCell(
xssfWorkbook, xssfRow, column,
value, XSSFCell.CELL_TYPE_STRING,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
BorderStyle.MEDIUM, new Color(0xdedede),
(short) 13, new Color(0x2765A7), "宋体",
backgroundColor, FillPatternType.SOLID_FOREGROUND
);
}
public static XSSFCell createStringXSSFCell(
XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value) {
return createXSSFCell(
xssfWorkbook, xssfRow, column,
value, XSSFCell.CELL_TYPE_STRING,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
BorderStyle.MEDIUM, new Color(0xdedede),
(short) 13, new Color(0x2765A7), "宋体",
new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
);
}
public static XSSFCell createDoubleXSSFCell(
XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Double value) {
return createXSSFCell(
xssfWorkbook, xssfRow, column,
value, XSSFCell.CELL_TYPE_NUMERIC,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
BorderStyle.MEDIUM, new Color(0xdedede),
(short) 13, new Color(0x2765A7), "宋体",
new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
);
}
public static XSSFCell createIntegerXSSFCell(
XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Integer value) {
return createXSSFCell(
xssfWorkbook, xssfRow, column,
value, XSSFCell.CELL_TYPE_NUMERIC,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
BorderStyle.MEDIUM, new Color(0xdedede),
(short) 13, new Color(0x2765A7), "宋体",
new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
);
}
public static XSSFCell createBlankXSSFCell(
XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column) {
return createXSSFCell(
xssfWorkbook, xssfRow, column,
null, XSSFCell.CELL_TYPE_BLANK,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
BorderStyle.MEDIUM, new Color(0xdedede),
(short) 13, new Color(0x2765A7), "宋体",
new Color(0xffffff), FillPatternType.SOLID_FOREGROUND
);
}
public static XSSFCell setIntegerStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) {
CreationHelper creationHelper = xssfWorkbook.getCreationHelper();
XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle();
xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0"));
return xssfCell;
}
public static XSSFCell setDoubleStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) {
CreationHelper creationHelper = xssfWorkbook.getCreationHelper();
XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle();
xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00"));
return xssfCell;
}
public static XSSFCell createXSSFCell(
XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column,
Object value, int valueType,
HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment,
BorderStyle borderStyle, Color borderColor,
short fontSize, Color fontColor, String fontName,
Color backgroudColor, FillPatternType fillPatternType) {
CreationHelper creationHelper = xssfWorkbook.getCreationHelper();
// 1、创建单元格对象
XSSFCell cell = xssfRow.createCell(column);
// 2、设定样式
XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
// 设定值
if (valueType == XSSFCell.CELL_TYPE_STRING) {
cell.setCellValue(creationHelper.createRichTextString(
WorkbookUtil.createSafeSheetName(String.valueOf(value))));
} else if (valueType == XSSFCell.CELL_TYPE_NUMERIC) {
if (value instanceof Date) { // 日期
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd"));
cell.setCellValue((Date) value);
} else if (value instanceof Double) {
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00"));
cell.setCellValue((Double) value);
} else if (value instanceof Integer) {
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0"));
cell.setCellValue(Double.valueOf(value.toString()));
} else {
throw new RuntimeException("只支持 Date Double Integer 单元格类型");
}
} else if (valueType == XSSFCell.CELL_TYPE_BLANK) {
cell.setCellType(Cell.CELL_TYPE_BLANK);
}
else {
throw new RuntimeException("暂时不支持字符串、日期、数字以为的类型");
}
// 对齐方式
cellStyle.setAlignment(horizontalAlignment);
cellStyle.setVerticalAlignment(verticalAlignment);
// 边框样式
cellStyle.setBorderTop(borderStyle);
cellStyle.setTopBorderColor(new XSSFColor(borderColor));
cellStyle.setBorderBottom(borderStyle);
cellStyle.setBottomBorderColor(new XSSFColor(borderColor));
cellStyle.setBorderLeft(borderStyle);
cellStyle.setLeftBorderColor(new XSSFColor(borderColor));
cellStyle.setBorderRight(borderStyle);
cellStyle.setRightBorderColor(new XSSFColor(borderColor));
// 字体颜色
XSSFFont font = xssfWorkbook.createFont();
font.setColor(new XSSFColor(fontColor));
font.setFontHeightInPoints(fontSize);
font.setFontName(fontName);
cellStyle.setFont(font);
// 单元背景色
cellStyle.setFillForegroundColor(new XSSFColor(backgroudColor));
cellStyle.setFillPattern(fillPatternType);
// TODO
cell.setCellStyle(cellStyle);
return cell;
}
}