Skip to content

Instantly share code, notes, and snippets.

@skymilong
Last active April 23, 2024 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skymilong/8db81c57d01c61f2788ebcf607d9fd85 to your computer and use it in GitHub Desktop.
Save skymilong/8db81c57d01c61f2788ebcf607d9fd85 to your computer and use it in GitHub Desktop.
用于poi操作表格进行同类型的合并单元格计算
private static String[][] convertToMatrix(List<Tdata> dataList, String[] titleRow) throws IllegalArgumentException {
if (dataList.isEmpty()) {
throw new IllegalArgumentException("Data list cannot be empty.");
}
String[][] matrix = new String[dataList.size() + 1][titleRow.length];
System.arraycopy(titleRow, 0, matrix[0], 0, titleRow.length);
for (int i = 0; i < dataList.size(); i++) {
Tdata data = dataList.get(i);
matrix[i + 1][0] = data.getA();
matrix[i + 1][1] = data.getB();
matrix[i + 1][2] = data.getC();
}
return matrix;
}
/**
* 计算并合并矩阵中具有相同值的连续行列。
*
* @param dataMatrix 输入的数据矩阵。
* @param start 起始行索引。
* @param end 结束行索引。
* @param currentColumn 当前处理的列索引。
* @param totalColumns 总列数。
* @param result 存储合并结果的列表,每个元素为一个包含起始行、结束行和列索引的数组。
*/
private static void calculateMergesMatrix(String[][] dataMatrix, int start, int end, int currentColumn, int totalColumns, List<int[]> result) {
// 如果当前列索引超过总列数或起始行大于结束行,结束递归。
if (currentColumn >= totalColumns || start > end) {
return;
}
int i = start;
// 遍历起始行至结束行。
while (i <= end) {
int j = i + 1;
// 当下一行的当前列值与当前行相同时,继续向下查找。
while (j <= end && dataMatrix[i][currentColumn].equals(dataMatrix[j][currentColumn])) {
j++;
}
// 如果发现有超过一行的连续行具有相同值,记录下来。
if (j - i > 1) {
result.add(new int[]{i, j - 1, currentColumn});
// 如果还有更多的列可以检查,递归调用自身进行检查。
if (currentColumn + 1 < totalColumns) {
calculateMergesMatrix(dataMatrix, i, j - 1, currentColumn + 1, totalColumns, result);
}
}
// 更新i到下一组可能的连续行的起始位置。
i = j;
}
}
@skymilong
Copy link
Author

YVuZGQ5Kza

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment