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();
}
}
添加回答
舉報(bào)