Articles in this section
Category / Section

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

1 min read

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

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied