Skip to main content

Uploading an Excel and Importing the data in database

This is very common requirement to read the data from uploaded excel and import it in database. Earlier it was a tedious task but not now, thanks to Nuget packages where you get so many utilities to make your life easy. In this post, we will also use Nuget package and perform the task of uploading excel and importing the data in database.

There are two steps to perform this,
1. To upload the excel file on server
2. Read the uploaded file and import the data to database

Step1: To upload the excel file on server

   <asp:FileUpload ID="excel_upload" runat="server"/>


    
    if (excel_upload.HasFile)
    {
       bool upload_file = true;
       string file_upload = Path.GetExtension(excel_upload.FileName.ToString());
       if (file_upload.Trim().ToLower() == ".xls" | 
            file_upload.Trim().ToLower() == ".xlsx")
       {
          // Save excel file onto Server
          xlsUpload.SaveAs(Server.MapPath("~/Uploads/" + excel_upload.FileName.ToString());
       }
    }


Step2: Read the uploaded file and import the data to database

To perform excel read/write, we will use ClosedXML (Nuget package). Install the package using package manager.



Once installed, include the namespace in your code file   using ClosedXML.Excel;

Now the code starts to get data from uploaded excel file.

      FileInfo fi = new FileInfo(filePath);

      //Open uploaded workbook
            var workBook = new XLWorkbook(fi.FullName);
      //Get the first sheet of workbook
      var worksheet = workBook.Worksheet(1);

      var firstRowUsed = worksheet.FirstRowUsed();
      var categoryRow = firstRowUsed.RowUsed();

      int coCategoryId = 1;

      //Get the column names from first row of excel
      Dictionary<int, string> keyValues = new Dictionary<int, string>();
      for (int cell = 1; cell <= categoryRow.CellCount(); cell++)
      {
        keyValues.Add(cell, categoryRow.Cell(cell).GetString());
      }

      //Get the next row
      categoryRow = categoryRow.RowBelow();
      while (!categoryRow.Cell(coCategoryId).IsEmpty())
      {
         int count = 1;
         var pc = new ExpandoObject();
         while (count <= categoryRow.CellCount())
         {
          // let this go through-if the data is bad, it will be rejected by SQL
           var data = categoryRow.Cell(count).Value;

           ((IDictionary<string, object>)pc).Add(keyValues[count], data);           

           count++;
         }
         categoryRow = categoryRow.RowBelow();
      }
      //Insert the data in SQL which is captured in pc object.


Reference: To get more detailed scenarios for excel data manipulation, click here.


~~ Happy Coding ~~








Comments

Popular posts from this blog

Nutch crawler and integration with Solr

Before moving ahead with this article, I assume you have Solr installed and running. If you would like to install Solr on windows, mac or via docker, please read Setup a Solr instance . There are several ways to install nutch which you can read from Nutch tutorial , however I have written this article for those who would like to install nutch using docker. I tried finding help on google but could not find any help for nutch installation using docker and spent good amount of time fixing issues specific to it. Therefore I have written this article to help and save time of other developers. Install nutch using docker- 1. Pull docker image of nutch using below command,      > docker pull apache/nutch 2. Once image is pulled, run the container,      > docker run -t -i -d --name nutchcontainer apache/nutch /bin/bash 3. You should be able to enter in the container and see bash prompt,      > bash-5.1#  Let's setup few important setting...

AJAX Progrraming

Ajax , shorthand for Asynchronous JavaScript and XML , is a web development technique for creating interactive web applications. The intent is to make web pages feel more responsive by exchanging small amounts of data with the server behind the scenes, so that the entire web page does not have to be reloaded each time the user requests a change. This is meant to increase the web page's interactivity, speed, and usability. The Ajax technique uses a combination of: XHTML (or HTML) and CSS, for marking up and styling information. The DOM accessed with a client-side scripting language, especially JavaScript and JScript, to dynamically display and interact with the information presented. The XMLHttpRequest object is used to exchange data asynchronously with the web server. In some Ajax frameworks and in certain situations, an IFrame object is used instead of the XMLHttpRequest object to exchange data with the web server, and in other implementations, dynamically added tags may be used. ...

Call a custom Script from Excel

  It is quite common that we receive the data in Excel and several times we have to remove unwanted characters from columns or modify the data as per the requirement. To avoid manual fixes of these kind of requirements, we may use Macro to automate the process which not only increase productivity but also avoid manual errors. We can easily create Macro to quickly fix it. Here are the steps to create Macro and run it. Open Excel Goto View tab on Main menu Click on 'View Macros'. This should open a Macro popup. Enter Macro Name and click on + icon. Clicking on + icon should open Macro Coding screen(as shown below). Add your code in the function. Click on Save icon to save the file with Macro. Click on Run icon to run the Macro code. The above code should remove additional line breaks from columns. Reference Code(to remove line breaks from columns): Sub MyCustomMacro () Dim MyRange As Range Application . ScreenUpdating = False Application . Calculation = xlCa...