|
|
|
|
package UnitTest.ImportExcel;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.QingLong.Util.ImportUtil;
|
|
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
|
|
public class TestReadMergeCell {
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断一个单元格是不是被合并了
|
|
|
|
|
*
|
|
|
|
|
* @param sheet
|
|
|
|
|
* @param row
|
|
|
|
|
* @param col
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isMerged(XSSFSheet sheet, int row, int col) {
|
|
|
|
|
// 判断单元格是否被合并
|
|
|
|
|
for (CellRangeAddress range : sheet.getMergedRegions()) {
|
|
|
|
|
if (row >= range.getFirstRow() && row <= range.getLastRow()
|
|
|
|
|
&& col >= range.getFirstColumn() && col <= range.getLastColumn()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
String source = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\source.xlsx";
|
|
|
|
|
int row = 3; // 假设要判断的单元格的行号
|
|
|
|
|
int col = 1; // 假设要判断的单元格的列号
|
|
|
|
|
|
|
|
|
|
InputStream is = new FileInputStream(source);
|
|
|
|
|
XSSFWorkbook wb = new XSSFWorkbook(is);
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
|
|
|
|
|
|
|
for (int i = row; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
|
if (isMerged(sheet, i, col)) {
|
|
|
|
|
XSSFCell cell = sheet.getRow(i).getCell(col);
|
|
|
|
|
String value = ImportUtil.getValue(cell).toString();
|
|
|
|
|
if (StrKit.isBlank(value)) {
|
|
|
|
|
for (int j = i - 1; ; j--) {
|
|
|
|
|
String prev = ImportUtil.getValue(sheet.getRow(j).getCell(col)).toString();
|
|
|
|
|
if (!StrKit.isBlank(prev)) {
|
|
|
|
|
System.out.println(prev);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println(ImportUtil.getValue(cell));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|