希望您能够帮助我。我正在开发一个网页,在aspx中生成一个GridView,它可以在点击时按下一个按钮,将gridview导出到excel文件。第二种情况对我不起作用,文件为空。这是代码:
protected void Button2_Click(object sender, EventArgs e) //click en botón Exportar- -Click to export
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "CUSS - Global Stats.xls"));
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
System.IO.StringWriter writer = new StringWriter();
System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
GridView1.AllowPaging = false;
GridView1.RenderControl(html); //gpp prints outs
Response.Write(writer.ToString());
Response.Flush();
Response.End();
}
catch (Exception)
{
}
Gridview在asp中正确生成,如pic.GridViewResult所示
protected void Button1_Click(object sender, EventArgs e)
{
...
GridView1.DataSource = Prints.ToList();
GridView1.DataBind();
...
}
你能帮我么?
应该有一个更好的方法,但我再次'我的网格视图:using ClosedXML.Excel;
点击事件:
public void button1_Click(object sender, EventArgs e)
{
using(XLWorkbook libro_trabajo = new XLWorkbook())
{
DataSet ps = datos_referencias();
libro_trabajo.Worksheets.Add(ps);
libro_trabajo.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
libro_trabajo.Style.Font.Bold = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=reporte_clientes.xlsx");
using (MemoryStream memoria = new MemoryStream())
{
libro_trabajo.SaveAs(memoria);
memoria.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
获取我的数据集:
public DataSet datos_referencias()
{
DataSet ds = new DataSet();
DataTable tabla_detallado = new DataTable("Clientes");
tabla_detallado.Columns.Add(new DataColumn("Cliente", typeof(string)));
objeto = new Conexion();
try
{
objeto.abrir_conexion_mysql();
objeto.cadena_comando_mysql = "SELECT * from ...ETC ";
objeto.aplicar_comando_mysql_extraccion();
while (objeto.leer_comando.Read())
{
DataRow fila = tabla_detallado.NewRow();
fila["Cliente"] = (objeto.leer_comando.GetString(1)).ToUpper();
tabla_detallado.Rows.Add(fila);
}
}
finally {objeto.cerrar_conexion_mysql_extraccion(); }
ds.Tables.Add(tabla_detallado);
return ds;
}
也许是因为GZip的回应?
在过滤器上?
response.Filter = New GZipStream(response.Filter, CompressionMode.Compress)