

- #Adventureworks2012 database download getting error how to
- #Adventureworks2012 database download getting error install
- #Adventureworks2012 database download getting error code
OSheet.Name = "ExportToExcelUsingInterOp" Start Excel and get Application object. Var filepath = constr = Source=DESKTOP-1PM1CJ9\SQLEXPRESS2 Initial Catalog=AdventureWorks2012 Integrated Security=true" Protected void ExportToExcelUsingInterop(object sender, EventArgs e)
#Adventureworks2012 database download getting error code
Once you have added the reference in your project, you can use the below C# code which will generate excel file on button click using System
#Adventureworks2012 database download getting error how to
Export datatable to excel in C# using InteropĪnother way of exporting datatable to excel in C# is using, let's take a look how to do it usinfg InterOp.įirst, you need to add reference of .dll in your project, as shown in the below image

MyMemoryStream.WriteTo(Response.OutputStream) Using MyMemoryStream As MemoryStream = New MemoryStream() Response.AddHeader("content-disposition", "attachment filename=DataTableToExcelExport.xlsx") Using wb As XLWorkbook = New XLWorkbook() Using sda As SqlDataAdapter = New SqlDataAdapter() Using cmd As SqlCommand = New SqlCommand("SELECT ProductID,Name, ProductNumber FROM.") Using con As SqlConnection = New SqlConnection(constr) Protected Sub ExportToExcel(ByVal sender As Object, ByVal e As EventArgs)ĭim constr As String = "Data Source=DESKTOP-4K41ID9\SQLEXPRESS Initial Catalog=AdventureWorks2012 Integrated Security=true" Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) VB.NET code for the above code, converted using online tools Imports ClosedXML.Excel Note: you cannot select GUID based column to export using ClosedXML/OpenXML as they don't support it, you may get error like "Unable to set cell value to 694215b7-08f7-4c0d-acb1-d734ba44c0c8 (GUID)"Īfter opening the browser Click "Export" button, you will see output as below Using (MemoryStream MyMemoryStream = new MemoryStream()) Using (SqlDataAdapter sda = new SqlDataAdapter()) Using (SqlCommand cmd = new SqlCommand("SELECT ProductID,Name, ProductNumber FROM.")) Using (SqlConnection con = new SqlConnection(constr)) String constr = Source=DESKTOP-4K41ID9\SQLEXPRESS Initial Catalog=AdventureWorks2012 Integrated Security=true" Protected void ExportToExcel(object sender, EventArgs e) Protected void Page_Load(object sender, EventArgs e) cs file will be as below using ClosedXML.Excel We will be using create a Button on the HTML or Front end Side and will be calling the C# code on button Click, so in the Default.aspx your code can be as below Īs you can see we are calling "ExportToExcel" on button Click, so C# code in the. Now, we have both the required files in our project, let's create a "Default.aspx" file.
#Adventureworks2012 database download getting error install
Once OpenXML installed using Nuget package manager console, you need to install ClosedXML now using Nuget, so run the below command Install-Package ClosedXML Now, as we will be using OpenXML opn source library, let's install it using Nuget library, so navigate to Tools->Nuget Package manager -.> Nuget packge manager console and use the below command to install it Now, let's create a new ASP.NET web-project in your Visual Studio, navigate to File-> New -> Project -> Select "Web" in the left-pne and "ASP.NET web-application" in right-pane, name it and click OK, then select "Empty" template -> Click OK (then your project must be generated with empty template.) Let's get started with this, before we proceed here is the image of the database, which we will be using in this example.īasically we are using AdventureWorks2012 database and it's table . You can also take a look on our previous related article like reading excel file in C# and how to create excel file in C# without using Office or Interop. In this article, I am going to explain you how you can can export your ASP.NET web-form datatable into excel file using C#, in which we will first fetch data from database and have used Open Source library OpenXML and CloseXML to get data from datatable into Excel file in C# / VB.
