We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to upload an Excel file and extract emails from it?

Description:

   

      You can upload an excel file in UploadBox control and extract the specific column from that excel file.

 

Solution:

 

       You can upload an excel file in upload box control by specifying excel file extension is allowed to upload as illustrated in the following code example.

 

[CSHTML]

 

 

@Html.EJ().Uploadbox("UploadDefault").SaveUrl(Url.Content("~/FileUpload/SaveDefault")).ExtensionsAllow(".xls,.xlsx").RemoveUrl(Url.Content("~/FileUpload/RemoveDefault")).AutoUpload(true).

 

 

While uploading an excel file, you need to read the data from that excel, extract the specific column and add it to list as shown below code.

 

[CS]

 

 

private List<MailData> ConvertData(string file)

         {

             List<MailData> list = new List<MailData>();

             ExcelEngine excelEngine = new ExcelEngine();

             IWorkbook workbook = excelEngine.Excel.Workbooks.Open(file);

             IWorksheet worksheet = workbook.Worksheets[0];

             for (int column = 1; column <= worksheet.UsedRange.LastColumn; column++)

                 if (worksheet.Range[1, column].Value.ToLower() == "mailid")

                     for (int row = 2; row <= worksheet.UsedRange.LastRow; row++)

                         list.Add(new MailData() { MailID = worksheet.Range[row, column].Value.ToString() });

             workbook.Close();

             excelEngine.Dispose();

             return list;

         }

 

 

Once the uploading process is completed, “complete” client side event will be triggered in our uploadbox control. In that event, you can display the extracted columns which are read from the excel file in argument of complete event.

 

[CSHTML]

 

 

@Html.EJ().Uploadbox("UploadDefault").SaveUrl(Url.Content("~/FileUpload/SaveDefault")).ExtensionsAllow(".xls,.xlsx").RemoveUrl(Url.Content("~/FileUpload/RemoveDefault")).AutoUpload(true).ClientSideEvents(e=>e.Error("onError").Complete("onSuccess")).ClientSideEvents(e=>e.Complete("onSuccess"))

 

 

[JavaScript]

 

 

  function onSuccess(args) {

        if (args.responseText != "")

        {

            $("#maildata").ejListBox({ dataSource: JSON.parse(args.responseText), fields: { text: "MailID" }, height: "200" });

            $("#details").html("");

            $("#header").html("E-Mail ID Lis:");

        }

    }

 

 

You can download the sample from the following location:

Sample: Uploadbox Sample
  
Excel file: SampleData Excel File

 

Article ID: Published Date: Last Revised Date: Platform: Control:
7858 07/20/2017 07/20/2017 ASP.NET MVC Uploadbox
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.