修改包含元数据的Excel工作表 - 如何避免不必要的警告?

时间:2012-12-31 15:39:19

标签: java excel jxl

我已经获得了一份我必须填写的Excel工作表,所以我正在使用JExcel为我做这件事。 但是,当我使用JExcel只填充一个单元格时,它可以工作,但我得到了很多 不必要的警告滞后于一切(我假设它是因为excel文件中包含的所有元数据)。有没有人知道如何在不删除元数据的情况下避免这些警告? (我需要保留元数据,因为我认为解析另一端填充的excel工作表非常重要)

非常感谢您的帮助!

public static void main(String args[]){
    Workbook workbook;
    try{
        workbook = Workbook.getWorkbook(new File("Test.xls"));
        WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"), workbook);
        WritableSheet sheet1 = copy.getSheet(0); 
        Label label = new Label(4,5, "PO1234355");
        sheet1.addCell(label);


        copy.write(); 
        copy.close();

    }
    catch(Exception e){
        System.out.println("Could not get workbook. Error: " + e.toString());
    }
}

这些是我得到的疯狂警告:

Warning:  Cannot read name ranges for BUY - setting to empty
Warning:  Cannot read name ranges for Buyer - setting to empty
Warning:  Cannot read name ranges for Casual_Buyer - setting to empty
Warning:  Cannot read name ranges for Departments - setting to empty
Warning:  Cannot read name ranges for Dept_No - setting to empty
Warning:  Cannot read name ranges for Designer - setting to empty
Warning:  Cannot read name ranges for Model - setting to empty
Warning:  Cannot read name ranges for Seasons - setting to empty
Warning:  Cannot read name ranges for Supplier - setting to empty
Warning:  Cannot read name ranges for Technologist - setting to empty
Warning:  Cannot read name ranges for Typed_by - setting to empty
Warning:  Cannot read name ranges for YESNO - setting to empty
Warning:  no filename property for drawing
Warning:  no filename property for drawing
Warning:  no filename property for drawing
Warning:  no filename property for drawing
Warning:  no filename property for drawing
Warning:  no filename property for drawing
Warning:  no filename property for drawing
Warning:  no filename property for drawing
Warning:  no filename property for drawing

3 个答案:

答案 0 :(得分:1)

您可以考虑使用此代码将一个工作簿中的工作表复制到另一个工作簿中的新工作表,以避免此类警告。另请查看如何在jxl中使用WorkBookSettings,以帮助您避免这些警告。

for (int i = 0 ; i < numrows ; i++)
  {
    for (int j = 0 ; j < numcols ; j++)
    {
      readCell = sheet.getCell(i, j);
      newCell = readCell.copyTo(i, j);
      readFormat = readCell.getCellFormat();
      newFormat = new WritableCellFormat(readFormat);
      newCell.setCellFormat(newFormat);
      newSheet.add(newCell);
    }
  }

Here是该警告的源代码

String n = name != null ? name : builtInName.getName();
logger.warn("Cannot read name ranges for " + n + 
                  " - setting to empty");
NameRange range = new NameRange(0,0,0,0,0);
ranges.add(range);

希望您能够分析警告的原因。

答案 1 :(得分:1)

我有同样的警告。我删除了Excel中的所有命名范围(公式&gt;名称管理器)。 之后所有的警告都消失了。

答案 2 :(得分:1)

我的文件没有命名范围,我仍然有这个警告。 所以我尝试使用带有WorkbookSettings的构造函数来获取我的工作簿。它帮助了我。

WorkbookSettings ws = new WorkbookSettings();
ws.setSuppressWarnings(true);
Workbook workbook = Workbook.getWorkbook(new File("Stock labels.xls"), ws);