C#打印EXCEL(一)

来源:互联网 发布:数码暴龙网络侦探攻略 编辑:程序博客网 时间:2024/06/01 09:48


0.引用命名空间
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;

1.首先需要获取到要打印的内容

 /// <summary>        /// 获取页面内容        /// </summary>        /// <returns></returns>        public string GetStream()        {            StringBuilder table = new StringBuilder("");            string strwhere = GetWhere();                      string sql = "SELECT a.JBXX_XMMC,a.JBXX_SEC ,a.JBXX_XZQMC,c.NAME  ,b.JH_GM_ZMJ ,b.JH_TZ_ZTZ  ,JBXX_XMJD "                    + " from TDZZPX.v_jbxx a "                    + " LEFT JOIN TDZZPX.Z_JH b ON a.JBXX_SEC = b.JBXX_SEC "                    + " LEFT JOIN gt_xmlx c ON a.JBXX_XMLX = c.NO";            sql += " WHERE 1=1 " + strwhere;            DataTable dt = BP.DA.DBAccess.RunSQLReturnTable(sql);            table.Append("<table style='padding: 0;margin: 0;' cellspacing=\"0\">");            string thcss = "color: #4f6b72; border-right: 1px solid #C1DAD7; border-bottom: 1px solid #C1DAD7; border-top: 1px solid #C1DAD7; letter-spacing: 2px; text-transform: uppercase;text-align: left; padding: 6px 6px 6px 12px; background: #CAE8EA no-repeat;font-size: 14px;";            table.Append("<tr>");            table.Append("<th style='" + thcss + "'>项目名称</th>");            table.Append("<th style='" + thcss + "'>项目编号</th>");            table.Append("<th style='" + thcss + "'>项目位置</th>");            table.Append("<th style='" + thcss + "'>项目类型</th>");            table.Append("<th style='" + thcss + "'>总规模</th>");            table.Append("<th style='" + thcss + "'>总投资</th>");            table.Append("<th style='" + thcss + "'>项目进度</th>");            table.Append("</tr>");            if (dt != null && dt.Rows.Count > 0)            {                foreach (DataRow row in dt.Rows)                {                    string tdcss = "style='border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;background: #fff;font-size: 14px;padding: 6px 6px 6px 12px;'";                    table.Append("<tr>");                    table.Append("<td " + tdcss + ">");                    table.Append(row["JBXX_XMMC"].ToString());                    table.Append("</td>");                    table.Append("<td " + tdcss + ">");                    table.Append(row["JBXX_SEC"].ToString());                    table.Append("</td>");                    table.Append("<td " + tdcss + ">");                    table.Append(row["JBXX_XZQMC"].ToString());                    table.Append("</td>");                    table.Append("<td " + tdcss + ">");                    table.Append(row["NAME"].ToString());                    table.Append("</td>");                    table.Append("<td " + tdcss + ">");                    table.Append(row["JH_GM_ZMJ"].ToString());                    table.Append("</td>");                    table.Append("<td " + tdcss + ">");                    table.Append(row["JH_TZ_ZTZ"].ToString());                    table.Append("</td>");                    table.Append("<td " + tdcss + ">");                    table.Append(GetJDMC(row["JBXX_XMJD"].ToString()));                    table.Append("</td>");                    table.Append("</tr>");                }            }            table.Append("</table>");            return table.ToString();        }

2.其次需要将打印内容封装

  /// <summary>        /// 导出EXCEL        /// </summary>        void exportexcel()        {            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");            Response.Clear();            Response.Buffer = true;            Response.Charset = "gb2312";            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");            Response.ContentType = "application/ms-excel";            this.EnableViewState = false;            string printStr = GetStream();            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);            oHtmlTextWriter.WriteLine(printStr);            Response.Write(oStringWriter.ToString());            Response.End();        }


3.最后调用该打印方法即可

  if (!IsPostBack)            {                exportexcel();            }


参考资源:http://download.csdn.net/detail/hugaozhuang/7338227




0 0
原创粉丝点击