关于ASP.NET页面打印技术的总结(2)

上一篇 / 下一篇  2007-07-18 08:56:00 / 个人分类:读说听

导出到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);    


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-22  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 4392
  • 日志数: 114
  • 建立时间: 2006-10-09
  • 更新时间: 2008-05-09

RSS订阅

Open Toolbar