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

Apache POI>无法读取Excel表

商经业
2023-03-14

Apache POI>无法读取Excel表

    null
        priceband = p.getProperty("priceband");
    if(priceband.contains("nationala")) {
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"NationalA");
        ExcelUtils.setCellData("example22222", 1, 1);
    }
    if(priceband.contains("nationala")) {
    ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"NationalB");
    ExcelUtils.setCellData("example22222", 1, 1);
}
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
public FileInputStream fis = null;
public FileOutputStream fileOut = null;
public static String priceband;

// This method is to set the File path and to open the Excel file, Pass
// Excel Path and Sheetname as Arguments to this method
public static void setExcelFile(String Path, String SheetName) throws Exception {
    try {
        // Open the Excel file
        FileInputStream ExcelFile = new FileInputStream(Path);
        // Access the required test data sheet
        ExcelWBook = new XSSFWorkbook(ExcelFile);
        ExcelWSheet = ExcelWBook.getSheet(SheetName);
    } catch (Exception e) {
        throw (e);
    }
}

// This method is to read the test data from the Excel cell, in this we are
// passing parameters as Row num and Col num
public static String getCellData(int RowNum, int ColNum) throws Exception {
    try {
        Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
        String CellData = Cell.getStringCellValue();
        return CellData;
    } catch (Exception e) {
        return "";
    }
}

// This method is to write in the Excel cell, Row num and Col num are the
// parameters
public static void setCellData(String Result, int RowNum, int ColNum) throws Exception {
    try {
        Row = ExcelWSheet.getRow(RowNum);
        Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
        if (Cell == null) {
            Cell = Row.createCell(ColNum);
            Cell.setCellValue(Result);
        } else {
            Cell.setCellValue(Result);
        }

        // Constant variables Test Data path and Test Data file name
        FileOutputStream fileOut = new FileOutputStream(Constant.Path_TestData + Constant.File_TestData);
        ExcelWBook.write(fileOut);
        fileOut.flush();
        fileOut.close();

    } catch (Exception e) {
        throw (e);
    }
}

public static void setupExcelPriceband() throws Exception {
    Properties p = new Properties();
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
    p.load(fi);
    priceband = p.getProperty("priceband");
    if(priceband.contains("nationala")) {
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"NationalA");
        ExcelUtils.setCellData("example22222", 1, 1);
    }
    else if (priceband.contains("nationalb")) {
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData, "NationalB");
        ExcelUtils.setCellData("df", 1, 1);
    }else {
    }
}

}

Excel文件

共有1个答案

翟缪文
2023-03-14

@Potnuru Ravi感谢您的帮助

下面的代码似乎已经完成了这个技巧:

public void setCellData(String sheetName, int colNum, int rowNum, String str) {
    int index = workbook.getSheetIndex(sheetName);
    sheet = workbook.getSheetAt(index);
    row = sheet.getRow(rowNum);
    cell = row.createCell(colNum);
    cell.setCellValue(str);
    try {
        fileOut = new FileOutputStream(path);

        try {
            workbook.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
 类似资料:
  • 问题内容: 尝试使用ApachePOI打开Excel时,我得到 我检查了。没有创建这样的文件夹。我正在使用Apache POI 3.6版。 有什么帮助吗?类似的代码在其他工作区中运行良好。在这里不知所措。 码: 问题答案: 您为什么要制作一个非常好的文件,将其包装在中,然后要求POI必须为您缓冲整个文件,以便可以进行随机访问?如果直接将文件直接传递给POI,生活会好很多,因此可以根据需要跳过它!

  • 嗨,我想在第一行搜索一个字符串,如果找到了,我想移动那一列。

  • 似乎这里和这里已经有了答案,但是,这些解决方案都不适合我。 我正在努力 不管怎样,最后仍然包含第一张而不是第二张。我使用的是Python 3和Pandas 0.20。1(水蟒发行版)。我错过了什么?如何将第二张图纸加载到?

  • 我正在写一个程序,它需要从excel文件中读取和写入数据,而不考虑格式(xls或xlsx)。 我知道ApachePOI,但它似乎有不同的类来处理xls文件(HSSF)和xlsx(XSSF)文件。 任何人都知道我将如何实现我在这里的目标。(也欢迎使用POI以外的API的想法)。

  • 我是编程界的新手。嗯,我正在尝试使用ApachePOI库读取excel文件(5行5列)。我实际上有两个相同问题的实现。在第一个代码片段中,我只是读取excel文件并将其打印到控制台中。 然而,现在我正试图将读取的excel数据保存到一个数组中。所以我想在动态获取excel行和列大小后设置数组大小。但令我惊讶的是,当我执行第二个代码段时,似乎“while(cellIterator.hasNext()

  • 介绍 读取Excel内容的封装,通过构造ExcelReader对象,指定被读取的Excel文件、流或工作簿,然后调用readXXX方法读取内容为指定格式。 使用 读取Excel中所有行和列,都用列表表示 ExcelReader reader = ExcelUtil.getReader("d:/aaa.xlsx"); List<List<Object>> readAll = reader.read(