C# datagridview到excel
iam正在添加对microsoft excel库12.0的引用 从COM组件向项目中添加对“Microsoft Office 12.0对象库”的引用 但是我无法创建这个对象。。。??? 我在页面中包含了名称空间,但我仍然无法理解intellisense中的错误C# datagridview到excel,c#,C#,iam正在添加对microsoft excel库12.0的引用 从COM组件向项目中添加对“Microsoft Office 12.0对象库”的引用 但是我无法创建这个对象。。。??? 我在页面中包含了名称空间,但我仍然无法理解intellisense中的错误 Excel.ApplicationClass ExcelApp=新建Excel.ApplicationClass() 您可能会遇到的错误是 'Microsoft.Office.Interop.Excel.ApplicationClass'
Excel.ApplicationClass ExcelApp=新建Excel.ApplicationClass() 您可能会遇到的错误是 'Microsoft.Office.Interop.Excel.ApplicationClass' 无法嵌入。使用适用的 接口 您需要做的是使用应用程序类。换句话说,只需将代码更改为
Excel.Application ExcelApp = new Excel.Application();
更新:以上假设您已将Excel设置为相应命名空间的别名。这可以通过在源代码顶部添加以下内容来实现
using Excel = Microsoft.Office.Interop.Excel;
更多信息:
实际上,我刚刚设法找到了我以前从Excel开始使用的。下载并添加EPPlus.dll作为您的C#项目的参考,您可以使用以下解决方案解决您的问题
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Drawing;
namespace Excel_Report
{
public partial class MReport : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
private void ExcelOut(DataTable tbl)
{
using (ExcelPackage pck = new ExcelPackage())
{
string DocName = "A_Report_" + DateTime.Now;
DocName = DocName.Replace("/", "_").Replace(" ", "_");
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(DocName);
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(tbl, true);
ws.Cells[ws.Dimension.Address].AutoFitColumns(); ;
//Format the header for column A-Z
using (ExcelRange rng = ws.Cells["A1:U1"])
{
rng.Style.Font.Bold = true;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid
rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189)); //Set color to dark blue
rng.Style.Font.Color.SetColor(Color.White);
}
//Write it back to the client
byte[] renderedBytes;
renderedBytes = pck.GetAsByteArray();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + DocName + ".xlsx");
Response.BinaryWrite(renderedBytes);
Response.End();
}
}
//This is the Save Report Click event
protected void Save_RPT_BTN_Click(object sender, EventArgs e)
{
//Finding my GridView, so on your blank.aspx page create a Gridview
ContentPlaceHolder ContentPH = (ContentPlaceHolder)Master.FindControl("Main_CPH_01");
GridView gridView001 = (GridView)ContentPH.FindControl("GridView1");
//This tells the GridView to grab its Data from its DataSource
gridView001.DataBind();
//Create a new DataTable to save the GridView's data
DataTable RPT_DT01 = new DataTable();
//Create a connection string and a query string both of which are using parts of a SqlDataSource that belong to the GridView
string aConnString = SqlDataSource1_RPT.ConnectionString.ToString();
string queryString = SqlDataSource1_RPT.SelectCommand.ToString();
//Set the properties to connect to your database
using (SqlConnection connection =
new SqlConnection(aConnString))
{
// Set the properties for the database query
SqlCommand command =
new SqlCommand(queryString, connection);
//Open a connection to the database
connection.Open();
//Create a Sql Reader
SqlDataReader reader01 = command.ExecuteReader();
if (connection.State == ConnectionState.Open)
{
// Call Read before accessing data.
reader01.Read();
// Load the datatable with the data from the Sql reader
RPT_DT01.Load(reader01);
//Create the Excel File from the Data table
ExcelOut(RPT_DT01);
// Call Close when done reading.
reader01.Close();
}
}
}
}
}
i代码1包括Microsoft.Office.Interop.Excel;但是我得到了microsoft.office.core。你应该添加对microsoft.office.Interop.Excel的引用,对microsoft.office.core的引用将自动出现。我的答案在几天前更新了。看看这对你是否有效。查看教程。