我们可以直接在My PC中将数据存储在Excel文件中 [英] Can we store the data in Excel file in My PC directly

查看:61
本文介绍了我们可以直接在My PC中将数据存储在Excel文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程



 创建  procedure  selectdata 
as
begin
选择 * 来自员工
结束





在我的前端我在 default.aspx 页面中创建了一个按钮

 <   asp:按钮    id   =  btnsave    runat   =   server   文字 <跨班=code-keyword> = 发送至Excel    onclick   =  btnsave_Click  /  >  

< asp:GridView id = gv runat = server >
< / asp:GridView >





default.aspx .cs

<前la ng =c#> protected void btnsave_Click( object sender,EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = 用户ID = aa;密码= 123;初始目录= abc;集成安全性= XXXX;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = selectdata;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gv.DataSource = ds;
gv.DataBind();
}





输出:它在网格视图中显示数据



但是可以将数据直接存储在我的电脑中的 Excel文件中

解决方案

关注...



1. DataSet到Excel分两步 - 使用不同的样式 [ ^ ]

2. 如何从c#中的数据集或数据表导出excel? [ ^ ]


在Default.aspx中

< asp: gridview id =gvrunat =server> 

< / asp:gridview>


< asp:button id =btnsendrunat =servertext =导出到Excelonclick =btnsend_Click/>





在Default.aspx.cs中

 protected void Page_Load(object sender ,EventArgs e)
{
if(IsPostBack!= true)
{
BindGrid();

}
}

private void BindGrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString =Data Source = aa; User Id = aa; Password = aa; Initial Catalog = aa; Integrated Security = xxxx;

SqlCommand cmd = new SqlCommand();
cmd.Connection = con;

cmd.CommandText =select * from student_info;
cmd.CommandType = CommandType.Text;

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;

DataTable dt = new DataTable();
da.Fill(dt);

gv.DataSource = dt;
gv.DataBind();
}

protected void btnsend_Click(object sender,EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader(content-disposition,attachment; filename = abc.doc);
Response.ContentType =application / vnd.ms-word;

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
BindGrid();
gv.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
/ *确认为指定的ASP.NET
服务器呈现HtmlForm控件在运行时控制。 * /
}


I have a Stored Procedure

Create procedure selectdata
as 
begin
    select * from Employee
end



In my front End I Created a Button in default.aspx page

<asp:Button id="btnsave" runat="server" Text="Send to Excel" onclick="btnsave_Click"/>

<asp:GridView id="gv" runat="server" >
</asp:GridView>



In default.aspx.cs

protected void btnsave_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString="User id=aa; password=123; Initial Catalog=abc;Integrated Security=xxxx";
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "selectdata";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    gv.DataSource = ds;
    gv.DataBind();
}



Output: It Displays the Data in Grid View

but is it possible to store the Data directly in Excel file in my PC

解决方案

Follow...

1. DataSet to Excel in Two steps – with Different Styles[^]
2. How to export excel from dataset or datatable in c#?[^]


In Default.aspx

<asp:gridview id="gv" runat="server" >
    
</asp:gridview>
    
    
<asp:button id="btnsend" runat="server" text="Export to Excel" onclick="btnsend_Click"  />



In Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack != true)
        {
            BindGrid();
            
        }
    }

    private void BindGrid()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=aa; User Id=aa; Password=aa; Initial Catalog=aa; Integrated Security=xxxx";

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;

        cmd.CommandText = "select * from student_info";
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        DataTable dt = new DataTable();
        da.Fill(dt);

        gv.DataSource = dt;
        gv.DataBind();
    }

    protected void btnsend_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=abc.doc");
        Response.ContentType = "application/vnd.ms-word";

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw); 
        BindGrid();
        gv.RenderControl(hw);
        Response.Output.Write(sw.ToString()); 
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }


这篇关于我们可以直接在My PC中将数据存储在Excel文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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