当前位置: 首页 > 知识库问答 >
问题:

Apache POI setCellData实际上没有设置单元格数据

孟晋
2023-03-14

使用Apache POI 3.16 Eclipse IDE neon 3 Selenium 3.4(在本例中并不重要)

我有一个问题,写值到一个excel电子表格,然后读回值。

以下是我想在高水平上做的事情:

    null
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Udemy_Excel_Driven {

    public static XSSFWorkbook wb;
    public static XSSFSheet sheet;
    public static XSSFRow row;
    public static XSSFCell cell;
    public static FileInputStream fis;

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

        System.out.println("Before cell edit value is:");

        System.out.println(getCellData(1,1));



        String value = setCellData(1,1,"Hello World");

        System.out.println("What's the value after setting it with setCellData()?");                
        System.out.println(value);
        System.out.println("What's the value using getCellData()?");
        System.out.println(getCellData(1,1));
    }

    public static String getCellData(int rowNum, int colNum) throws IOException
    {

        /*
         * Hierarchy of excel data:
         * 
         * Workbook - take control of this
         * Sheet - pick up the sheet of the workbook
         * Row - pick the row
         * Column - after picking the row, select the column
         * Value - grab the value from the cell 
         * 
         */


        //0. = identify the path to the excel file in the system.
        fis = new FileInputStream("C:\\data.xlsx");

        //1. Create a new XSSFWorkbook object.  You need to pass in a FileInputStream object into it, which you created earlier.
         wb = new XSSFWorkbook(fis);

        //2. Get the sheet in the workbook.  Create a new XSSFsheet object and set it to the sheet in the workbook
        // Access the workbook method "getSheet" and pass in the name of the sheet
        sheet = wb.getSheet("script");

        //3. Get the row and column.  We are going to access the data from row 2 column 2.  And remember the indices start at 0.  
         row = sheet.getRow(rowNum);
         cell = row.getCell(colNum);
        //get the value specified in the row and cell
         return cell.getStringCellValue();
    }



    public static String setCellData(int rowNum, int colNum, String data) throws IOException
    {

        fis = new FileInputStream("C:\\data.xlsx");
         wb = new XSSFWorkbook(fis);
        sheet = wb.getSheet("script");
         row = sheet.getRow(rowNum);
         cell = row.getCell(colNum);

          cell.setCellValue(data);
          String cellData = cell.getStringCellValue();
         return cellData;

    }
Before cell edit value is:
B2
What's the value after setting it with setCellData()?
Hello World
What's the value using getCellData()?
B2

我不认为写实际上发生了,因为我打开了excel文件,“Hello World”字符串不在指定的单元格中。这个问题有答案吗?

共有1个答案

景正文
2023-03-14

我没有看到您的代码中有任何部分实际上写到您的文件中。它或多或少应该是这样的:

FileOutputStream fileOut = new FileOutputStream("C:\\data.xlsx");
wb.write(fileOut);
fileOut.close();

您也可以参考本指南来了解此问题以及您可能感兴趣实现的其他功能。

 类似资料: