Apache Poi-在现有.xlsx文件中添加新页面

时间:2019-08-18 18:13:09

标签: java testing automated-tests apache-poi

有一个脚本可以测试产品列表页面。 在执行此脚本期间,应将网页中的数据(以名称和价格为两个List的形式)两次传输到.xlsx文件,每次都传输到新表。

问题是第二次调用后xlsx文件被覆盖。 SmartsPopular工作表消失,而显示Smarts 3-6 K。

public class Script
    @Test
    public void script3() throws IOException {
    openSmartphones();
    moreGoodsClick();
    moreGoodsClick();

    FileExcelCreating.main("SmartsPopular", goodsNamesListCreating, goodsPricesListCreating);

    moreGoodsClick();
    moreGoodsClick();

    FileExcelCreating.main("Smarts 3-6 K", goodsNamesListCreating, goodsPricesListCreating);
---------------------------------------------------------------------------------------------------------
public class FileExcelCreating 
    public static void main(String sheetName, List<String> goodsNames, List<String> goodsPrices) throws IOException {

        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet(sheetName);

        Row r0 = sheet.createRow(0);
        Cell c0 = r0.createCell(0);
        c0.setCellValue("Name");
        Cell c1 = r0.createCell(1);
        c1.setCellValue("Price");

        Row a;

        List<Integer> goodsPricesInt = new ArrayList<>();
        for(String s : goodsPrices) goodsPricesInt.add(Integer.valueOf(s));

        for (int i = 0; i < goodsNames.size(); i++) {
            a = sheet.createRow(i+1);
            String name = goodsNames.get(i);
            a.createCell(0).setCellValue(name);
        }

        for (int j = 0; j < goodsPricesInt.size(); j++) {
            a = sheet.getRow(j+1);
            Integer price = goodsPricesInt.get(j);
            a.createCell(1).setCellValue(price);
        }

        sheet.setAutoFilter(CellRangeAddress.valueOf("A1:B" + (goodsPricesInt.size())));

        FileOutputStream outputStream = new FileOutputStream  ("/FilesTXT/Smartphones.xlsx");

        wb.write(outputStream);
        outputStream.close();
    }

1 个答案:

答案 0 :(得分:0)

代码行Workbook wb = new XSSFWorkbook();始终创建一个新的空工作簿。然后,您的代码在其中创建一个工作表,并将具有一个工作表的工作簿写入文件中。因此,很明显,结果始终是一个文件,其中有一个工作簿,其中有一个一个工作表。

您需要检查是否已经有文件。如果是这样,则从该文件创建Workbook。然后,您将获得部分填充的工作簿。当然,然后您还需要检查工作簿中是否存在工作表名称,因为一个人无法创建两个具有相同名称的工作表。

...
private static final String fileName = "./FilesTXT/Smartphones.xlsx";
...

...
Workbook wb = null;
File file = new File(fileName);
if(file.exists()) {
 wb = WorkbookFactory.create(new FileInputStream(file));
} else {
 wb = new XSSFWorkbook();
}  

Sheet sheet = wb.getSheet(sheetName); if(sheet == null) sheet = wb.createSheet(sheetName);
...

由于我认为您的代码中还有其他问题。我不会将方法main命名为Java,这实际上不是主要方法。创建单元格内容仅需要一个循环。因此,我将提供一个完整的示例:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.List;
import java.util.ArrayList;

public class TestScript {
 public static void main(String[] args) throws Exception {

  List<String> goodsNamesListCreating = new ArrayList<String>();
  goodsNamesListCreating.add("SmartsPopular Name 1");
  goodsNamesListCreating.add("SmartsPopular Name 2");
  goodsNamesListCreating.add("SmartsPopular Name 3");

  List<String> goodsPricesListCreating = new ArrayList<String>();
  goodsPricesListCreating.add("123");
  goodsPricesListCreating.add("456");
  goodsPricesListCreating.add("789");

  FileExcelCreating.create("SmartsPopular", goodsNamesListCreating, goodsPricesListCreating);

  goodsNamesListCreating = new ArrayList<String>();
  goodsNamesListCreating.add("Smarts 3-6 K Name 1");
  goodsNamesListCreating.add("Smarts 3-6 K Name 2");
  goodsNamesListCreating.add("Smarts 3-6 K Name 3");
  goodsNamesListCreating.add("Smarts 3-6 K Name 4");

  goodsPricesListCreating = new ArrayList<String>();
  goodsPricesListCreating.add("321");
  goodsPricesListCreating.add("654");
  goodsPricesListCreating.add("987");

  FileExcelCreating.create("Smarts 3-6 K", goodsNamesListCreating, goodsPricesListCreating);

 }
}

class FileExcelCreating {

 private static final String fileName = "./FilesTXT/Smartphones.xlsx";

 public static void create(String sheetName, List<String> goodsNames, List<String> goodsPrices) throws Exception {

  Workbook wb = null;
  File file = new File(fileName);
  if(file.exists()) {
   wb = WorkbookFactory.create(new FileInputStream(file));
  } else {
   wb = new XSSFWorkbook();
  }  

  Sheet sheet = wb.getSheet(sheetName); if(sheet == null) sheet = wb.createSheet(sheetName);

  Row row = sheet.createRow(0);
  Cell cell = row.createCell(0);
  cell.setCellValue("Name");
  cell = row.createCell(1);
  cell.setCellValue("Price");

  List<Integer> goodsPricesInt = new ArrayList<>();
  for(String s : goodsPrices) goodsPricesInt.add(Integer.valueOf(s));

  for (int i = 0; i < goodsNames.size(); i++) {
   row = sheet.createRow(i+1);
   String name = goodsNames.get(i);
   row.createCell(0).setCellValue(name);
   Integer price = (i < goodsPricesInt.size())?goodsPricesInt.get(i):null;
   if (price != null) row.createCell(1).setCellValue(price);
  }

  sheet.setAutoFilter(CellRangeAddress.valueOf("A1:B" + goodsNames.size()));

  FileOutputStream outputStream = new FileOutputStream(file);

  wb.write(outputStream);
  outputStream.close();
  wb.close();
 }
}