Entity framework with excel

Let’s start uploading excel data to Microsoft SQL Server Database.

Prerequisite :

  • Microsoft SQL Server 2014
  • Visual studio 2013

Figure 1: Create table as in the following,

 

Figure 2 : Add new item ADO.NET Entity Data Model and click add button,

Figure 3: Choose Entity framework and click next button,

 

Figure 4 : Include database object from our SQL database please and select our target table Users and click finish,

 

Figure 5: Install NuGet package LinqToExcel in our project,

 

Download Excel file format and enter your own data to this format for uploading,

In this view using FormMethod.Post «UploadExcel»  function name Controller name «User«,

@using (Html.BeginForm("UploadExcel", "User", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "return myFunction()" }))  

Download Excel file format href link,

<a href="/User/DownloadExcel/"><img src="~/excel.ico" width="25" height="25" title="Download Excel format" alt="excel" />

View

@{  
    ViewBag.Title = "Index";  
}  
  
<h4>Add Users via Excel</h4>  
<hr />  
@using (Html.BeginForm("UploadExcel", "User", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "return myFunction()" }))  
{  
    <div class="form-horizontal">  
        <div class="form-group">  
            <div class="control-label col-md-2">Download Format:</div>  
            <div class="col-md-10">  
                <a href="/User/DownloadExcel/"><img src="~/excel.ico" width="25" height="25" title="Download Excel format" alt="excel" /></a>  
            </div>  
        </div>
        <div class="form-group">
            <div class="control-label col-md-2">Excel:</div>  
            <div class="col-md-10">  
                <input type="file" id="FileUpload" name="FileUpload" class="" />  
            </div>  
        </div>  
        <div class="form-group">  
            <div class="col-md-offset-2 col-md-10">  
                <input type="submit" value="Upload" id="btnSubmit" class="btn btn-primary" />
            </div>  
        </div>  
    </div>  
}

Model

Userlist .cs 

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
  
namespace ExcelImport.Models  
{  
    public class UserList  
    {         
        public string Name { get; set; }  
        public string Address{ get; set; }  
        public string ContactNo { get; set; }  
  
  
    }  
}

Download Excel file format and enter your own data to this format for uploading. In the doc folder here’s format of sheet,

  

public FileResult DownloadExcel()    
{    
    string path = "/Doc/Users.xlsx";    
    return File(path, "application/vnd.ms-excel", "Users.xlsx");    
}
//deleting excel file from folder    
if ((System.IO.File.Exists(pathToExcelFile)))    
{    
    System.IO.File.Delete(pathToExcelFile);    
}    
return Json("success", JsonRequestBehavior.AllowGet); 

Controller Full Code: The JsonResult UploadExcel function using HttpPost return Json result,

using System;  
using System.Collections.Generic;  
using System.Data;  
using System.Data.Entity;  
using System.Data.Entity.Validation;  
using System.Data.OleDb;  
using System.IO;  
using System.Linq;  
using System.Net;  
using System.Text.RegularExpressions;  
using System.Web;  
using System.Web.Mvc;  
using ExcelImport.Models;  
using LinqToExcel;  
using System.Data.SqlClient;  
  
namespace ExcelImport.Controllers  
{  
    public class UserController : Controller  
    {  
        private test2Entities db = new test2Entities();  
        // GET: User  
        public ActionResult Index()  
        {  
            return View();  
        }
        /// <summary>  
        /// This function is used to download excel format.  
        /// </summary>  
        /// <param name="Path"></param>  
        /// <returns>file</returns>  
        public FileResult DownloadExcel()  
        {  
            string path = "/Doc/Users.xlsx";  
            return File(path, "application/vnd.ms-excel", "Users.xlsx");  
        }  
  
