如何在Excel中逐行打印传递的字符串

问题描述 投票:2回答:2

我正在用excel打印一些字符串值

在excel中,最后一个字符串正在打印,但是我需要逐行打印它们

代码

for(String a:arrOfStr)
{
    System.out.println(a);
    //CustomKeywords.'WriteExcel.demoKey'(a)
    CustomKeywords.'WriteExcel.demoKey'(a)
}

关键字

@Keyword
    public void demoKey(String name) throws IOException{

        FileInputStream fis = new FileInputStream("C:\\Users\\lahiruh\\Katalon Studio\\Project Decypha\\Decypha data files\\Demo1.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fis);

        XSSFSheet sheet = workbook.getSheet("Sheet1");
        int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
        Row row = sheet.createRow(rowCount+);
        Cell cell = row.createCell(0);
        cell.setCellType(cell.CELL_TYPE_STRING);
        cell.setCellValue(name);
        FileOutputStream fos = new FileOutputStream("C:\\Users\\lahiruh\\Katalon Studio\\Project Decypha\\Decypha data files\\Demo1.xlsx");
        workbook.write(fos);
        fos.close();

现在我可以获得类似(覆盖每个值并给出最后一个字符串)的输出this

我想看到this的样子

java selenium katalon-studio
2个回答
1
投票

尝试此

for(int i = 0; i < arrOfStr.size(); i++)
{
    String a = arrOfStr[i];
    System.out.println(a);
    //CustomKeywords.'WriteExcel.demoKey'(a)
    CustomKeywords.'WriteExcel.demoKey'(a,i)
}

@关键字

public void demoKey(String name, int index) throws IOException{

        FileInputStream fis = new FileInputStream("path");
        XSSFWorkbook workbook = new XSSFWorkbook(fis);

        XSSFSheet sheet = workbook.getSheet("Sheet1");
        Row row = sheet.createRow(index + 1);
        Cell cell = row.createCell(0);
        cell.setCellType(cell.CELL_TYPE_STRING);
        cell.setCellValue(name);
        FileOutputStream fos = new FileOutputStream("path");
        workbook.write(fos);
        fos.close();
    }
}

0
投票

您必须按照以下方式进行。

  1. 首先创建一组行
  2. 创建单元格并定义单元格数据类型。
  3. 将值存储在单元格中

我在下面提供了用于逐行存储数据的结构。

int rowNumber = 0; // Row number starting with 0

for (i = 0 ; i < 10 ; i++) { //This for loop can be your data set

    Row row = sheet.createRow(rowNumber++);

    int columnNumber = 0; //Cell or Column number starting with 0
    for (j = 0 ; j < 5 ; j++) { //This for loop can be your data set
       Cell cell = row.createCell(columnNumber++);
       cell.setCellValue("Some string value");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.