naresh
naresh

Reputation: 11

Related on Custom Timer Job in sharepoint 2010

How to create a custom job to export an Excel file in a list which has only 2 columns(Title,Description) in a sharepoint 2010?i want the coding part of this question?

Reading Data from Excel and writing into a sharepoint list,this has to be done through custom job coding

Thanks in Advance... Naresh

Upvotes: 0

Views: 761

Answers (3)

shabos
shabos

Reputation: 108

Use OpenXMLSDK - a free download that needs to be installed on the server.

    [...]
    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!


                    }
                }
            }
         }
     }
  }
}

I recommend putting this code into an event receiver on the document library (as seen above).

Upvotes: 1

Stefan
Stefan

Reputation: 14880

Open the Excel File
Take a look at the Excel Services for SharePoint 2010. There is a walkthrough explaining the required steps to open an excel file.

SharePoint Custom Timer Job
To create a custom SharePoint timer job, you have to create a class that inherits from SPJobDefinition. A complete tutorial can be found in this blog post: Creating Custom Timer Job in SharePoint 2010.

Upvotes: 0

Madhur Ahuja
Madhur Ahuja

Reputation: 22709

Have you looked at Excel REader for .NET

http://exceldatareader.codeplex.com/

Upvotes: 0

Related Questions