问题
Ok - so I realized that after writing some code and figuring out stuff, that jxl does not support xlsx, only POI (XSSF). Ok fine. What I am trying to do is search for a cell with a string value "Result" in a specified column. Once I find that, then as a test passes or fails, it writes "Pass" or "Fail" in the cell that is blank. And it will continue to do that until the test set has finished. Here is the code I wrote using jxl.
WritableWorkbook wb = Workbook.createWorkbook(new File(testData), workbook);
WritableSheet ws = wb.getSheet("OrderCreateQA3");
int colCount = ws.getColumns();
for(int j=0; j<colCount; j++){
Cell cell = ws.getCell(j,0);
if(cell.getContents().contains("Result")){
Cell[] cells = ws.getColumn(j);
int len = cells.length;
Label label = new Label(j,len, "Fail", getCellFormat(Colour.RED));
ws.addCell(label);
break;
}
}
wb.write();
wb.close();
The above was pretty straightforward, I find the string, then get that column name and write pass or fail given the length. Any ideas on how to do it using POI?
回答1:
That's pretty straightforward, too. Simply replace jxl
classes/methods with their POI counterparts:
POI has an XSSFWorkbook
with an XSSFWorkbook(java.io.File file)
constructor and an XSSFSheet getSheet(java.lang.String name)
method.
XSSFSheet
has an XSSFRow getRow(int rownum)
method (loop until getLastRowNum()
for instance).
XSSFRow
has an XSSFCell getCell(int cellnum, ...)
method.
XSSFCell
has appropriate getters/setters.
来源:https://stackoverflow.com/questions/26025413/i-need-to-write-a-pass-fail-in-excel-at-a-specified-column-using-poi