        [HttpPost]  
        public JsonResult UploadExcel(User users, HttpPostedFileBase FileUpload)  
        {  
  
            List<string> data = new List<string>();  
            if (FileUpload != null)  
            {  
                // tdata.ExecuteCommand("truncate table OtherCompanyAssets");  
                if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")  
                {                    
                    string filename = FileUpload.FileName;  
                    string targetpath = Server.MapPath("~/Doc/");  
                    FileUpload.SaveAs(targetpath + filename);  
                    string pathToExcelFile = targetpath + filename;  
                    var connectionString = "";  
                    if (filename.EndsWith(".xls"))  
                    {  
                        connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);  
                    }  
                    else if (filename.EndsWith(".xlsx"))  
                    {  
                        connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";", pathToExcelFile);  
                    }  
  
                    var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);  
                    var ds = new DataSet();
                    adapter.Fill(ds, "ExcelTable");
                    DataTable dtable = ds.Tables["ExcelTable"];
                    string sheetName = "Sheet1";
                    var excelFile = new ExcelQueryFactory(pathToExcelFile);  
                    var artistAlbums = from a in excelFile.Worksheet<User>(sheetName) select a;
                    foreach (var a in artistAlbums)  
                    {  
                        try  
                        {  
                            if (a.Name != "" && a.Address != "" && a.ContactNo != "")  
                            {  
                                User TU = new User();  
                                TU.Name = a.Name;  
                                TU.Address = a.Address;  
                                TU.ContactNo = a.ContactNo;  
                                db.Users.Add(TU);
                                db.SaveChanges();
                            }  
                            else  
                            {  
                                data.Add("<ul>");  
                                if (a.Name == "" || a.Name == null) data.Add("<li> name is required</li>");  
                                if (a.Address == "" || a.Address == null) data.Add("<li> Address is required</li>");  
                                if (a.ContactNo == "" || a.ContactNo == null) data.Add("<li>ContactNo is required</li>");
                                data.Add("</ul>");  
                                data.ToArray();  
                                return Json(data, JsonRequestBehavior.AllowGet);  
                            }  
                        }
                        catch (DbEntityValidationException ex)  
                        {  
                            foreach (var entityValidationErrors in ex.EntityValidationErrors)  
                            {
                                foreach (var validationError in entityValidationErrors.ValidationErrors)  
                                {
                                    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                                }
                            }  
                        }  
                    }  
                    //deleting excel file from folder  
                    if ((System.IO.File.Exists(pathToExcelFile)))  
                    {  
                        System.IO.File.Delete(pathToExcelFile);  
                    }  
                    return Json("success", JsonRequestBehavior.AllowGet);  
                }  
                else  
                {  
                    //alert message for invalid file format  
                    data.Add("<ul>");  
                    data.Add("<li>Only Excel file format is allowed</li>");  
                    data.Add("</ul>");  
                    data.ToArray();  
                    return Json(data, JsonRequestBehavior.AllowGet);  
                }  
            }  
            else  
            {  
                data.Add("<ul>");  
                if (FileUpload == null) data.Add("<li>Please choose Excel file</li>");  
                data.Add("</ul>");  
                data.ToArray();  
                return Json(data, JsonRequestBehavior.AllowGet);  
            }  
        }  
    }  
}

Output

 

Summary

We learned how to import excel data to Database using ASP.NET MVC Entity framework. I hope this article is useful for all .NET beginners.

Read more articles on ASP.NET:

  • MVC — Show Record Using Cascading Dropdown List In MVC Using jQuery And JSON
  • Creating An ASP.NET MVC 5 Site Using Bootstrap Custom Templates

In this article I will explain with an example, how to bulk import (insert) Excel file data into Database using Entity Framework in ASP.Net MVCRazor.

The uploaded Excel file data will be read using OLEDB library and the read data will be inserted into SQL Server database using Entity Framework.

Database

I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.

Entity Framework Excel: Bulk Import (Insert) data from Excel to Database using Entity Framework in ASP.Net MVC

Note: You can download the database table SQL by clicking the download link below.

Connection Strings

The Excel files of version 97-2003 and 2007 and above make use different OLEDB providers and hence two different connection strings have been saved in the Web.Config file.

The DataSource property has been assigned a Placeholder {0}, which will be replaced by actual path of the File.

<connectionStrings>

    <add name =«Excel03ConString» connectionString=«Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=’Excel 8.0;HDR=YES’«/>

    <add name =«Excel07ConString« connectionString=«Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=’Excel 8.0;HDR=YES’«/>

</connectionStrings>

Namespaces

You will need to import the following namespaces.

using System.IO;

using System.Data;

using System.Data.OleDb;

using System.Configuration;

Controller

The Controller consists of two Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.

Action method for handling POST operation for uploading and reading Excel file

This Action method gets called when the Excel File is selected and the Import Button is clicked, and it gets the uploaded file in the HttpPostedFileBase parameter.

The uploaded Excel file is saved to a folder named Uploads and then based on its extension whether XLS (97-2003) or XLSX (2007 and above), appropriate connection string is read from the Web.Config file and Placeholder is replaced by the path of the Excel file.

