Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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中设置单元格数据_Java_Apache Poi - Fatal编程技术网

Java 无法在excel中设置单元格数据

Java 无法在excel中设置单元格数据,java,apache-poi,Java,Apache Poi,我已经创建了下面的函数来在excel中设置单元格数据,但是当我设置数据时。文件被破坏,线程“main”java.lang.NoClassDefFoundError:org/openxmlformats/schemas/spreadsheetml/x2006/main/CTDxfs$1中出现异常 public boolean setCellData(String sheetName、String colName、int rowNum、String data){ 试一试{ fis=新文件输入流(路

我已经创建了下面的函数来在excel中设置单元格数据,但是当我设置数据时。文件被破坏,线程“main”java.lang.NoClassDefFoundError:org/openxmlformats/schemas/spreadsheetml/x2006/main/CTDxfs$1中出现异常

public boolean setCellData(String sheetName、String colName、int rowNum、String data){
试一试{
fis=新文件输入流(路径);
工作簿=新XSSF工作簿(fis);
如果(rowNum您需要。具体来说,从:

要使用新的OOXML文件格式,POI需要一个jar,其中包含由XMLBeans编译的文件格式XSD。这些XSD一旦编译成Java类,就位于org.openxmlformats.schemas命名空间中

有两个jar文件可用,如组件概述部分所述。所有模式的完整jar是ooxml-schemas-1.3.jar,目前约为15mb。较小的poi ooxml schemas jar仅约为4mb。但后一个jar文件仅包含通常使用的部分

许多用户选择使用较小的poi ooxml架构jar来节省空间。但是,poi ooxml架构jar仅包含单元测试所标识的通常使用的XSD和类。您可以尝试使用最小poi ooxml架构jar中未包含的部分文件格式。在这种情况下,您应该切换到完整的ooxml-schemas-1.3.jar。从长远来看,您可能还希望提交一个新的单元测试,该测试使用XSD的额外部分,以便将来的poi ooxml schemas jar将包含它们


因此,短期内,您只需要从
poioxml模式
jar切换到更大的(完整的)模式
ooxml模式
jar
ooxml-schemas-1.3.jar
。从长远来看,您需要向Apache POI项目提交一个junit测试,该项目使用所需的CT类,该类将在将来的版本中自动包含在较小的
POI ooxml模式
jar中。

发布整个类后请显示完整的堆栈跟踪错误的原因。
 public boolean setCellData(String sheetName,String colName,int rowNum, String data){
    try{
    fis = new FileInputStream(path); 
    workbook = new XSSFWorkbook(fis);

    if(rowNum<=0)
        return false;

    int index = workbook.getSheetIndex(sheetName);
    int colNum=-1;
    if(index==-1)
        return false;


    sheet = workbook.getSheetAt(index);


    row=sheet.getRow(0);
    for(int i=0;i<row.getLastCellNum();i++){
        //System.out.println(row.getCell(i).getStringCellValue().trim());
        if(row.getCell(i).getStringCellValue().trim().equals(colName))
            colNum=i;
    }
    if(colNum==-1)
        return false;

    sheet.autoSizeColumn(colNum); 
    row = sheet.getRow(rowNum-1);
    if (row == null)
        row = sheet.createRow(rowNum-1);

    cell = row.getCell(colNum); 
    if (cell == null)
        cell = row.createCell(colNum);


    cell.setCellValue(data);

    fileOut = new FileOutputStream(path);

    workbook.write(fileOut);

    fileOut.close();    

    }
    catch(Exception e){
        e.printStackTrace();
        return false;
    }
    return true;
}