POI XSSFWorkbook 导出excel合并单元格
一,背景介绍
需求,需要导出一个excel,带单元格合并,类似于:
二,代码
网上找了一份代码,自己做了一些更新和修改。
核心代码:
复制代码
1
2// 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始) CellRangeAddress range = new CellRangeAddress(0, 0, 0, 5);
实例:
复制代码
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286package com.madorangecat.artmartxt.utils.excel; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.*; /** * @Author:贺强 * @Date: 2022/12/13 10:37 */ public class TestExcelDemo { public static void main(String[] args) { Student student = new Student(); student.setName("XXX "); student.setResult("95"); student.setClazz("二班"); student.setGrade("五年级"); student.setNumber("66"); List<Student> list = new ArrayList<>(); list.add(student); list.add(student); list.add(student); // testExcelDemo(list); testExcelDemoPlus(list); } /** * @param list 需要写入excel的数据 从数据库或者其他途径读取 */ public static void testExcelDemoPlus(List<Student> list) { /** 第一步,创建一个Workbook,对应一个Excel文件 */ XSSFWorkbook wb = new XSSFWorkbook(); /** 第二步,在Workbook中添加一个sheet,对应Excel文件中的sheet */ XSSFSheet sheet = wb.createSheet("excel导出标题sheet"); /** 第三步,设置样式以及字体样式*/ XSSFCellStyle titleStyle = createTitleCellStyle(wb); XSSFCellStyle headerStyle = createHeadCellStyle(wb); XSSFCellStyle contentStyle = createContentCellStyle(wb); /** 第四步,创建标题 ,合并标题单元格 */ // 行号 int rowNum = 0; // 创建第一页的第一行,索引从0开始 XSSFRow row0 = sheet.createRow(rowNum); row0.setHeight((short) 800);// 设置行高 String title = "标题"; XSSFCell c00 = row0.createCell(0); c00.setCellValue(title); c00.setCellStyle(titleStyle); // 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始) sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));//标题合并单元格操作,6为总列数 /*当需要多个标题时可以使用下面的代码*/ /*String title1 = "excel导出标题02"; XSSFCell c001 = row0.createCell(2); c001.setCellValue(title1); c001.setCellStyle(titleStyle); // 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始) sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 3));//标题合并单元格操作,6为总列数 // rowNum = 1; // XSSFRow row1 = sheet.createRow(rowNum); // row1.setHeight((short) 800);// 设置行高 String title3 = "excel导出标题03"; XSSFCell c003 = row0.createCell(4); c003.setCellValue(title3); c003.setCellStyle(titleStyle); // 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始) sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4));//标题合并单元格操作,6为总列数*/ //第二行 rowNum = 2; XSSFRow row2 = sheet.createRow(rowNum); row2.setHeight((short) 700); String[] row_third = {"学号", "姓名", "年级", "班级", "成绩"}; for (int i = 0; i < row_third.length; i++) { //sheet.setColumnWidth(i, 256*30); //设置列宽度 XSSFCell tempCell = row2.createCell(i); tempCell.setCellValue(row_third[i]); tempCell.setCellStyle(headerStyle); } rowNum = 3; for (Student student : list) { XSSFRow tempRow = sheet.createRow(rowNum); rowNum++; tempRow.setHeight((short) 500); // 循环单元格填入数据 for (int j = 0; j < 5; j++) { XSSFCell tempCell = tempRow.createCell(j); tempCell.setCellStyle(contentStyle); String tempValue = ""; if (j == 0) { // 学号 tempValue = student.getNumber(); } else if (j == 1) { // 姓名 tempValue = student.getName(); } else if (j == 2) { // 年级 tempValue = student.getGrade(); } else if (j == 3) { // 班级 tempValue = student.getClazz(); } else if (j == 4) { // 成绩 tempValue = student.getResult(); } tempCell.setCellValue(tempValue); } } String filePath = "D:\Demo\"; String fileName = "testExcelDemo.xlsx"; File file = new File(filePath + fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(file); // 写入磁盘 wb.write(fos); fos.close();//记得关闭 } catch (IOException e) { e.printStackTrace(); } } /** * @param list 需要写入excel的数据 从数据库或者其他途径读取 */ public static void testExcelDemo(List<Student> list) { /** 第一步,创建一个Workbook,对应一个Excel文件 */ XSSFWorkbook wb = new XSSFWorkbook(); /** 第二步,在Workbook中添加一个sheet,对应Excel文件中的sheet */ XSSFSheet sheet = wb.createSheet("excel导出标题"); /** 第三步,设置样式以及字体样式*/ XSSFCellStyle titleStyle = createTitleCellStyle(wb); XSSFCellStyle headerStyle = createHeadCellStyle(wb); XSSFCellStyle contentStyle = createContentCellStyle(wb); /** 第四步,创建标题 ,合并标题单元格 */ // 行号 int rowNum = 0; // 创建第一页的第一行,索引从0开始 XSSFRow row0 = sheet.createRow(rowNum++); row0.setHeight((short) 800);// 设置行高 String title = "excel导出标题"; XSSFCell c00 = row0.createCell(0); c00.setCellValue(title); c00.setCellStyle(titleStyle); // 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始) sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));//标题合并单元格操作,6为总列数 //第二行 XSSFRow row2 = sheet.createRow(rowNum++); row2.setHeight((short) 700); String[] row_third = {"学号", "姓名", "年级", "班级", "成绩"}; for (int i = 0; i < row_third.length; i++) { //sheet.setColumnWidth(i, 256*30); //设置列宽度 XSSFCell tempCell = row2.createCell(i); tempCell.setCellValue(row_third[i]); tempCell.setCellStyle(headerStyle); } for (Student student : list) { XSSFRow tempRow = sheet.createRow(rowNum++); tempRow.setHeight((short) 500); // 循环单元格填入数据 for (int j = 0; j < 5; j++) { XSSFCell tempCell = tempRow.createCell(j); tempCell.setCellStyle(contentStyle); String tempValue = ""; if (j == 0) { // 学号 tempValue = student.getNumber(); } else if (j == 1) { // 姓名 tempValue = student.getName(); } else if (j == 2) { // 年级 tempValue = student.getGrade(); } else if (j == 3) { // 班级 tempValue = student.getClazz(); } else if (j == 4) { // 成绩 tempValue = student.getResult(); } tempCell.setCellValue(tempValue); } } String filePath = "D:\Demo\"; String fileName = "testExcelDemo.xlsx"; File file = new File(filePath + fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(file); // 写入磁盘 wb.write(fos); fos.close();//记得关闭 } catch (IOException e) { e.printStackTrace(); } } /** * 创建标题样式 * * @param wb * @return */ private static XSSFCellStyle createTitleCellStyle(XSSFWorkbook wb) { XSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景颜色 XSSFFont headerFont1 = (XSSFFont) wb.createFont(); // 创建字体样式 headerFont1.setBold(true); //字体加粗 headerFont1.setFontName("黑体"); // 设置字体类型 headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小 cellStyle.setFont(headerFont1); // 为标题样式设置字体样式 return cellStyle; } /** * 创建表头样式 * * @param wb * @return */ private static XSSFCellStyle createHeadCellStyle(XSSFWorkbook wb) { XSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setWrapText(true);// 设置自动换行 cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景颜色 cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直对齐 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle.setBottomBorderColor(IndexedColors.BLACK.index); cellStyle.setBorderBottom(BorderStyle.THIN); //下边框 cellStyle.setBorderLeft(BorderStyle.THIN); //左边框 cellStyle.setBorderRight(BorderStyle.THIN); //右边框 cellStyle.setBorderTop(BorderStyle.THIN); //上边框 XSSFFont headerFont = (XSSFFont) wb.createFont(); // 创建字体样式 headerFont.setBold(true); //字体加粗 headerFont.setFontName("黑体"); // 设置字体类型 headerFont.setFontHeightInPoints((short) 12); // 设置字体大小 cellStyle.setFont(headerFont); // 为标题样式设置字体样式 return cellStyle; } /** * 创建内容样式 * * @param wb * @return */ private static XSSFCellStyle createContentCellStyle(XSSFWorkbook wb) { XSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中 cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中 cellStyle.setWrapText(true);// 设置自动换行 cellStyle.setBorderBottom(BorderStyle.THIN); //下边框 cellStyle.setBorderLeft(BorderStyle.THIN); //左边框 cellStyle.setBorderRight(BorderStyle.THIN); //右边框 cellStyle.setBorderTop(BorderStyle.THIN); //上边框 // 生成12号字体 XSSFFont font = wb.createFont(); font.setColor((short) 8); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); return cellStyle; } }
最后
以上就是现实草丛最近收集整理的关于POI XSSFWorkbook 导出excel合并单元格的全部内容,更多相关POI内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复