2011-09-05 57 views
0

如何創建一個自定義作業來導出列表中只有2列(標題,說明)的Excel文件?我想要這個問題的編碼部分?關於SharePoint 2010中的自定義計時器作業

從Excel

讀取數據和寫入SharePoint列表,這可以通過自定義編碼工作提前

謝謝... 納雷什

+0

從Excel讀取數據並寫入分享點列表 – naresh

+0

您是否在SharePoint服務器上安裝了Excel?您需要使用數據訪問方法(例如Jet或ACE OLEDB提供程序或Excel ODBC驅動程序)或Excel自動化。 http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/b95d115d-9a66-4d6d-ba78-6061c19d4a58 –

回答

0

你有沒有看的Excel閱讀器.NET

完成

http://exceldatareader.codeplex.com/

+0

是的,我確實看到..但我想通過自定義的文件被寫入SharePoint列表中作業 – naresh

+0

不能使用sharepoint API嗎? http://msdn.microsoft.com/en-us/library/ms467435.aspx –

1

使用OpenXMLSDK - 需要在服務器上安裝的免費下載。

[...] 
    using DocumentFormat.OpenXml; 
    using DocumentFormat.OpenXml.Packaging; 
    using DocumentFormat.OpenXml.Spreadsheet; 

    public class OffBookAssetLibraryEventReceiver : SPItemEventReceiver 
    { 
     public override void ItemUpdated(SPItemEventProperties properties) 
     { 
     // This if statement is to work around the sharepoint issue of this event firing twice. 
     if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null) 
     { 

    byte[] workSheetByteArray = properties.ListItem.File.OpenBinary(); 

    Stream stream = new MemoryStream(workSheetByteArray); 

    Package spreadsheetPackage = Package.Open(stream, FileMode.Open, FileAccess.ReadWrite); 

    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(spreadsheetPackage); 

    SharedStringTablePart shareStringTablePart = spreadsheetDocument.WorkbookPart.SharedStringTablePart; 

    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets; 

    try 
     { 
      foreach (Sheet sheet in sheets) 
      { 
       var worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id.Value); 

       IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>(); 

       if (rows.Count() > 0) 
       { 
        int rowNumber = 0; 

        foreach (Row row in rows) 
        { 
         IEnumerable<Cell> cells = row.Elements<Cell>(); 
         Cell title = null; 
         Cell description = null; 

         title = cells.ToArray()[0]; 
         description = cells.ToArray()[1]; 

         // This is the code used to extract cells from excel that are NOT inline (Inline cells are decimal and dates - although dates are stored as int) 
         int index = int.Parse(title.CellValue.Text); 
         string titleString = spreadsheetDocument.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(index).InnerText; 

         index = int.Parse(description.CellValue.Text); 
         string descriptionString = spreadsheetDocument.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(index).InnerText; 

         //Insert into your sharepoint list here! 


        } 
       } 
      } 
     } 
    } 
    } 
} 

我建議把這個代碼插入文檔庫的事件接收器(如上面所看到的)。

相關問題