[英]How to determine number of rows in a merged cell using apache poi?
我正在使用excel為我的測試自動化腳本輸入數據,當我知道要讀取的確切行和列時,我能夠從excel讀取數據。 我面臨的挑戰是當我在表格中合並單元格時。 例如, 測試數據優於樣本
這里ScriptName和Iteration是我為我的腳本識別一組唯一數據的主鍵。 所以我的問題是:
目前以下是方法 - getEntireCellValue()我用來從excel獲取數據,我需要幫助來解決上述2個問題。 任何形式的支持都非常感謝。
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;
}
PS我在這里做的是,我正在嘗試獲取腳本的合並行數,例如,為登錄腳本合並6行,然后在這6行中查找單元格數以獲取參考集名稱(3個單元格)。 注意:當我調用上面的方法 - getNumOfMergedRows()來確定為Login腳本合並的行數時,我得到4作為輸出而不是6。
我有一個示例代碼,它將fiel作為輸入。它將計算所有值的第3列中的值並將其存儲在地圖中。 它通過計算excel文件中的空白值並在每個有效值處重置計數器來實現
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 {
}
}
}
下面的代碼將確定列中colded單元格的數量 - colName,開始行為startingRow
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.