import org.jeecgframework.poi.excel.excelexportutil的exportexcel导出怎样设置
展开全部
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
package org.jeecgframework.poi.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jeecgframework.poi.excel.entity.params.MergeEntity;
/**
* 纵向合并单元格工具类
* @author JueYue
* @date 2015年6月21日 上午11:21:40
*/
public final class PoiMergeCellUtil {
private PoiMergeCellUtil() {
}
/**
* 纵向合并相同内容的单元格
*
* @param sheet
* @param mergeMap key--列,value--依赖的列,没有传空
* @param startRow 开始行
*/
public static void mergeCells(Sheet sheet, Map mergeMap, int startRow) {
Map mergeDataMap = new HashMap();
if (mergeMap.size() == 0) {
return;
}
Row row;
Set sets = mergeMap.keySet();
String text;
for (int i = startRow; i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
for (Integer index : sets) {
if (row.getCell(index) == null) {
mergeDataMap.get(index).setEndRow(i);
} else {
text = row.getCell(index).getStringCellValue();
if (StringUtils.isNotEmpty(text)) {
hanlderMergeCells(index, i, text, mergeDataMap, sheet, row.getCell(index),
mergeMap.get(index));
} else {
mergeCellOrContinue(index, mergeDataMap, sheet);
}
}
}
}
if (mergeDataMap.size() > 0) {
for (Integer index : mergeDataMap.keySet()) {
sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
mergeDataMap.get(index).getEndRow(), index, index));
}
}
}
/**
* 处理合并单元格
*
* @param index
* @param rowNum
* @param text
* @param mergeDataMap
* @param sheet
* @param cell
* @param delys
*/
private static void hanlderMergeCells(Integer index, int rowNum, String text,
Map mergeDataMap, Sheet sheet,
Cell cell, int[] delys) {
if (mergeDataMap.containsKey(index)) {
if (checkIsEqualByCellContents(mergeDataMap.get(index), text, cell, delys, rowNum)) {
mergeDataMap.get(index).setEndRow(rowNum);
} else {
sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
mergeDataMap.get(index).getEndRow(), index, index));
mergeDataMap.put(index, createMergeEntity(text, rowNum, cell, delys));
}
} else {
mergeDataMap.put(index, createMergeEntity(text, rowNum, cell, delys));
}
}
/**
* 字符为空的情况下判断
*
* @param index
* @param mergeDataMap
* @param sheet
*/
private static void mergeCellOrContinue(Integer index, Map mergeDataMap,
Sheet sheet) {
if (mergeDataMap.containsKey(index)
&& mergeDataMap.get(index).getEndRow() != mergeDataMap.get(index).getStartRow()) {
sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
mergeDataMap.get(index).getEndRow(), index, index));
mergeDataMap.remove(index);
}
}
private static MergeEntity createMergeEntity(String text, int rowNum, Cell cell, int[] delys) {
MergeEntity mergeEntity = new MergeEntity(text, rowNum, rowNum);
List list = new ArrayList(delys.length);
mergeEntity.setRelyList(list);
for (int i = 0; i < delys.length; i++) {
list.add(getCellNotNullText(cell, delys[i], rowNum));
}
return mergeEntity;
}
private static boolean checkIsEqualByCellContents(MergeEntity mergeEntity, String text,
Cell cell, int[] delys, int rowNum) {
// 没有依赖关系
if (delys == null || delys.length == 0) {
return mergeEntity.getText().equals(text);
}
// 存在依赖关系
if (mergeEntity.getText().equals(text)) {
for (int i = 0; i < delys.length; i++) {
if (!getCellNotNullText(cell, delys[i], rowNum).equals(
mergeEntity.getRelyList().get(i))) {
return false;
}
}
return true;
}
return false;
}
/**
* 获取一个单元格的值,确保这个单元格必须有值,不然向上查询
*
* @param cell
* @param index
* @param rowNum
* @return
*/
private static String getCellNotNullText(Cell cell, int index, int rowNum) {
String temp = cell.getRow().getCell(index).getStringCellValue();
while (StringUtils.isEmpty(temp)) {
temp = cell.getRow().getSheet().getRow(--rowNum).getCell(index).getStringCellValue();
}
return temp;
}
}
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
package org.jeecgframework.poi.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jeecgframework.poi.excel.entity.params.MergeEntity;
/**
* 纵向合并单元格工具类
* @author JueYue
* @date 2015年6月21日 上午11:21:40
*/
public final class PoiMergeCellUtil {
private PoiMergeCellUtil() {
}
/**
* 纵向合并相同内容的单元格
*
* @param sheet
* @param mergeMap key--列,value--依赖的列,没有传空
* @param startRow 开始行
*/
public static void mergeCells(Sheet sheet, Map mergeMap, int startRow) {
Map mergeDataMap = new HashMap();
if (mergeMap.size() == 0) {
return;
}
Row row;
Set sets = mergeMap.keySet();
String text;
for (int i = startRow; i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
for (Integer index : sets) {
if (row.getCell(index) == null) {
mergeDataMap.get(index).setEndRow(i);
} else {
text = row.getCell(index).getStringCellValue();
if (StringUtils.isNotEmpty(text)) {
hanlderMergeCells(index, i, text, mergeDataMap, sheet, row.getCell(index),
mergeMap.get(index));
} else {
mergeCellOrContinue(index, mergeDataMap, sheet);
}
}
}
}
if (mergeDataMap.size() > 0) {
for (Integer index : mergeDataMap.keySet()) {
sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
mergeDataMap.get(index).getEndRow(), index, index));
}
}
}
/**
* 处理合并单元格
*
* @param index
* @param rowNum
* @param text
* @param mergeDataMap
* @param sheet
* @param cell
* @param delys
*/
private static void hanlderMergeCells(Integer index, int rowNum, String text,
Map mergeDataMap, Sheet sheet,
Cell cell, int[] delys) {
if (mergeDataMap.containsKey(index)) {
if (checkIsEqualByCellContents(mergeDataMap.get(index), text, cell, delys, rowNum)) {
mergeDataMap.get(index).setEndRow(rowNum);
} else {
sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
mergeDataMap.get(index).getEndRow(), index, index));
mergeDataMap.put(index, createMergeEntity(text, rowNum, cell, delys));
}
} else {
mergeDataMap.put(index, createMergeEntity(text, rowNum, cell, delys));
}
}
/**
* 字符为空的情况下判断
*
* @param index
* @param mergeDataMap
* @param sheet
*/
private static void mergeCellOrContinue(Integer index, Map mergeDataMap,
Sheet sheet) {
if (mergeDataMap.containsKey(index)
&& mergeDataMap.get(index).getEndRow() != mergeDataMap.get(index).getStartRow()) {
sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
mergeDataMap.get(index).getEndRow(), index, index));
mergeDataMap.remove(index);
}
}
private static MergeEntity createMergeEntity(String text, int rowNum, Cell cell, int[] delys) {
MergeEntity mergeEntity = new MergeEntity(text, rowNum, rowNum);
List list = new ArrayList(delys.length);
mergeEntity.setRelyList(list);
for (int i = 0; i < delys.length; i++) {
list.add(getCellNotNullText(cell, delys[i], rowNum));
}
return mergeEntity;
}
private static boolean checkIsEqualByCellContents(MergeEntity mergeEntity, String text,
Cell cell, int[] delys, int rowNum) {
// 没有依赖关系
if (delys == null || delys.length == 0) {
return mergeEntity.getText().equals(text);
}
// 存在依赖关系
if (mergeEntity.getText().equals(text)) {
for (int i = 0; i < delys.length; i++) {
if (!getCellNotNullText(cell, delys[i], rowNum).equals(
mergeEntity.getRelyList().get(i))) {
return false;
}
}
return true;
}
return false;
}
/**
* 获取一个单元格的值,确保这个单元格必须有值,不然向上查询
*
* @param cell
* @param index
* @param rowNum
* @return
*/
private static String getCellNotNullText(Cell cell, int index, int rowNum) {
String temp = cell.getRow().getCell(index).getStringCellValue();
while (StringUtils.isEmpty(temp)) {
temp = cell.getRow().getSheet().getRow(--rowNum).getCell(index).getStringCellValue();
}
return temp;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询