Note: I am considering all Excel files with the first row as the Header Row containing the names of the columns, you can set HDR=’No’ if your excel file does not have a Header Row.

Using the fetched Sheet name, a SELECT statement is executed and all the records from the Excel sheet are fetched into a DataTable.

Now a loop is executed over the DataTable rows and one by one each record is saved into the database Table using Entity Framework.

public class HomeController : Controller

{

    // GET: Home

    public ActionResult Index()

    {

        return View();

    }

    [HttpPost]

    public ActionResult Index(HttpPostedFileBase postedFile)

    {

        string filePath = string.Empty;

        if (postedFile != null)

        {

            string path = Server.MapPath(«~/Uploads/»);

            if (!Directory.Exists(path))

            {

                Directory.CreateDirectory(path);

            }

            filePath = path + Path.GetFileName(postedFile.FileName);

            string extension = Path.GetExtension(postedFile.FileName);

            postedFile.SaveAs(filePath);

            string conString = string.Empty;

            switch (extension)

            {

                case «.xls»: //Excel 97-03.

                    conString = ConfigurationManager.ConnectionStrings[«Excel03ConString»].ConnectionString;

                    break;

                case «.xlsx»: //Excel 07 and above.

                    conString = ConfigurationManager.ConnectionStrings[«Excel07ConString»].ConnectionString;

                    break;

            }

            DataTable dt = new DataTable();

            conString = string.Format(conString, filePath);

            using (OleDbConnection connExcel = new OleDbConnection(conString))

            {

                using (OleDbCommand cmdExcel = new OleDbCommand())

                {

                    using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())

                    {

                        cmdExcel.Connection = connExcel;

                        //Get the name of First Sheet.

                        connExcel.Open();

                        DataTable dtExcelSchema;

                        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                        string sheetName = dtExcelSchema.Rows[0][«TABLE_NAME»].ToString();

                        connExcel.Close();

                        //Read Data from First Sheet.

                        connExcel.Open();

                        cmdExcel.CommandText = «SELECT * From [« + sheetName + «]»;

                        odaExcel.SelectCommand = cmdExcel;

                        odaExcel.Fill(dt);

                        connExcel.Close();

                    }

                }

            }

            //Insert records to database table.

            CustomersEntities entities = new CustomersEntities();

            foreach (DataRow row in dt.Rows)

            {

                entities.Customers.Add(new Customer

                {

                    Name = row[«Name»].ToString(),

                    Country = row[«Country»].ToString()

                });

            }

            entities.SaveChanges();

        }

        return View();

    }

}

View

The View consists of an HTML FileUpload element and a Submit Button enclosed in a Form element.

The HTML Form has been created using the Html.BeginForm method which accepts the following parameters.

ActionName – Name of the Action. In this case the name is Index.

ControllerName – Name of the Controller. In this case the name is Home.

FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.

HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.

@{

    Layout = null;

}

<!DOCTYPE html>

<html>

<head>

    <meta name=»viewport» content=»width=device-width»/>

    <title>Index</title>

</head>

<body>

    @using (Html.BeginForm(«Index», «Home», FormMethod.Post, new { enctype = «multipart/form-data» }))

    {

        <input type=»file» name=»postedFile»/>

        <input type=»submit» value=»Import»/>

    }

</body>

</html>

Screenshots

The Excel File

Entity Framework Excel: Bulk Import (Insert) data from Excel to Database using Entity Framework in ASP.Net MVC

Table containing the data from the Excel file

Entity Framework Excel: Bulk Import (Insert) data from Excel to Database using Entity Framework in ASP.Net MVC

Downloads

We have some info that is contained within an excel sheet. the excel sheet contains two columns; customer name + customer balance.
Now I need to display this info inside my asp.net mvc view, basically to query the excel sheet based on the customer name and display its total balance inside my view.

In this way we will be uploading the excel sheet inside our system each month or each day , and we will be querying the excel sheet instead of the DB. So is this possible using EF ? or it is better to exact the data from the excel sheet and add it to Datable inside the database?
Thanks

asked Jan 5, 2015 at 11:27

John John's user avatar

1

You can use LinqToExcel , this provides normal linq structure to query excel sheets

answered Jan 5, 2015 at 11:34

Aram Tchekrekjian's user avatar

Excel is not a database system so don’t think directly you can use the entity framework. But yes Excel can be read using .net APIs

answered Jan 5, 2015 at 11:47

Mahesh Malpani's user avatar

