EPPLUS - JackHu88/Comm GitHub Wiki

Read data from Excle sheet in sharepoint online

using (ClientContext context = new ClientContext("https://XXX.sharepoint.com"))
            {
                SecureString securepassWord = new SecureString();
                foreach (char c in password.ToCharArray())
                {
                    securepassWord.AppendChar(c);
                }
                context.Credentials = new SharePointOnlineCredentials(account, securepassWord);

                Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl("/Shared%20Documents/MASTER%20CUSTOMER%20LIST%20LICENSE%20APAC.xlsx");
                ListItem listItem = file.ListItemAllFields;
                ClientResult<System.IO.Stream> data = file.OpenBinaryStream();
                context.Load(file);
                context.Load(listItem);
                context.ExecuteQuery();

                string modified = listItem["Modified"].ToString();


                using (var p = new ExcelPackage())
                {

                    using (MemoryStream mStream = new MemoryStream())
                    {
                        if (data != null)
                        {
                            data.Value.CopyTo(mStream);
                            p.Load(mStream);

                            var ws = p.Workbook.Worksheets["APAC approved"];
                            if (ws != null)
                            {
                                int rowIndex = 3;
                                while (true)
                                {
                                    string Name = ws.Cells[rowIndex, 1].Value == null ? null : ws.Cells[rowIndex, 1].Value.ToString().Trim();
                                    string Country = ws.Cells[rowIndex, 2].Value == null ? null : ws.Cells[rowIndex, 2].Value.ToString().Trim();
                                    string DistorUser = ws.Cells[rowIndex, 3].Value == null ? null : ws.Cells[rowIndex, 3].Value.ToString().Trim();
                                    string DistributorName  = ws.Cells[rowIndex, 4].Value == null ? null : ws.Cells[rowIndex, 4].Value.ToString().Trim();
                                    string Approved = ws.Cells[rowIndex, 5].Value == null ? null : ws.Cells[rowIndex, 5].Value.ToString().Trim();
                                    if (string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(Country) && string.IsNullOrEmpty(DistorUser))
                                    {
                                        break;
                                    }

                                    rowIndex++;
                                }

                            }

                        }
                    }
                }
                

            }

Generate Data from template

FileInfo file = new FileInfo(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Template.xlsx");
                using (ExcelPackage Excel_Package = new ExcelPackage(file))
                {

                    ExcelWorksheet ws = Excel_Package.Workbook.Worksheets[1];
                    ws.Name = "C - " + DateTime.Now.ToString("yyyyMMddHHmmss");

                    int tmpRowIndex = 4;
                    List<Summary> Summary_List = ViewState["Summary_List"] as List<Summary>;
                    for (int i = 0; i < Summary_List .Count; i++)
                    {
                            foreach (Detail item in Summary_List)
                            {
                                //sheetC
                                rebateSKU = item.F_RESKU;
                                quality = -1;
                                actualPayment = decimal.Parse(item.F_RePay);
                                repriceCode = 63;
                                user = "FA";
                                salesCode = item.F_SCode;
                                if (salesCode.Length >= 2)
                                {
                                    salesCode = salesCode.Substring(2, salesCode.Length - 2);
                                }
                                ws.Cells[tmpRowIndex, 1].Value = rebateSKU;
                                ws.Cells[tmpRowIndex, 2].Value = quality;
                                ws.Cells[tmpRowIndex, 3].Value = actualPayment;
                                ws.Cells[tmpRowIndex, 5].Value = repriceCode;
                                ws.Cells[tmpRowIndex, 31].Value = user;
                                ws.Cells[tmpRowIndex,38].Value = salesCode;
                                ////sheetD                              

                                tmpRowIndex++;
                                quality = 1;
                                repriceCode = 21;
                                user = "FA";
                                ws.Cells[tmpRowIndex, 1].Value = rebateSKU;
                                ws.Cells[tmpRowIndex, 2].Value = quality;
                                ws.Cells[tmpRowIndex, 3].Value = actualPayment;
                                ws.Cells[tmpRowIndex, 5].Value = repriceCode;
                                ws.Cells[tmpRowIndex, 31].Value = user;
                                ws.Cells[tmpRowIndex, 38].Value = salesCode;
                                tmpRowIndex++;
                            }                        
                    }


                    var data = Excel_Package.GetAsByteArray();

                    HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;  filename=RebatePaymentExport" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");
                    HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString());
                    HttpContext.Current.Response.BinaryWrite(data);

                }

Download Data


using (ExcelPackage Excel_Package = new ExcelPackage())
  {
      using (ExcelWorksheet Excel_Sheet = Excel_Package.Workbook.Worksheets.Add("Sheet1"))
      {

         //add header
                        Excel_Sheet.Cells[1, 1].Value = "Sequence No";
                        Excel_Sheet.Cells[1, 2].Value = "FIN Comments";
                        Excel_Sheet.Cells[1, 3].Value = "Form Status"
         //add content
                        for (int i = 0; i < Detail_List.Count; i++)
                        {
                            Excel_Sheet.Cells[i + 2, 1].Value = Detail_List[i].SequenceNo;
                            Excel_Sheet.Cells[i + 2, 2].Value = Detail_List[i].F_FINCOMM;
                            Excel_Sheet.Cells[i + 2, 3].Value = Detail_List[i].FormStatus;
                        }

          var data = Excel_Package.GetAsByteArray();

                        HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;  filename=DownloadFile" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");
                        HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString());
                        HttpContext.Current.Response.BinaryWrite(data);
      }
 }

⚠️ **GitHub.com Fallback** ⚠️