无法在Excel中设置单元格数据

问题描述:

我创建了以下函数来设置Excel中的单元格数据,但是当我设置数据时。该文件是越来越corupted和我线程获得的errror为异常 “主要” java.lang.NoClassDefFoundError:组织/ openxmlformats /模式/ SpreadsheetML中/ x2006 /主/ CTDxfs $ 1 My Jars无法在Excel中设置单元格数据

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; 
} 
+0

发布全班 –

+0

请认真填写错误的完整堆栈跟踪。 –

您需要read the Apache POI FAQ!。具体而言,从Apache POI FAQ entry on "I'm using the poi-ooxml-schemas jar, but my code is failing with "java.lang.NoClassDefFoundError: org/openxmlformats/schemas/something"

To use the new OOXML file formats, POI requires a jar containing the file format XSDs, as compiled by XMLBeans. These XSDs, once compiled into Java classes, live in the org.openxmlformats.schemas namespace.

There are two jar files available, as described in the components overview section. The full jar of all of the schemas is ooxml-schemas-1.3.jar, and it is currently around 15mb. The smaller poi-ooxml-schemas jar is only about 4mb. This latter jar file only contains the typically used parts though.

Many users choose to use the smaller poi-ooxml-schemas jar to save space. However, the poi-ooxml-schemas jar only contains the XSDs and classes that are typically used, as identified by the unit tests. Every so often, you may try to use part of the file format which isn't included in the minimal poi-ooxml-schemas jar. In this case, you should switch to the full ooxml-schemas-1.3.jar. Longer term, you may also wish to submit a new unit test which uses the extra parts of the XSDs, so that a future poi-ooxml-schemas jar will include them.

所以,短期内,你只需要从poi-ooxml-schemas罐子切换到大(和完整)ooxml-schemas罐子ooxml-schemas-1.3.jar。长远来说,您需要向使用您所需的CT类的Apache POI项目提交junit测试,然后该类将自动包含在未来发行版中较小的poi-ooxml-schemas jar中。