It is better to extract the data from the excel sheet and add it to Datable which you can do using oledbconnection object , connectionobject.GetOleDbSchemaTable.

answered Aug 25, 2015 at 7:25

Vamshi Chilukuri's user avatar

FileContextCore seems like an effort to make excel and other file types work in EF Core. Obviously this can be nice for consistency if you’re already using EF Core.

answered Oct 11, 2018 at 12:01

Graham's user avatar

GrahamGraham

1,48916 silver badges22 bronze badges

1

I was working on a project recently that used Entity Framework and had to span multiple databases. I was quite surprised to find out that EF didn’t support making one edmx file span multiple databases, and so came up with this solution to get around that using SQL Synonyms.

Here’s a quick walkthrough of how it works. Lets say I have two databases, located on separate servers. We’ll call them ServerA.DatabaseA and ServerB.DatabaseB. I would like to have one edmx file that contains objects from both databases, and include links between the two.

Setting up the Synonyms

First, I needed to setup my database synonyms. A simple script like this was run on DatabaseB to print out my CREATE SYNONYM statements, and the resulting output got verified and ran on DatabaseA.

declare @name varchar(max)

DECLARE db_cursor CURSOR FOR
select name from sysobjects where type = 'U'

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS = 0
BEGIN   

print 'CREATE SYNONYM [dbo].[' + @name + '] FOR [ServerB].[DatabaseB].[dbo].[' + @name + ']
 '
       FETCH NEXT FROM db_cursor INTO @name
END   

CLOSE db_cursor
DEALLOCATE db_cursor

This allowed me to reference tables on ServerB.DatabaseB from ServerA.DatabaseA using their regular table names. For example, if DatabaseA had Customers and DatabaseB had products, this allows me to run

SELECT * FROM Products

from DatabaseA instead of writing

SELECT * FROM ServerB.DatabaseB.dbo.Products

It will also work on Views, Stored Procedures, and User-Defined Functions. The only catch is no two object names can be the same.

Creating the EDMX files

Next, we need to create our edmx files. You will end up with one working copy of the edmx file, and a separate copy for each database you are trying to span.

Create a project for each database containing nothing more than a single edmx file pointing to one of the databases. Name the edmx files the same and make sure the entity namespaces are the same.

Now create a 3rd project and edmx file for your working copy. Make sure it’s also named the same and has the same entity namespace. Point it to whatever database contains the synonyms. In this case, that’s DatabaseA.

Merging the edmx files

Now we need to merge the edmx files. Since they are just xml files, this could be done by hand, but if we did that we’d need to do it every time the database got updated, so a script is far better.

Here is the one I use. Its not perfect, but for an XML dummy like me it worked just fine. I created a new project to hold it, and whenever I made changes to my edmx files I would run this project.

static void Main(string[] args)
{
    // Directory that can access all 3 edmx files
    // Fairly sure there's a better way to do this, but this
    // was the first that came to my mind
    string rootDir = Directory.GetCurrentDirectory() + @"........";

    // Location of working edmx file to write merged edmx to
    string resultFile = rootDir + @"DALEntities.edmx";

    // List of edmx files to merge
    string[] files = new string[]
    {
        rootDir + @"DatabaseAEntities.edmx",
        rootDir + @"DatabaseBEntities.edmx"
    };

    // Load result file
    var a = new XmlDocument();
    a.Load(resultFile);

    // TODO: Clear result file of anything except LNK_ items?
    // Actually that might cause a problem with nav properties.
    // Will probably have to create a merged file of list of files,
    // then merge changes from that single file with the result file

    // Loop through files to merge and add their nodes to the result file
    foreach (var edmxFile in files)
    {
        var b = new XmlDocument();
        b.Load(edmxFile);

        string rootNode = "/edmx:Edmx/edmx:Runtime/";

        XmlNamespaceManager nsMan = new XmlNamespaceManager(a.NameTable);
        nsMan.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2008/10/edmx");

        // SSDL
        MergeNodes(
            a.SelectSingleNode(rootNode + "edmx:StorageModels", nsMan)["Schema"]["EntityContainer"],
            b.SelectSingleNode(rootNode + "edmx:StorageModels", nsMan)["Schema"]["EntityContainer"].ChildNodes,
            a, b);

        MergeNodes(
            a.SelectSingleNode(rootNode + "edmx:StorageModels", nsMan)["Schema"],
            b.SelectSingleNode(rootNode + "edmx:StorageModels", nsMan)["Schema"].ChildNodes,
            a, b);

        // CSDL
        MergeNodes(
            a.SelectSingleNode(rootNode + "edmx:ConceptualModels", nsMan)["Schema"]["EntityContainer"],
            b.SelectSingleNode(rootNode + "edmx:ConceptualModels", nsMan)["Schema"]["EntityContainer"].ChildNodes,
            a, b);

        MergeNodes(
            a.SelectSingleNode(rootNode + "edmx:ConceptualModels", nsMan)["Schema"],
            b.SelectSingleNode(rootNode + "edmx:ConceptualModels", nsMan)["Schema"].ChildNodes,
            a, b);

        // MSL
        MergeNodes(
            a.SelectSingleNode(rootNode + "edmx:Mappings", nsMan)["Mapping"]["EntityContainerMapping"],
            b.SelectSingleNode(rootNode + "edmx:Mappings", nsMan)["Mapping"]["EntityContainerMapping"].ChildNodes,
            a, b);

    }

    // Save result file
    a.Save(resultFile);

    Console.WriteLine("Done");
    Console.ReadLine();
}

