C# excel导出

本文介绍了一种使用C#从数据库中导出员工薪资数据到Excel文件的方法。该方法根据用户输入的关键字筛选数据,并将结果以表格形式输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///
/// 导出数据
///
///
///
protected void btnprint_Click(object sender, EventArgs e)
{
string sql = “”;
if (txtKey.Text == “”)
sql = “select * from VI_GongZi where (” + ddltype.SelectedValue + " like ‘%" + txtKey.Text.Trim() + "%’) order by GongZiId desc";
else
sql = “select * from VI_GongZi order by GongZiId desc”;

    DataTable dt = DB.getDataTable(sql);
    StringBuilder str = new StringBuilder();
    str.Append("<meta http-equiv=\"content-type\" content=\"application/excel;charset=utf-8\" />");
    str.Append("<table border='1'>");

    str.Append("<tr style='background:#cccccc;'>");
    str.Append("<td>员工编号 </td>");
    str.Append("<td>员工姓名 </td>");
    str.Append("<td>考勤说明 </td>");
    str.Append("<td>添加时间 </td>");
    str.Append("<td>基本工资 </td>");
    str.Append("<td>加班费 </td>");
    str.Append("<td>出差补助 </td>");
    str.Append("<td>异常考勤扣款 </td>");
    str.Append("<td>合计工资 </td>");
    str.Append("</tr>");

    foreach (DataRow row in dt.Rows)
    {
        str.Append("<tr>");
        str.Append("<td>" + row["userid"] + "</td><td>" + row["username"] + "</td><td>" + row["title"] + "</td>");
        str.Append("<td>" + row["addtime"] + "</td><td>" + row["gongzi"] + "</td><td>" + row["jiaban"] + "</td>");
        str.Append("<td>" + row["chuchai"] + "</td><td>" + row["koukuan"] + "</td><td>" + row["total"] + "</td>");

        str.Append("</tr>");
    }
    str.Append("</table>");

    this.Page.Response.Clear();
    this.Page.Response.Buffer = false;
    this.Page.Response.Charset = "UTF-8";
    this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=Print_GongZi_Data.xls");
    this.Page.Response.ContentEncoding = Encoding.UTF8;
    this.Page.Response.ContentType = "application/excel";
    this.Page.EnableViewState = false;
    this.Page.Response.Write(str.ToString());
    this.Page.Response.End();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值