【问题标题】:How can I print the passing string row by row in excel如何在excel中逐行打印传递的字符串
【发布时间】:2019-11-22 12:57:49
【问题描述】:

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

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

我想看

【问题讨论】:

  • 这里有什么问题?
  • 我需要逐行打印值,目前,它在同一行打印并相互覆盖

标签: java selenium katalon-studio


【解决方案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();
    }
}

【讨论】:

    【解决方案2】:

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

    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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多