private static void MergeNodes(XmlNode parentNodeA, XmlNodeList childNodesB, XmlDocument parentA, XmlDocument parentB)
{
    foreach (XmlNode oNode in childNodesB)
    {
        // Exclude container node and comments
        if (oNode.Name == "EntityContainer" || oNode.Name == "#comment") continue;

        bool isFound = false;
        string name = oNode.Attributes["Name"].Value;

        foreach (XmlNode child in parentNodeA.ChildNodes)
        {
            if (child.Name == "EntityContainer" || child.Name == "#comment") continue;

            // If node already exists, exit loop
            if (child.OuterXml == oNode.OuterXml && child.InnerXml == oNode.InnerXml)
            {
                isFound = true;
                Console.WriteLine("Found::NoChanges::" + oNode.Name + "::" + name);
                break;
            }

            // If node by the same name exists
            if (child.Attributes["Name"].Value == name)
            {
                if (oNode.Name == "EntityType")
                {
                    foreach (XmlNode property in child.ChildNodes)
                    {
                        // Make sure to keep any navigation properties that have been added
                        if (property.Name == "NavigationProperty"
                            && property.Attributes["Relationship"] != null
                            && Regex.IsMatch(property.Attributes["Relationship"].Value, @".*.LNK_.*"))
                        {
                            oNode.AppendChild(parentB.ImportNode(property, true));
                        }
                    }
                }

                isFound = true;
                Console.WriteLine("Found::Replaced::" + oNode.Name + "::" + name);
                parentNodeA.ReplaceChild(parentA.ImportNode(oNode, true), child);
            }
        }        if (!isFound)
        {
            Console.WriteLine("NotFound::Adding::" + oNode.Name + "::" + name);
            parentNodeA.AppendChild(parentA.ImportNode(oNode, true));
        }
    }}

This is a work in progress, but it works for now. I’ll update this post after I fix up the merge project.

Creating Links between the Databases

Now you’ll have one edmx file, containing the information from both databases. You can create Associations between the two objects, however they will be erased anytime you merge your changes. To get around this, make sure all your links contain the same prefix and modify the merge code to exclude NavigationProperties that are prefixed with whatever you use. In my case, I prefixed them with LNK_

Summary

And there you have it. Test it out and you’ll find it works just fine. Your working edmx points to DatabaseA, which contains synonyms for DatabaseB objects. EF has no idea that half its objects exist on another database, because from its point of view it is all one database. Navigational properties between the two databases work just fine providing you’ve manually setup the links in the working copy.

To update the working copy, update the projects containing the individual database edmx, and run the merge code again. Providing you don’t make any changes to the working copy except links, and all your links contain the same prefix to avoid getting erased when the merge occurs, you should have no problems.

Screenshot of MergeEdmx Projects

Notes

  • This only works in a databases that supports Synonyms. I was using SQL Server 2005.
  • This was the first project I did using EF and I was using EF 4. I have no idea if this works on older versions.
  • You have to be sure that items are uniquely named between databases.
  • The Merge code is not perfect – sorry. I plan on fixing it up to remove Deleted items but haven’t gotten around to it. I just have a TODO bit in my code for now. If someone knows of a better tool to merge XML files, let me know! It has to be something an xml-idiot can use though 🙂

Introduction

It brings all/selected data (specified by LINQ query) of entity model into specific excel sheet.

