2011-11-18 23 views
0

我想读取在itemadded事件上传的excel文件。我需要通过阅读上传的Excel表单来更新文档库的元数据列。但我得到以下错误,阅读excel附件itemAdded事件

enter image description here

这里是我的代码,

SPItem item = properties.ListItem; 

    string workbookpath = "http://server/sites/dev/Published%20documents/Test.xlsx"; 
    ApplicationClass excel = new ApplicationClass(); 
    excel.Visible = false; 

    Workbook excelWorkBook = excel.Workbooks.Open(workbookpath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); 

    Sheets sheets = excelWorkBook.Worksheets; 
    Worksheet worksheet = (Worksheet)sheets.get_Item(1); 
    Range range = worksheet.get_Range("A1", "A1"); 

    item["Status"] = (string)range.Cells.Value2; 

    item.Update(); 

    base.ItemAdded(properties); 

出于测试目的,我硬编码的Excel文件的路径。

回答

1

我认为你文档处于签出状态,因为你必须从Excel应用程序打开它,所以你需要做以下的问题:

  1. 关闭Excel应用
  2. 入住文档回去救chages或撤消签出(使用或item.UndoCheckOut()

此后,您应该能够更新项目属性