1.运用 div+js 选择区域打印
首先在<HEAD></HEAD>之间写 js 代码:
<script type="text/javascript">
function print_ele_f(id_s)
{
var ele = document.getElementById(id_s);
var w = window.open('about:blank');
w.document.body.innerHTML = ele.innerHTML;
w.print();
}
</script>
然后再要打印的区域用 <div id="contents1" style="text-align: center"></div>把这个区域包含
进去。新建一个按钮<input type="button" runat="server" id="Button5"
value="网页打印" OnClick="print_ele_f('contents1');" />
2.导出打印,datagrid 导出到 excel 打印。
先写如下代码:
public override void VerifyRenderingInServerForm(Control control)//这个不可少,不然会报错
的
{
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;
filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
然后再相应的事件里写调用打印的功能代码如下:
string filename = "excel 名字.xls";
Export("application/ms-excel", filename);
导出 word 跟这个类似只是最后那句 Export("application/ms-excel", filename);换成了
Export("application/ms-word", filename);但是用 word 会有一个明显的问题,就说 word 里出