从sql数据创建.xlsx文件 [英] creating .xlsx file from sql data

查看:77
本文介绍了从sql数据创建.xlsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
使用System.Configuration;
使用System.IO;


public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
ShowData();
string dbcon = ConfigurationManager.ConnectionStrings [cn]。ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{

}
protected void Button1_Click(object sender,EventArgs e)
{
//Excel2007.WorkBook workbook = new Excel2007.WorkBook();

Response.Clear();
Response.BufferOutput = false;
string archiveName =customer.xls;
Response.ContentType =application / zip;
Response.AddHeader(content-disposition,filename =+ archiveName);
Response.Charset =;
// workbook.WorkSheets.Add(FirstSheet);
// workbook.WorkSheets [FirstSheet]。Cells [0,1] .Value =first;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false; GridView1.DataBind();
GridView1.HeaderRow.Style.Add(background-color,#FFFFFF);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
//workbook.Save(Response.OutputStream);
Response.End();
}

public void ShowData()
{

SqlDataAdapter adp = new SqlDataAdapter(从quizcats中选择catname,catid,creationdate,pagename, ConfigurationManager.ConnectionStrings [ CN]的ConnectionString)。
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}

解决方案

请关注并尝试链接



在ASP中将Gridview数据导出到Excel。 NET [ ^ ]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ShowData();
        string dbcon = ConfigurationManager.ConnectionStrings["cn"].ToString();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Excel2007.WorkBook workbook = new Excel2007.WorkBook();

        Response.Clear();
        Response.BufferOutput = false;
        string archiveName = "customer.xls";
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + archiveName);
        Response.Charset = "";
      //  workbook.WorkSheets.Add("FirstSheet");
       // workbook.WorkSheets["FirstSheet"].Cells[0, 1].Value = "first";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false; GridView1.DataBind();
        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        //workbook.Save(Response.OutputStream);
        Response.End();
    }

    public void ShowData()
    {

        SqlDataAdapter adp = new SqlDataAdapter("select catname,catid,creationdate,pagename from quizcats  ", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

解决方案

Please follow and try the link

Export Gridview Data to Excel in ASP.NET[^]


这篇关于从sql数据创建.xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