java - POI中多列写入

标签 java excel apache-poi

我尝试写 2 List <WebElement> 使用 Apache POI 列出 Excel 文件。但我的方法只保存//column 2值(value)观。 //column 1数据未保存在 Excel 文件中。

请帮我解决这个问题。

public void FourthExcel(String classNameOne,String classNameTwo) {

            WebDriverWait wait = new WebDriverWait(driver, 40);
            wait.pollingEvery(2, TimeUnit.SECONDS);

            String filePath = System.getProperty("user.dir");

            //Book one
            XSSFWorkbook workbookOne = new XSSFWorkbook();
            XSSFSheet sheetOne = workbookOne.createSheet("Expenses Sheet");

            CommonClass.sleepTime(3000);

            WebElement expencesTble = driver.findElement(expencesTable);
            wait.until(ExpectedConditions.elementToBeClickable(expencesTble));

            List<WebElement> ColOneList = driver.findElements(By.className(classNameOne));
            List<WebElement> ColTwoList = driver.findElements(By.className(classNameTwo));

            List<Object> ColOneobjectList = Arrays.asList(ColOneList.toArray());
            List<Object> ColTwoobjectList = Arrays.asList(ColTwoList.toArray());

            int ColumnOneSize = ColOneList.size()-1;

            for (int i = 0; i <= ColumnOneSize; i++) {
                out.println("For");
                out.println(ColOneList.get(i).getText());

                //Column 1
                XSSFRow rowOneColZero = sheetOne.createRow(i);
                rowOneColZero.createCell(0).setCellValue(String.valueOf(ColOneobjectList.get(i)));

                //Column 2
                XSSFRow rowOneCOlOne = sheetOne.createRow(i);
                rowOneCOlOne.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i));
            }
            try {
                out.println("try");
                FileOutputStream fileOut = new FileOutputStream(filePath + "\\testExcel.xls");
                workbookOne.write(fileOut);
                workbookOne.close();

            } catch (IOException e) {
                out.println(e);

            }
        }

Excel 文件如下所示:

A1 data is not written into the excel file.

enter image description here

最佳答案

for循环中你重写了XSSFRow行对象,请这样写:

    for (int i = 0; i <= ColumnOneSize; i++) {
                    out.println("For");
                    out.println(ColOneList.get(i).getText());

                    //create row
                    XSSFRow row= sheetOne.createRow(i);
                    //to Column 1 
                    row.createCell(0).setCellValue(String.valueOf( ColOneobjectList.get(i)));        
                    //to Column 2
                    row.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i));
    }

关于java - POI中多列写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47827787/

相关文章:

java - WSO2 Carbon 管理控制台异常

java - Wicket:从表单设置 JodaDateTime

vba - 可以根据另一个单元格的值锁定单元格范围吗?

java - poi 和 poi-ooxml 有什么区别

java - 如何将图像放入excel java的单元格中?

java - Jasper 报告 JAVA Swing

java - Spring 无法使用 @ComponentScan 中带有星号的 REGEX 类型过滤器来包含/排除 Bean

sql - 使用excel连接远程sql数据库

c# - Solidworks 和 Excel C# 引用系列零件设计表

java - XWPFDocument 给出 NoClassDefFoundException :org. doc4j.NameSpace