gpt4 book ai didi

java - 无法写入 Excel (Selenium WebDriver) - Apache POI

转载 作者:行者123 更新时间:2023-12-02 03:34:22 25 4
gpt4 key购买 nike

我是 Selenium Web 自动化的新手,请对我温柔点。

我创建了一种将数组内容写入 Excel 工作表的方法。我没有遇到任何异常或错误,并且没有看到数据写入 Excel 工作表。

excel工作表名称-mysheet.xlsxExcel 工作簿中的工作表名称:“FirstLevelMenu”

public class WriteExcelData {
XSSFWorkbook wb;
XSSFSheet sheet;

public void writeData(String path, String sheetName, String[] data) {

try {
File src = new File(path);
FileInputStream fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
sheet = wb.getSheet(sheetName);
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
Row row = sheet.getRow(0);
Row newRow = sheet.createRow(rowCount + 1);
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell col = newRow.createCell(j);
col.setCellValue(data[j]);
}
fis.close();
FileOutputStream fout = new FileOutputStream(src);
wb.write(fout);
fout.close();
} catch (Exception e) {
e.getMessage();
}
}

public static void main(String[] args) {
WriteExcelData test=new WriteExcelData();
String[] data=new String[2];
data[0]="cat";
data[1]="cat";

test.writeData("C:\\mysheet.xlsx", "FirstLevelMenu", data);
}
}

最佳答案

由于您正在使用新的 xlsx 工作表进行编写,请尝试下面的代码...我相信它会起作用:)

public static void main(String[] args) throws InvalidFormatException, IOException{

FileInputStream fis=new FileInputStream("D://Data.xlsx");
XSSFWorkbook wb= new XSSFWorkbook(fis);

//XSSFSheet sh= wb.getSheetAt(0); Or
XSSFSheet sh = wb.createSheet("Test");
XSSFRow row=sh.createRow(0);
XSSFCell cell= row.createCell(0);

//cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue("Ish Mishra");

FileOutputStream fos=new FileOutputStream("D:\\Data.xlsx");
wb.write(fos);
fos.close();

System.out.println("Excel File Written successfully");

关于java - 无法写入 Excel (Selenium WebDriver) - Apache POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37616642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com