Thursday 9 March 2017

Read From Excel and update in a list

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data;

using System.IO;
using System.Xml;
using System.Net;
using Excel;
using System.Data.SqlClient;
//using Excel = Microsoft.Office.Interop.Excel;
namespace Solstad_ReadFromExcelUpdateCharteringContarct
{
    class Program
    {
        static void Main(string[] args)
        {
            AddIntoDB();
            return;
            FileStream stream = File.Open("C:\\CCS\\Console Applications\\Solstad\\HSE_PPIR.xlsx", FileMode.Open, FileAccess.Read);
                    DataSet result = null;
                        System.Data.DataTable dtResult = null;
                        //Reading Excel file from document library.

                        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                        excelReader.IsFirstRowAsColumnNames = true;

                        //Converting it into Dataset.
                        result = excelReader.AsDataSet();
                        dtResult = result.Tables[0];
                        //  saasWeb.AllowUnsafeUpdates = true;
                        for (int i = 0; i < dtResult.Rows.Count; i++)
                        {
                            //Parent List
                            //string parentList= "Chartering Contract";
                            string parentList = "";
                            //string childList = "Vessel Chartering Contract";
                            string childList = "";
                           
                            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                using (SPSite site = new SPSite(""))
                                {
                                    SPWeb web = site.OpenWeb();
                                    SPList spList = web.Lists[parentList];
                                    SPQuery spQItems = new SPQuery();
                                    spQItems.Query = "<Where><Eq><FieldRef Name='Kontrakts_x0020_ID' /><Value Type='Text'>"+Convert.ToString(dtResult.Rows[i]["Contract ID"])+"</Value></Eq></Where>";
                                    SPListItemCollection items = spList.GetItems(spQItems);
                                    if (items.Count > 0)
                                    {
                                        foreach (SPListItem item in items)
                                        {
                                            item["ApplyStaircaserates"] = false;
                                            using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
                                            {
                                                item.SystemUpdate(false);
                                            }

                                            SPList lstChildList = web.Lists["Vessel Chartering Contract"];
                                            SPQuery spqChild = new SPQuery();
                                            spqChild.Query = "<Where><Eq><FieldRef Name='Contract_x0020_ID' /><Value Type='Text'>" + Convert.ToString(item["ID"]) + "</Value></Eq></Where>";
                                            SPListItemCollection childItemsColn = lstChildList.GetItems(spqChild);
                                            if (childItemsColn.Count > 0)
                                            {
                                                foreach (SPListItem childItem in childItemsColn)
                                                {
                                                    childItem["ApplyStaircaserates"] = false;
                                                    using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
                                                    {
                                                        childItem.SystemUpdate(false);
                                                        Console.WriteLine("ID:" + Convert.ToString(item["ID"]));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            });
         
         
                        }

        }
        public static void AddIntoDB()
        {
            FileStream stream = File.Open("C:\\CCS\\Console Applications\\Solstad\\HSE_PPIR.xlsx", FileMode.Open, FileAccess.Read);
            DataSet result = null;
            System.Data.DataTable dtResult = null;
            //Reading Excel file from document library.

            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            excelReader.IsFirstRowAsColumnNames = true;

            //Converting it into Dataset.
            result = excelReader.AsDataSet();
            dtResult = result.Tables[0];
            if (dtResult.Rows.Count > 0)
            {
                for (int i = 0; i < dtResult.Rows.Count; i++)
                {
                    string strVessel = Convert.ToString(dtResult.Rows[i]["Column1"]);
                    string strPPIR = Convert.ToString(dtResult.Rows[i]["Column11"]);
                    if (strVessel.Trim() != "")
                    {

                    }
                }
            }
        }
     
   
    }
    class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
    {
        public DisabledItemEventsScope()
        {
            base.DisableEventFiring();
        }

        #region IDisposable Members

        public void Dispose()
        {
            base.EnableEventFiring();
        }

        #endregion
    }
}

No comments:

Post a Comment