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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: ASP.NET MVC |
Control: Uploadbox
Tags: uploadbox

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

 

2X faster development

The ultimate ASP.NET MVC UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon 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.Close Icon

Live Chat Icon For mobile