Я хочу экспортировать gridview в excel 2007, код, который я использую, может импортировать в excel 2007 с помощью приложения типа mime/vnd.ms-excel (excel 2003), но я получаю предупреждение msg что «Файл, который вы пытаетесь открыть, находится в другом формате ...», с да и нет, чтобы клик, нажав кнопку «Да», файл открывается, купите, у меня не может быть этого msg для клиентов. И с помощью mime type for excel 2007 (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet), файл даже не открывается. «Excel не может открыть файл, потому что формат или расширение недействительны».Экспорт GridView в Excel 2007
Это код, я использую прямо сейчас:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Drawing;
namespace TesteFornecedores
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
using (DataSet ds = new DataSet())
{
ds.ReadXml(Server.MapPath("~/Customers.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ExcelList");
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
this.BindGrid();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex%2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}
}
Кто-то знает решение баночной помочь мне открыть в Excel 2007 это GridView?
Спасибо.