Method to read file from upload control.
- if (fuExcelUpload.HasFile) {
- string strPath = "C:\\UploadExcel";
- string SheetName = "Upload";
- Directory.CreateDirectory(strPath);
- strPath = strPath + "\\";
- fuExcelUpload.SaveAs(strPath + fuExcelUpload.FileName);
- strPath = strPath + fuExcelUpload.FileName;
- DataSet ds = LoadExcel(strPath, SheetName);
- DataTable dt = ds.Tables[0];
- InsertIntoList(dt);
- }
Method to insert excel data into SharePoint List.
- private void InsertIntoList(DataTable listTable) {
- try {
- SPSecurity.RunWithElevatedPrivileges(delegate() {
- using(SPSite site = new SPSite(SPContext.Current.Web.Url)) {
- site.AllowUnsafeUpdates = true;
- using(SPWeb web = site.OpenWeb()) {
- web.AllowUnsafeUpdates = true;
- SPList lstRMD = web.Lists["Your List Name"];
-
- for (int iRow = 0; iRow < listTable.Rows.Count; iRow++) {
- SPListItem revItem1 = lstRMD.Items.Add();
- revItem1["Field1"] = Convert.ToString(listTable.Rows[iRow][0]);
- revItem1["Field2"] = Convert.ToString(listTable.Rows[iRow][1]);
- revItem1.Update();
- }
- web.AllowUnsafeUpdates = false;
- }
- site.AllowUnsafeUpdates = false;
- }
-
- });
-
- } catch (Exception ex) {}
-
- }