第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Apache poi - 在現(xiàn)有 .xlsx 文件中添加新頁(yè)面

Apache poi - 在現(xiàn)有 .xlsx 文件中添加新頁(yè)面

有一個(gè)測(cè)試產(chǎn)品列表頁(yè)面的腳本。在此腳本期間,網(wǎng)頁(yè)中的數(shù)據(jù)(以包含名稱和價(jià)格的兩個(gè)列表的形式)應(yīng)兩次傳輸?shù)?.xlsx 文件,每次傳輸?shù)叫鹿ぷ鞅?。問題是 xlsx 文件在第二次調(diào)用后被覆蓋。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 回答

?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

代碼行Workbook wb = new XSSFWorkbook();總是創(chuàng)建一個(gè)新的空工作簿。然后您的代碼在其中創(chuàng)建一張工作表并將包含一張工作表的工作簿寫入文件。所以很明顯,結(jié)果總是一個(gè)文件,其中包含一個(gè)工作簿,其中有一個(gè)工作表。


您需要檢查是否已經(jīng)有一個(gè)文件。如果是這樣,則從Workbook該文件創(chuàng)建。然后您將擁有部分填寫的工作簿。當(dāng)然你還需要檢查工作簿中是否已經(jīng)存在工作表名稱,因?yàn)椴荒軇?chuàng)建兩個(gè)具有相同名稱的工作表。


...

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

...

因?yàn)槲艺J(rèn)為您的代碼中還有其他問題。我不會(huì)命名一個(gè)main不是真正的主要方法的方法Java。并且創(chuàng)建單元格內(nèi)容只需要一個(gè)循環(huán)。所以我將提供一個(gè)完整的例子:


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

 }

}



查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)