• C#自定义导出Excel


            //导出Excel
            protected void ibtExcel_Click(object sender, ImageClickEventArgs e)
            {
                decimal s1 = 0;
                decimal s2 = 0;
                decimal s3 = 0;
                decimal s4 = 0;

                Response.Clear();
                Response.AddHeader("content-disposition""attachment;filename=FileName.xls");
                Response.Charset = "gb2312";
                Response.ContentType = "application/vnd.xls";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

                GridView gv = new GridView();
                gv = GridView1;
                DataSet ds = DbHelperSQL.Query(Session["dt"].ToString());
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                        decimal d1 = decimal.Parse(dr[1].ToString());
                        decimal d2 = decimal.Parse(dr[2].ToString());
                        decimal d3 = decimal.Parse(dr[3].ToString());
                        decimal d4 = decimal.Parse(dr[4].ToString());

                        s1 += d1;
                        s2 += d2;
                        s3 += d3;
                        s4 += d4;

                        gv.DataKeyNames = new string[] { "dw" };
                        gv.DataSource = ds.Tables[0].DefaultView;
                        gv.DataBind();
                }

                gv.DataBind();
                gv.RenderControl(htmlWrite);
                
                StringBuilder sb = new StringBuilder();
                sb.Append("<div><table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" AllowCustomPaging=\"True\" border=\"1\" id=\"tbd\" style=\"background-color:White;border-color:#ECE9D8;border-1px;border-style:None;100%;border-collapse:collapse\"><tr><td colspan=\"6\" style=\"font-size:15px;font-weight:bolder; text-align:center\">合同信息查看</td></tr><tr><td>审定日期</td><td>" + tbFromDate.Value +""+ tbToDate.Value + "</td><td>合同类型</td><td>" + ddlType.SelectedValue + "</td><td>卖方性质</td><td>" + ddlPG_Attr.SelectedValue + "</td></tr><tr><td>知识产权类型</td><td>" + ddlPact_PR.SelectedValue + "</td><td>买方国别</td><td>" + ddlCountry.SelectedValue + "</td><td>买方地区</td><td>" + tbBs_Name.Value + "</td></tr><tr><td>买方性质</td><td>" + ddlBuyer_Attr.SelectedValue + "</td><td>技术领域</td><td>" + ddlPact_TD.SelectedValue + "</td><td></td><td></td></tr></table>")
                sb.Append(htmlWrite.InnerWriter.ToString().Replace("<div>""").Replace("</table>""").Replace("</div>"""));
                sb.Append("<tr><td align=\"center\" colspan=\"2\">合计</td><td align=\"center\">" + s1.ToString() + "</td><td align=\"center\">" + s2.ToString() + "</td><td align=\"center\">" + s3.ToString() + "</td><td align=\"center\">" + s4.ToString() + "</td></tr></table></tr></div>");
               
                Response.Write(sb.ToString());
                Response.End();
                gv.DataBind();
            }
    效果图:
  • 相关阅读:
    装饰模式
    普元EOS生成WebService时使用自定义实体映射属性
    Mysql字符串查询注意事项(空格敏感及部分字段大小写敏感问题)
    java数组转换为集合-Arrays.asList使用
    Java报异常时getMessage()方法返回null
    118. Pascal's Triangle
    13. Roman to Integer
    9. Palindrome Number
    8. String to Integer (atoi)
    7. Reverse Integer
  • 原文地址:https://www.cnblogs.com/lhws/p/2392607.html
Copyright © 2020-2023  润新知