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
- C#打印EXCEL(一)
- C#打印EXCEL(三)
- C#打印EXCEL(二)
- C# 设置Excel打印选项及打印excel文档
- C# 设置Excel打印选项及打印excel文档
- C# Excel操作(一)
- C#打印技术(一)
- C#创建与Excel打印输出(转)
- C#创建与操作Excel打印输出
- C# 导出 Excel 和相关打印设置
- C# 导出 Excel 和相关打印设置
- C# 导出 Excel 和相关打印设置
- C#关于Excel导出及打印
- C# 导出 Excel 和相关打印设置
- c#调用excel的打印预览页
- c# 对excel数据进行打印
- C#控制Excel的打印格式
- C# 导出 Excel 和相关打印设置
- 十二大Android开发资源
- modelsim SE-64 10.1c 波形加载慢的问题解决办法
- PHP filter_input() 详解
- Fibonacci String 1708
- 年月日三下拉框联动
- C#打印EXCEL(一)
- Instruments-Automation: 通过命令行运行测试用例
- .net 调用webservice 总结
- php配置可调用curl函数的方法
- 给表格添加滚动条
- C#控件集DotNetBar安装及破解
- CSS基础知识梳理-background
- 一致性哈希算法(consistent hashing)例子+测试。
- warning C4996: “strcpy”被声明为否决的解决办法