无为清净楼资源网 Design By www.qnjia.com
B/S结构导致了Web应用程序中打印的特殊性。
  "codetitle">复制代码 代码如下:
<input type="button" name="print" value="预览并打印" onclick="preview()">

  ②如果直接使用window.print将打印页面上的所有内容,但是我们可以使用
复制代码 代码如下:
<script language="Javascript">
function preview()
{
bdhtml=window.document.body.innerHTML;
sprnstr="<!--startprint-->";
eprnstr="<!--endprint-->";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
}
</script>
<!--省略部分代码-->
<form id="WebForm1" method="post" runat="server">
<center>本部分以上不被打印</center>
<!--startprint-->
<div align="center">
<asp:DataGrid id="dgShow" runat="server">
<!--省略部分代码-->
</asp:DataGrid>
</div>
<!--endprint-->
<center>本部分以下不被打印</center>
<div align="center">
<input type="button" name="print" value="预览并打印" onclick="preview()">
</div>
<style> @media Print { .Noprn { DISPLAY: none }}
</style>
<p class="Noprn">不打印</p>
<table id="datagrid">
<tr>
<td>打印</td>
</tr>
</table>
<input class="Noprn" type="button" onclick="window.print()" value="print">
</form>

WebBrowser 控件技术
  "codetitle">复制代码 代码如下:
<object ID=‘WebBrowser1' WIDTH=0 HEIGHT=0
CLASSID=‘CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'>
//打印
WebBrowser1.ExecWB(6,1);
//打印设置
WebBrowser1.ExecWB(8,1);
//打印预览
WebBrowser1.ExecWB(7,1);
//直接打印
WebBrowser1.ExecWB(6,6);
//自定义类PrintClass
public string DGPrint(DataSet ds)
{
//DGPrint执行的功能:根据DataTable转换成对应的HTML对应的字符串
DataTable myDataTable=new DataTable();
myDataTable=ds.Tables[0];
int myRow=myDataTable.Rows.Count;
int myCol=myDataTable.Columns.Count;
StringBuilder sb=new StringBuilder();
string colHeaders="<html><body>"+"<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>" +"<table><tr>";
for(int i=0;i<myCol;i++)
{
colHeaders +="<td>"+ myDataTable.Columns[i].ColumnName.ToString()+"</td>";
}
colHeaders += "</tr>";
sb.Append(colHeaders);
for(int i=0;i<myRow;i++)
{
sb.Append("<tr>");
for(int j=0;j<myCol;j++)
{
sb.Append("<td>");
sb.Append(myDataTable.Rows[i][j].ToString().Trim());
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table></body></html>");
colHeaders=sb.ToString();
colHeaders+="<script languge='Javascript'>WebBrowser.ExecWB(6,1); window.opener=null;window.close();</script>";
return(colHeaders);
}

//页面:打印按钮事件
PrintClass myP = new PrintClass();
Response.Write(myP.DGPrint(Bind());
  在把DataGrid转换为对应的HTML代码时,如果存在按钮列就会报错,最好把这一列隐藏,一般只能转换数据列。其次要注意分页问题,一般只能打印当前一页,最好在打印之前除掉分页
导出到Excel,Word中去打印
  "codetitle">复制代码 代码如下:
protected void btnMIME_Click(object sender, System.EventArgs e)
{
BindData();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "inline;filename="+HttpUtility.UrlEncode("下载文件.xls",Encoding.UTF8));
//如果输出为Word,修改为以下代码
//Response.ContentType = "application/ms-word"
//Response.AddHeader("Content-Disposition", "inline;filename=test.doc")
StringBuilder sb=new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append("<html><body>");
dgShow.RenderControl(hw);
sb.Append("</body></html>");
Response.Write(sb.ToString());
Response.End();
}
protected void btnCom_Click(object sender, System.EventArgs e)
{
ExportToExcel(BindData(),Server.MapPath("ComExcel.xls"));
}
//从DataSet到出到Excel
#region从DataSet到出到Excel
///导出指定的Excel文件
public void ExportToExcel(DataSet ds,string strExcelFileName)
{
if (ds.Tables.Count==0 || strExcelFileName=="") return;
doExport(ds,strExcelFileName);
}
///执行导出
private void doExport(DataSet ds,string strExcelFileName)
{
excel.Application excel= new excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable table=ds.Tables[0] ;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
excel.Visible=false;
excel.ActiveWorkbook.SaveAs(strExcelFileName+".XLS",Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
excel.Quit();
excel=null;
GC.Collect();//垃圾回收
}
#endregion

利用.Net组件打印
  利用.Net组件
  "codetitle">复制代码 代码如下:
XslTransform xslt = new XslTransform();
xslt.Load(Server.MapPath( "StudentsToHTML.xsl") );
XPathDocument XDoc = new XPathDocument(Server.MapPath( "Students.Xml" ));
XmlWriter writer = new XmlTextWriter( server.MapPath("Students.html"), System.Text.Encoding.UTF8 );
xslt.Transform( XDoc, null, writer );
writer.Close();
Response.Redirect("Students.html");

利用ActiveX控件打印
  利用第三方控件
  "codetitle">复制代码 代码如下:
//水晶报表的填充,省略连接代码
myReport ReportDoc = new myReport();
ReportDoc.SetDataSource(ds);
Crv.ReportSource = ReportDoc;
//输出为指定类型文件
CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
ReportDoc.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
string strFileName = server.MapPath("Output");
switch (ddlFormat.SelectedItem.Text)
{
case "Rich Text (RTF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;
DiskOpts.DiskFileName =strFileName + ".rtf";
break;
case "Portable Document (PDF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
DiskOpts.DiskFileName = strFileName + ".pdf";
break;
case "MS word (DOC)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
DiskOpts.DiskFileName = strFileName + ".doc";
break;
case "MS excel (XLS)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;//
DiskOpts.DiskFileName = strFileName + ".xls";
break;
default:
break;
}
ReportDoc.ExportOptions.DestinationOptions = DiskOpts;
ReportDoc.Export();
//打印
// 指定打印机名称
string strPrinterName;
strPrinterName = @"Canon Bubble-Jet BJC-210SP";
// 设置打印页边距
PageMargins margins;
margins = ReportDoc.PrintOptions.PageMargins;
margins.bottomMargin = 250;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 450;
ReportDoc.PrintOptions.ApplyPageMargins(margins);
//应用打印机名称
ReportDoc.PrintOptions.PrinterName = strPrinterName;
// 打印 // 打印报表。将startPageN 和endPageN
// 参数设置为0 表示打印所有页。
ReportDoc.PrintToPrinter(1, false,0,0);
标签:
页面打印,打印

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com