Java 如何使用ApachePOI确定合并单元格中的行数?
我使用excel为我的测试自动化脚本输入数据,当我知道要读取的确切行和列时,我能够从excel中读取数据。我面临的挑战是当我将单元格合并到工作表中时。例如 这里,ScriptName和Iteration是识别脚本的唯一数据集的主键。 所以我的问题是:Java 如何使用ApachePOI确定合并单元格中的行数?,java,excel,selenium,automated-tests,apache-poi,Java,Excel,Selenium,Automated Tests,Apache Poi,我使用excel为我的测试自动化脚本输入数据,当我知道要读取的确切行和列时,我能够从excel中读取数据。我面临的挑战是当我将单元格合并到工作表中时。例如 这里,ScriptName和Iteration是识别脚本的唯一数据集的主键。 所以我的问题是: 我想获取与脚本名和迭代相关的所有引用集名称,即对于登录脚本,迭代1:我必须获取ABC1参考集、ABC2参考集、ABC3参考集 我想获取与脚本名、迭代和引用集有关的所有PackageName,即对于登录脚本、迭代1、引用集ABC1 Ref set:我
public void getExcelRowNum() {
boolean found = false;
String scriptCell = null, iterationCell = null;
try {
@SuppressWarnings("rawtypes")
Iterator iterator = sheet.rowIterator();
while (iterator.hasNext()) {
Row row = (Row) iterator.next();
scriptCell = row.getCell(1).toString().trim();
iterationCell = row.getCell(2).toString().trim();
if (row.getCell(2).getCellTypeEnum() == CellType.NUMERIC)
iterationCell = iterationCell.substring(0, iterationCell.indexOf(".")).trim();
if ((scriptCell.equals(scriptName) && iterationCell.equals(String.valueOf(iteration).trim()))
|| (scriptCell.equals(scriptName) && Integer.parseInt(iterationCell) == iteration)) {
rowNum = row.getRowNum();
found = true;
break;
}
}
if (rowNum == -1 || found == false)
throw new Exception("Please check the test name: " + scriptName + " or the iteration: " + iteration
+ " in the test data sheet");
row = sheet.getRow(0);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void getExcelColNum(String colName) {
boolean found = false;
try {
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i).getStringCellValue().trim().equals(colName.trim())) {
col_Num = i;
found = true;
break;
}
}
if (col_Num == -1 || found == false)
throw new Exception("Please check the column name: " + colName + " in the test data sheet");
}
catch (Exception e) {
e.printStackTrace();
}
}
public void getCell() {
try {
row = sheet.getRow(rowNum);
cell = row.getCell(col_Num);
}
catch (Exception e) {
e.printStackTrace();
}
}
//Prior to calling this method. I am connecting to the excel sheet which
is in .xlsx or xls format
public String getEntireCellValue(String sheetName, String colName) {
try {
sheet = workbook.getSheet(sheetName);
getExcelRowNum();
getExcelColNum(colName);
getCell();
if (cell.getCellTypeEnum() == CellType.STRING)
return cell.getStringCellValue().trim();
else if (cell.getCellTypeEnum() == CellType.BLANK)
return null;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public int getNumOfMergedRows() {
int rowsMerged = 0;
try {
for(int i = 0; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
if (range.getFirstRow() <= rowNum && range.getLastRow() >=
rowNum) {
++rowsMerged;
}
}
System.out.println("Number of rows merged are: " + rowsMerged);
}
catch (Exception e) {
e.printStackTrace();
}
return rowsMerged;
}
public void getExcelRowNum(){
布尔值=false;
字符串scriptCell=null,iterationCell=null;
试一试{
@抑制警告(“原始类型”)
迭代器迭代器=sheet.rowIterator();
while(iterator.hasNext()){
行=(行)迭代器。下一步();
scriptCell=row.getCell(1.toString().trim();
iterationCell=row.getCell(2.toString().trim();
if(row.getCell(2.getCellTypeEnum()==CellType.NUMERIC)
iterationCell=iterationCell.substring(0,iterationCell.indexOf(“.”).trim();
if((scriptCell.equals(scriptName)&&iterationCell.equals(String.valueOf(iteration.trim()))
||(scriptCell.equals(scriptName)和&Integer.parseInt(iterationCell)=迭代)){
rowNum=row.getRowNum();
发现=真;
打破
}
}
if(rowNum==1 | | found==false)
抛出新异常(“请检查测试名称:“+scriptName+”或迭代:“+iteration
+“在试验数据表中”);
行=表。getRow(0);
}
捕获(例外e){
e、 printStackTrace();
}
}
public void getExcelColNum(字符串colName){
布尔值=false;
试一试{
对于(int i=0;i
另外,我在这里做的是,我试图获取脚本的合并行数,例如,登录脚本合并了6行,然后找到这6行中的单元格数,以获得引用集名称(3个单元格)。
注意:当我调用上面的方法-getNumOfMergedRows()来确定登录脚本合并的行数时,我得到的是4而不是6。我有一个示例代码,它以字段作为输入。它将计算第3列中所有值的值,并将其存储在映射中。它通过计算Excel文件中的空白值,并在每个有效值< /P>重置计数器。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Demo {
public static void main(String[] args) {
readFile(new File("excelstack.xlsx"));
}
public static void readFile(File file) {
try {
FileInputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
Map<String,Integer> map=new HashMap<String,Integer>();
int coltocheck=1;
int counter=1;
String currentitem="";
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if(currentCell.getCellTypeEnum()==CellType.BLANK ) {
if(currentCell.getColumnIndex()==coltocheck)
map.put(currentitem, counter++);
}
else if(currentCell.getCellTypeEnum()==CellType.STRING && currentCell.getColumnIndex()==coltocheck) {
currentitem=currentCell.getStringCellValue();
counter=1;
map.put(currentitem, counter++);
}
}
}
for(Map.Entry<String, Integer> m:map.entrySet())
System.out.println(m.getKey()+" "+m.getValue());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellType;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公开课演示{
公共静态void main(字符串[]args){
readFile(新文件(“excelstack.xlsx”);
}
公共静态void readFile(文件文件){
试一试{
FileInputStream excelFile=新的FileInputStream(文件);
工作簿=新XSSF工作簿(Excel文件);
工作表数据类型工作表=工作簿。getSheetAt(0);
迭代器迭代器=datatypeSheet.Iterator();
Map Map=newhashmap();
int coltocheck=1;
int计数器=1;
字符串currentitem=“”;
while(iterator.hasNext()){
Row currentRow=iterator.next();
迭代器单元
public int getNumOfMergedRows(String colName, int startingRow) {
int rowsMerged = 0, col = 0;
XSSFRow mergedRow = null;
XSSFCell mergedCell = null;
try {
col = getExcelColNum(colName);
for (int i = startingRow + 1; i < sheet.getPhysicalNumberOfRows(); i++) {
mergedRow = sheet.getRow(i);
mergedCell = mergedRow.getCell(col);
if (mergedCell.getCellTypeEnum() == null || mergedCell.getCellTypeEnum() == CellType.BLANK)
rowsMerged++;
else
break;
}
rowsMerged++;
}
catch (Exception e) {
e.printStackTrace();
}
logger.info(rowsMerged + " rows are merged in columne" + colName + " for " + scriptName + " script");
return rowsMerged;
}