I need to write a pass fail in excel at a specified column using POI

岁酱吖の 提交于 2020-01-06 08:49:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!