Background 

First of all you have to create the Entity Framework Model of the table on which you want to do the LINQ operation.

Using the code

It is a function, takes four arguments: Excel file path name, Excel sheet name, Result against LINQ query as IQueryable and Entity Framework Model object.

using System;
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Data.Objects;
using System.Data.EntityClient;
using System.Linq;

public void EntityToExcelSheet(string excelFilePath, 
       string sheetName, IQueryable result, ObjectContext ctx)
{
    Excel.Application oXL;
    Excel.Workbook oWB;
    Excel.Worksheet oSheet;
    Excel.Range oRange;
    try
    {
        
        oXL = new Excel.Application();

        
        oXL.Visible = true;
        oXL.DisplayAlerts = false;

        
        oWB = oXL.Workbooks.Add(Missing.Value);

        
        oSheet = (Excel.Worksheet)oWB.ActiveSheet;
        oSheet.Name = sheetName;

        
        
        DataTable dt = EntityToDataTable(result, ctx);

        int rowCount = 1;
        foreach (DataRow dr in dt.Rows)
        {
            rowCount += 1;
            for (int i = 1; i < dt.Columns.Count + 1; i++)
            {
                
                if (rowCount == 2)
                    oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
            }
        }

        
        oRange = oSheet.Range[oSheet.Cells[1, 1], oSheet.Cells[rowCount, dt.Columns.Count]];
        oRange.Columns.AutoFit();

        
        oSheet = null;
        oRange = null;
        oWB.SaveAs(excelFilePath, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, 
          Missing.Value, Missing.Value, Missing.Value, 
          Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, 
          Missing.Value, Missing.Value, Missing.Value);
        oWB.Close(Missing.Value, Missing.Value, Missing.Value);
        oWB = null;
        oXL.Quit();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

public DataTable EntityToDataTable(IQueryable result, ObjectContext ctx)
{
    try
    {
        EntityConnection conn = ctx.Connection as EntityConnection;
        using (SqlConnection SQLCon = new SqlConnection(conn.StoreConnection.ConnectionString))
        {
            ObjectQuery query = result as ObjectQuery;
            using (SqlCommand Cmd = new SqlCommand(query.ToTraceString(), SQLCon))
            {
                foreach (var param in query.Parameters)
                {
                    Cmd.Parameters.AddWithValue(param.Name, param.Value);
                }
                using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

How to call the method:

using (DebopamDBEntities db = new DebopamDBEntities())
{
    var query = db.Employees.Select(i => i).AsQueryable();
    try
    {
        EntityToExcelSheet("E:\Employees.xls", "Employees", query, db);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message, 
          "Error Creating Excel File", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

  • Remove From My Forums
  • Question

  • Hi,

    Can you please tell me how to insert values from Excel sheet into the entity framework database.? I am using ExcelDataReader library for it. http://exceldatareader.codeplex.com/

    I can read from excel into the web browser, but am unable to insert values from excel into the entity framework database using C#

    Any help would be greatly appreciated

Answers

  • I’m actually using ExcelDataReader in my EF application.  Rather than read into a DataSet, I stream them using the DataReader itself into POCO entities I wrote by hand.  I then perform validations and translate them into EF entities before sending
    them over the wire into a WCF service for commit to the database.

    If your Excel file already conforms to your data model and you can read the data directly into an EF entity, this is another option. 

    public IEnumerable<MyEntity> ReadEntitiesFromFile( IExcelDataReader reader, string filePath )
    {
       var myEntities = new List<MyEntity>();
       var stream = File.Open( filePath, FileMode.Open, FileAccess.Read );
       
       using ( var reader = ExcelReaderFactory.CreateOpenXmlReader( stream ) )
       {
         while ( reader.Read() )
         {
            var myEntity = new MyEntity():
            myEntity.MyProperty1 = reader.GetString(1);
            myEntity.MyProperty2 = reader.GetInt32(2);
            
            myEntites.Add(myEntity);
          }
       }
    
       return myEntities;
    }
      
    • Proposed as answer by

      Tuesday, July 6, 2010 11:02 AM

    • Marked as answer by
      Michael Sun [MSFT]Microsoft employee
      Friday, July 9, 2010 7:42 AM

Понравилась статья? Поделить с друзьями:
  • Entirerow hidden vba excel
  • Eplan спецификация изделий в excel
  • Entirerow excel это что
  • Epica martyr of the free word
  • Entire row vba excel