Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法写入excel(Selenium WebDriver)-Apache POI_Java_Excel_Selenium Webdriver_Apache Poi - Fatal编程技术网

Java 无法写入excel(Selenium WebDriver)-Apache POI

Java 无法写入excel(Selenium WebDriver)-Apache POI,java,excel,selenium-webdriver,apache-poi,Java,Excel,Selenium Webdriver,Apache Poi,我是Selenium Web自动化的新手,请对我温柔一点 我创建了一个将数组内容写入excel工作表的方法。 我没有发现异常或错误,也没有看到数据写入excel工作表 excel工作表名称-mysheet.xlsx excel工作簿中的工作表名称:“FirstLevelMenu” 公共类WriteExcelData{ XSSFWB; XSSF表; public void writeData(字符串路径、字符串名称、字符串[]数据){ 试一试{ 文件src=新文件(路径); FileInputSt

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

我创建了一个将数组内容写入excel工作表的方法。 我没有发现异常或错误,也没有看到数据写入excel工作表

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

公共类WriteExcelData{
XSSFWB;
XSSF表;
public void writeData(字符串路径、字符串名称、字符串[]数据){
试一试{
文件src=新文件(路径);
FileInputStream fis=新的FileInputStream(src);
wb=新XSSF工作簿(fis);
sheet=wb.getSheet(sheetName);
int rowCount=sheet.getLastRowNum()-sheet.getFirstRowNum();
Row Row=sheet.getRow(0);
Row newRow=sheet.createRow(rowCount+1);
对于(int j=0;j

当您使用新的xlsx工作表来编写时,请尝试下面的代码…我相信它会起作用:)


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


这与硒有什么关系?这与硒有什么关系?谢谢@Grs007。当Data.xlsx文件存在时,上面的代码可以工作。当Data.xlsx不存在时,它不会自动创建。谢谢@Grs007。当Data.xlsx文件存在时,上面的代码可以工作。当Data.xlsx不存在时,它不会自动创建。
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);
    }
}
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");