HSSF 如何使用 Constant Int 列号将结果写入 Excel
HSSF How to write result in Excel by using Constant Int column number
我是 Java 和 Selenium 的新手,Selenium 非常有趣,我正在使用 TestNG 数据驱动框架开发 Selenium WebDriver。
有一个 Excel 实用程序,可以使用 HSSF
将数据写入 excel
SuiteUtility.WriteResultUtility(FilePath, TestCaseName, "Pass/Fail/Skip", DataSet+1, "PASS");
我打算使用常量文件代替硬编码,而不是硬编码。例如,
public static final String KEYWORD_PASS = "PASS";
public static final int COL_TEST_CASE_RESULT = 10; // put it in column no 10
所以它会像这样并且变得更易于管理
SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_TEST_CASE_RESULT, DataSet+1, Constant.KEYWORD_PASS);
我的问题是如何修改下面的代码,以便我可以将 String colName 更改为 int column number。
public boolean writeResult(String wsName, String colName, int rowNumber, String Result){
try{
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return false;
int colNum = retrieveNoOfCols(wsName);
int colNumber=-1;
HSSFRow Suiterow = ws.getRow(0);
for(int i=0; i<colNum; i++){
if(Suiterow.getCell(i).getStringCellValue().equals(colName.trim())){
colNumber=i;
}
}
if(colNumber==-1){
return false;
}
HSSFRow Row = ws.getRow(rowNumber);
HSSFCell cell = Row.getCell(colNumber);
if (cell == null)
cell = Row.createCell(colNumber);
cell.setCellValue(Result);
opstr = new FileOutputStream(filelocation);
wb.write(opstr);
opstr.close();
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public void setData(String adr,String sht,int rn,int cn,String val) throws Exception{
FileInputStream fsIP= new FileInputStream(new File(adr)); //Read the spreadsheet that needs to be updated
XSSFWorkbook wb = new XSSFWorkbook(fsIP); //Access the workbook
XSSFSheet worksheet = wb.getSheet(sht); //Access the worksheet, so that we can update / modify it.
Cell cell = null; // declare a Cell object
cell = worksheet.getRow(rn).getCell(cn); // Access the second cell in second row to update the value
cell.setCellValue(val); // Get current cell value value and overwrite the value
fsIP.close(); //Close the InputStream
FileOutputStream output_file =new FileOutputStream(new File(adr)); //Open FileOutputStream to write updates
wb.write(output_file); //write changes
output_file.close(); //close the stream
}
可能会有帮助。
我是 Java 和 Selenium 的新手,Selenium 非常有趣,我正在使用 TestNG 数据驱动框架开发 Selenium WebDriver。
有一个 Excel 实用程序,可以使用 HSSF
将数据写入 excelSuiteUtility.WriteResultUtility(FilePath, TestCaseName, "Pass/Fail/Skip", DataSet+1, "PASS");
我打算使用常量文件代替硬编码,而不是硬编码。例如,
public static final String KEYWORD_PASS = "PASS";
public static final int COL_TEST_CASE_RESULT = 10; // put it in column no 10
所以它会像这样并且变得更易于管理
SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_TEST_CASE_RESULT, DataSet+1, Constant.KEYWORD_PASS);
我的问题是如何修改下面的代码,以便我可以将 String colName 更改为 int column number。
public boolean writeResult(String wsName, String colName, int rowNumber, String Result){
try{
int sheetIndex=wb.getSheetIndex(wsName);
if(sheetIndex==-1)
return false;
int colNum = retrieveNoOfCols(wsName);
int colNumber=-1;
HSSFRow Suiterow = ws.getRow(0);
for(int i=0; i<colNum; i++){
if(Suiterow.getCell(i).getStringCellValue().equals(colName.trim())){
colNumber=i;
}
}
if(colNumber==-1){
return false;
}
HSSFRow Row = ws.getRow(rowNumber);
HSSFCell cell = Row.getCell(colNumber);
if (cell == null)
cell = Row.createCell(colNumber);
cell.setCellValue(Result);
opstr = new FileOutputStream(filelocation);
wb.write(opstr);
opstr.close();
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public void setData(String adr,String sht,int rn,int cn,String val) throws Exception{
FileInputStream fsIP= new FileInputStream(new File(adr)); //Read the spreadsheet that needs to be updated
XSSFWorkbook wb = new XSSFWorkbook(fsIP); //Access the workbook
XSSFSheet worksheet = wb.getSheet(sht); //Access the worksheet, so that we can update / modify it.
Cell cell = null; // declare a Cell object
cell = worksheet.getRow(rn).getCell(cn); // Access the second cell in second row to update the value
cell.setCellValue(val); // Get current cell value value and overwrite the value
fsIP.close(); //Close the InputStream
FileOutputStream output_file =new FileOutputStream(new File(adr)); //Open FileOutputStream to write updates
wb.write(output_file); //write changes
output_file.close(); //close the stream
}
可能会有帮助。