Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 29, 2013 12:26 AM by Decker Dong - MSFT
Member
175 Points
577 Posts
Jan 24, 2013 01:17 PM|LINK
hi there, hope in your help.
i need Create Excel file in ASP.NET C# using OpenXML and this is my example link:
http://www.c-sharpcorner.com/uploadfile/jayendra/how-to-create-excel-file-in-asp-net-c-sharp/
i tried this code but i have error:
CS0246: The type or namespace name 'OpenXmlDataDataContext' could not be found (are you missing a using directive or an assembly reference?)
Can you help me?
Any other suggestion for my problem?
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OpenXml.aspx.cs" Inherits="OpenXml" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btn_Excel" runat="server" Text="Excel" onclick="btn_Excel_Click" /> </div> </form> </body> </html>
.cs
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using OfficeOpenXml; public partial class OpenXml : System.Web.UI.Page { DataTable Dt = new DataTable(); object[] query; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } private void GetRecoredForExcelfile() { using (OpenXmlDataDataContext db = new OpenXmlDataDataContext()) { var info = from p in db.userinfos select p; if (info != null) { query = info.ToArray(); Dt = ConvertToDatatable(query); } } } public static DataTable ConvertToDatatable(Object[] array) { PropertyInfo[] properties = array.GetType().GetElementType().GetProperties(); DataTable dt = CreateDataTable(properties); if (array.Length != 0) { foreach (object o in array) FillData(properties, dt, o); } return dt; } #region Private Methods private static DataTable CreateDataTable(PropertyInfo[] properties) { DataTable dt = new DataTable(); DataColumn dc = null; foreach (PropertyInfo pi in properties) { dc = new DataColumn(); dc.ColumnName = pi.Name; dt.Columns.Add(dc); } return dt; } private static void FillData(PropertyInfo[] properties, DataTable dt, Object o) { DataRow dr = dt.NewRow(); foreach (PropertyInfo pi in properties) { dr[pi.Name] = pi.GetValue(o, null); } dt.Rows.Add(dr); } #endregion protected void btn_Excel_Click(object sender, EventArgs e) { GetRecoredForExcelfile(); string newFilePath = Server.MapPath("ExcelFile/ErrorList.xlsx"); string templateFilePath = Server.MapPath("ExcelFile/ErrorListtemplate.xlsx"); FileInfo newFile = new FileInfo(newFilePath); FileInfo template = new FileInfo(templateFilePath); using (ExcelPackage xlPackage = new ExcelPackage(newFile, template)) { foreach (ExcelWorksheet aworksheet in xlPackage.Workbook.Worksheets) { aworksheet.Cell(1, 1).Value = aworksheet.Cell(1, 1).Value; } ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"]; int startrow = 5; int row = 0; int col = 0; for (int j = 0; j < Dt.Columns.Count; j++) { col++; for (int i = 0; i < Dt.Rows.Count; i++) { row = startrow + i; ExcelCell cell = worksheet.Cell(row, col); cell.Value = Dt.Rows[i][j].ToString(); xlPackage.Save(); } } } } }
All-Star
118619 Points
18779 Posts
Jan 26, 2013 01:57 AM|LINK
Hi,
You must refer this dll file in your References folder:
You must add
See the download demo:
OpenXmlExample.zip
Jan 26, 2013 06:05 AM|LINK
I followed all the instructions in the tutorial but I have the same error.
Jan 26, 2013 08:25 AM|LINK
cms9651 I followed all the instructions in the tutorial but I have the same error.
I suspect this isn't a real class in the project. I think it may be a generated class something like LINQ-TO-SQL.
Jan 26, 2013 11:29 AM|LINK
thank you, I understand... do you have another example link?
Jan 26, 2013 11:33 AM|LINK
cms9651 thank you, I understand... do you have another example link?
For "'OpenXmlDataDataContext' " I haven't.
Maybe you can do like the download sample to create an OpenXmlDataContext, that's only a variable name——as far as I see.
Jan 26, 2013 11:47 AM|LINK
hi, can you help me?
Jan 26, 2013 11:34 PM|LINK
cms9651 hi, can you help me?
This is just a LINQ-TO-SQL. So please create a dbml file and rename it as "OpenXmlDataContext".
Jan 27, 2013 09:51 AM|LINK
thank you.
I have created dbml file and rename it as "OpenXmlDataContext" and in code behind I changed OpenXmlDataDataContext to OpenXmlDataContext.
The error is persistent:
CS0246: The type or namespace name 'OpenXmlDataContext' could not be found (are you missing a using directive or an assembly reference?)
Jan 27, 2013 10:24 AM|LINK
cms9651 cms9651
Remove the OpenXmlDataContext out of the App_Code and have a try.
cms9651
Member
175 Points
577 Posts
[ C# .net 4] Create Excel file using OpenXML
Jan 24, 2013 01:17 PM|LINK
hi there, hope in your help.
i need Create Excel file in ASP.NET C# using OpenXML and this is my example link:
http://www.c-sharpcorner.com/uploadfile/jayendra/how-to-create-excel-file-in-asp-net-c-sharp/
i tried this code but i have error:
Can you help me?
Any other suggestion for my problem?
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OpenXml.aspx.cs" Inherits="OpenXml" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btn_Excel" runat="server" Text="Excel" onclick="btn_Excel_Click" /> </div> </form> </body> </html>.cs
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using OfficeOpenXml; public partial class OpenXml : System.Web.UI.Page { DataTable Dt = new DataTable(); object[] query; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } private void GetRecoredForExcelfile() { using (OpenXmlDataDataContext db = new OpenXmlDataDataContext()) { var info = from p in db.userinfos select p; if (info != null) { query = info.ToArray(); Dt = ConvertToDatatable(query); } } } public static DataTable ConvertToDatatable(Object[] array) { PropertyInfo[] properties = array.GetType().GetElementType().GetProperties(); DataTable dt = CreateDataTable(properties); if (array.Length != 0) { foreach (object o in array) FillData(properties, dt, o); } return dt; } #region Private Methods private static DataTable CreateDataTable(PropertyInfo[] properties) { DataTable dt = new DataTable(); DataColumn dc = null; foreach (PropertyInfo pi in properties) { dc = new DataColumn(); dc.ColumnName = pi.Name; dt.Columns.Add(dc); } return dt; } private static void FillData(PropertyInfo[] properties, DataTable dt, Object o) { DataRow dr = dt.NewRow(); foreach (PropertyInfo pi in properties) { dr[pi.Name] = pi.GetValue(o, null); } dt.Rows.Add(dr); } #endregion protected void btn_Excel_Click(object sender, EventArgs e) { GetRecoredForExcelfile(); string newFilePath = Server.MapPath("ExcelFile/ErrorList.xlsx"); string templateFilePath = Server.MapPath("ExcelFile/ErrorListtemplate.xlsx"); FileInfo newFile = new FileInfo(newFilePath); FileInfo template = new FileInfo(templateFilePath); using (ExcelPackage xlPackage = new ExcelPackage(newFile, template)) { foreach (ExcelWorksheet aworksheet in xlPackage.Workbook.Worksheets) { aworksheet.Cell(1, 1).Value = aworksheet.Cell(1, 1).Value; } ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"]; int startrow = 5; int row = 0; int col = 0; for (int j = 0; j < Dt.Columns.Count; j++) { col++; for (int i = 0; i < Dt.Rows.Count; i++) { row = startrow + i; ExcelCell cell = worksheet.Cell(row, col); cell.Value = Dt.Rows[i][j].ToString(); xlPackage.Save(); } } } } }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 26, 2013 01:57 AM|LINK
Hi,
You must refer this dll file in your References folder:
You must add
See the download demo:
OpenXmlExample.zip
cms9651
Member
175 Points
577 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 26, 2013 06:05 AM|LINK
I followed all the instructions in the tutorial but I have the same error.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 26, 2013 08:25 AM|LINK
Hi,
I suspect this isn't a real class in the project. I think it may be a generated class something like LINQ-TO-SQL.
cms9651
Member
175 Points
577 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 26, 2013 11:29 AM|LINK
thank you, I understand... do you have another example link?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 26, 2013 11:33 AM|LINK
Hi,
For "'OpenXmlDataDataContext' " I haven't.
Maybe you can do like the download sample to create an OpenXmlDataContext, that's only a variable name——as far as I see.
cms9651
Member
175 Points
577 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 26, 2013 11:47 AM|LINK
hi, can you help me?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 26, 2013 11:34 PM|LINK
This is just a LINQ-TO-SQL. So please create a dbml file and rename it as "OpenXmlDataContext".
cms9651
Member
175 Points
577 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 27, 2013 09:51 AM|LINK
thank you.
I have created dbml file and rename it as "OpenXmlDataContext" and in code behind I changed OpenXmlDataDataContext to OpenXmlDataContext.
The error is persistent:
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: [ C# .net 4] Create Excel file using OpenXML
Jan 27, 2013 10:24 AM|LINK
Hi,
Remove the OpenXmlDataContext out of the App_Code and have a try.