我需要使用 POI 在指定列的 excel 中写一个 pass fail [英] I need to write a pass fail in excel at a specified column using POI

查看:43
本文介绍了我需要使用 POI 在指定列的 excel 中写一个 pass fail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的 - 所以我意识到在编写一些代码并弄清楚之后,jxl 不支持 xlsx,只支持 POI (XSSF).好的.我想要做的是在指定的列中搜索具有字符串值Result"的单元格.一旦我发现,然后当测试通过或失败时,它会在空白的单元格中写入通过"或失败".它将继续这样做,直到测试集完成.这是我用jxl写的代码.

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();

上面很简单,我找到了字符串,然后获取该列名并根据长度写入通过或失败.关于如何使用 POI 做到这一点的任何想法?

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?

推荐答案

这也很简单.只需将 jxl 类/方法替换为它们的 POI 对应项:

That's pretty straightforward, too. Simply replace jxl classes/methods with their POI counterparts:

POI 有一个 XSSFWorkbook使用 XSSFWorkbook(java.io.File file) 构造函数和 XSSFSheet getSheet(java.lang.String name) 方法.

POI has an XSSFWorkbook with an XSSFWorkbook(java.io.File file) constructor and an XSSFSheet getSheet(java.lang.String name) method.

XSSFSheet 有一个 XSSFRow getRow(int rownum) 方法(例如循环直到 getLastRowNum()).

XSSFSheet has an XSSFRow getRow(int rownum) method (loop until getLastRowNum() for instance).

XSSFRow 有一个 XSSFCell getCell(int cellnum, ...) 方法.

XSSFCell 有合适的 getter/setter.

XSSFCell has appropriate getters/setters.

这篇关于我需要使用 POI 在指定列的 excel 中写一个 pass fail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