protected void btnupload_Click(object sender, ImageClickEventArgs e) { UploadDetails.SaveAs(Server.MapPath(UploadDetails.FileName)); string fileName = UploadDetails.ResolveClientUrl(UploadDetails.PostedFile.FileName); ViewState["FileName"] = fileName; System.Data.DataTable dtAppDesign = new System.Data.DataTable(); string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileName) + ";Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'"; OleDbConnection con = new OleDbConnection(SourceConstr); string finalFileNameWithPath = string.Format(Server.MapPath(fileName)); FileInfo workBook = new FileInfo(Server.MapPath(fileName)); System.Data.DataTable dtItem = new System.Data.DataTable(); string queryItem = "Select * from [Items$]"; OleDbDataAdapter dataItem = new OleDbDataAdapter(queryItem, con); dataItem.Fill(dtItem); dtItem.Columns.Add("Name"); dtItem.Columns.Add("Desc"); dtItem.Columns.Add("Pic"); ImportItemsOnly(dtItem, fileName); } private void ImportItemsOnly(System.Data.DataTable dtItem, string fileName) { try { FileInfo workBook = new FileInfo(Server.MapPath(fileName)); using (ExcelPackage xlPackage = new ExcelPackage(workBook)) { ExcelWorksheet worksheetItemOption = xlPackage.Workbook.Worksheets["CommonMenuItems"]; for (int i = 0; i < dtItem.Rows.Count; i++) { try { if (dtItem.Rows[i][1].ToString() != "" || dtItem.Rows[i][1].ToString() != "") { try { postImportRequest.CommonMenu = new CommonMenu(); postImportRequest.Action = "InsertCommonItems"; if (dtItem.Rows[i]["ItemName"].ToString() != "") { postImportRequest.CommonMenu.ItemName = dtItem.Rows[i]["ItemName"].ToString(); } if (dtItem.Rows[i]["Description"].ToString() != "") { postImportRequest.CommonMenu.Description = dtItem.Rows[i]["Description"].ToString(); } if (dtItem.Rows[i]["Image"].ToString() != "") { string ImgfileName = dtItem.Rows[i]["Image"].ToString(); string fileExtension = Path.GetExtension(dtItem.Rows[i]["Image"].ToString()); string NewFileName = GenerateCode() + fileName; byte[] buffer = File.ReadAllBytes(ImgfileName); postImportRequest.CommonMenu.Image = buffer; } postImportResponse = ImportService.CommonItems(postImportRequest); if (postImportResponse.SuccessMsg.ResponseErrors[0].SuccessMsg == "Successfully Inserted") { lblmsg.Text = "Successfully Imported"; } } } xlPackage.Save(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)