• 頁面設置主要是指設置打印文檔的頁邊距、頁眉、頁腳、紙張等內容。頁面設置將直接影響到打印文檔版面的生成效果,因此它和打印文檔的生成有着密切的關係。好比:表格的行數、大小、位置、字體的大小等。
現有的技術是利用IE6.0 內置的打印模板方式來控制頁面設置,其能夠對打印目標文檔產生很是大的影響。打印模板能夠控制頁邊距、頁眉、頁腳、奇偶頁等內容,並能夠將用戶的設置取得,還能夠將設置發送到服務器端。打印模板技術能夠自定預覽窗口和打印格式,最大限度地影響目標文檔和打印效果。
IE直接打印
• 經常使用方法:大部分狀況會把查詢的結果綁定到DataGrid上來,而後打印DataGrid。這種狀況的打印通常來講格式比較固定簡單,肯定後基本不會再做更改。因此能夠採用IE直接打印。
<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 控件技術
• 打印操做的實現
此功能的實現主要是利用WebBrowser控件的函數接口來實現打印、打印預覽(默認的)、
頁面設置(默認的)。
<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中去打印
• 能夠在服務端或者客戶端進行。
• 優勢:使用這種方法,可適應性比較強,控制較好。
• 缺點:在服務端使用的話,要求服務端要安裝Word,Excel,在客戶端使用的話,要
求客戶端在IE的安全設置上有必定要求。
代碼以下:
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組件
• 優勢:這種打印方式對於格式變化大,數據量小的應用來講很是合適。
• 缺點:
– 須要客戶端安.Net framework組件。
– Xml的解析上,若是文件較大速度上不是很理想。
– 頁面首次加載時會有明顯的延時。
使用XSL和XSLT轉換Xml
• XSL:擴展樣式表語言,能夠經過它來把Xml轉換爲其餘的文本格式
• XSL轉換包括髮現或者選擇一個模式匹配,經過使用XPath選擇一個結果集,而後對結果集中的每一項,爲這些匹配定義結果輸出。
• XSL是一個功能強大的工具,能夠把Xml轉換成任何你想要的格式。
代碼以下:
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控件打印
利用第三方控件
• 本身開發控件。這種方式不少商用軟件採用這種方式,寫成控件後已經無所謂是在web中使用仍是應用程序中使用了。
• 優勢:打印方式很是靈活,基本上程序能作到的web也能作獲得。
• 缺點:客戶端須要安裝組件,部署不是很方便。
使用水晶報表
• 用戶僅須要Web 瀏覽器就能夠查看報表
• 報表查看器控件能夠是應用程序中衆多控件之一。
• 與報表輕鬆交互
• 用戶可將報表導出爲Microsoft word 和Excel 格式,以及PDF、HTML 和Crystal Reports for visual Studio .Net格式。
• 可使用報表控件直接打印
代碼以下:
//水晶報表的填充,省略鏈接代碼
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);