-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombineData.java
More file actions
105 lines (104 loc) · 3.28 KB
/
CombineData.java
File metadata and controls
105 lines (104 loc) · 3.28 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class CombineData {
public ArrayList<Integer> combineColumns = new ArrayList<Integer>();//用于保存需要合并的列号
private String[][] datas;//table的数据,用来计算合并的单元格
private ArrayList<HashMap<Integer, Integer>> rowPoss;
private ArrayList<HashMap<Integer, Integer>> rowCounts;
public CombineData() { }
public CombineData(String[][] datas, int... combineColumns) {
this.datas = datas;
for (int i = 0; i < combineColumns.length; i++) {
if (combineColumns[i] < 0) {
continue; }
this.combineColumns.add(combineColumns[i]);
}
process();
}
public CombineData(String[][] datas, List<Integer> combineColumns) {
this.datas = datas;
for (Integer column : combineColumns) {
if (column < 0) {
continue; }
this.combineColumns.add(column);
}
process();
}
public void initData(String[][] datas, int... combineColumns) {
this.datas = datas;
this.combineColumns.clear();
for (int i = 0; i < combineColumns.length; i++) {
if (combineColumns[i] < 0) {
continue;
}
this.combineColumns.add(combineColumns[i]);
}
process();
}
public void initData(String[][] datas, List<Integer> combineColumns) {
this.datas = datas;
this.combineColumns.clear();
for (Integer column : combineColumns) {
if (column < 0) {
continue;
}
this.combineColumns.add(column);
}
process();
}
private void process() {
rowPoss = new ArrayList<HashMap<Integer, Integer>>();
rowCounts = new ArrayList<HashMap<Integer, Integer>>();
for (Integer integer : combineColumns) {
HashMap<Integer, Integer> rowPos = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> rowCount = new HashMap<Integer, Integer>();
String pre = "";
int count = 0;
int start = 0;
for (int i = 0; i < datas.length; i++) {
String[] data = datas[i];
if (pre.equals(data[integer])) {
count++;
} else {
rowCount.put(start, count);
pre = data[integer];
count = 1;
start = i;
}
rowPos.put(i, start);
}
rowCount.put(start, count);
rowPoss.add(rowPos);
rowCounts.add(rowCount);
}
}
/**
* 返回table中row行column列单元格所跨行数
*/
public int span(int row, int column) {
int index = combineColumns.lastIndexOf(column);
if (index != -1) {
return rowCounts.get(index).get(rowPoss.get(index).get(row));
} else {
return 1;
}
}
/**
* 返回table中row行column列单元格所在的合并单元格的起始行位置
*/
public int visibleCell(int row, int column) {
int index = combineColumns.lastIndexOf(column);
if ((index != -1) && row > -1) {
return rowPoss.get(index).get(row);
} else {
return row;
}
}
}