【发布时间】:2017-06-17 20:59:31
【问题描述】:
我正在使用 eclipse、selenium web 驱动程序、apache POI、java。我对这个和 java 很陌生。
我有一个包含 3 列的 xls 文件:电子邮件、密码、结果。我有 3 行数据。结果栏为空白。
每次迭代,脚本都应该读取电子邮件和密码,然后执行操作。然后将结果写入Result列。
继续直到到达最后一行。
我对列号进行了硬编码,它可以工作。我无法弄清楚如何找到带有标题“结果”的列号,然后如何在相应行的单元格中写入。
File file = new File("C://dd.xls");
try {
// Open the Excel file
FileInputStream fis = new FileInputStream(file);
// Access the required test data sheet
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheet("Sheet1");
int TotalRow;
int TotalCol;
TotalRow = sheet.getLastRowNum();
TotalCol = sheet.getRow(0).getLastCellNum();
System.out.println("row " + TotalRow + " col " + TotalCol);
// Loop through all rows in the sheet
// Start at row 1 as row 0 is our header row
for(int count = 1;count<=sheet.getLastRowNum();count++){
//for(int col = 1;col<=TotalCol;col++){
HSSFRow row = sheet.getRow(count);
System.out.println("Running test case " + row.getCell(0).toString() + " " + row.getCell(1).toString());
HSSFCell hSSFCell = row.getCell(TotalCol-1);
String value = count + "aa";
hSSFCell.setCellValue(value);}
// }
fis.close();
FileOutputStream outputStream = new FileOutputStream(file);
wb.write(outputStream);
outputStream.close();
} catch (IOException e) {
System.out.println("Test data file not found");
}
https://drive.google.com/open?id=1jqefYaffunknolrj6i4SXOoq2LiKT3xTgSsdzaxsBfQ
【问题讨论】:
-
从 A1 开始并读取单元格的文本(A2、A3 等)并在找到标题时中断???
标签: java