ASP.NET打印代码

Excel操作类代码:
using System;
using System.Data;
using System.Web;
using System.IO;
using System.Diagnostics;

namespace XHONE
{
    
/// <summary>
    
/// Web Excel模板打印
    
/// </summary>

    public class ProcessReport
    
{
    
        
/// <summary>
        
/// Excel应用程序
        
/// </summary>

        private Microsoft.Office.Interop.Excel.ApplicationClass m_oExcelApp;
             
        
/// <summary>
        
/// Excel全部工作薄
        
/// </summary>

        private Microsoft.Office.Interop.Excel.Workbooks m_oBooks;
        
/// <summary>
        
/// Excel指定工作薄
        
/// </summary>

        //private Excel._Workbook            m_oBook;
        private Microsoft.Office.Interop.Excel.Workbook m_oBook;
        
/// <summary>
        
/// Excel工作单张
        
/// </summary>

        //private Excel._Worksheet        m_oSheet;
        private Microsoft.Office.Interop.Excel.Worksheet m_oSheet;
        
        
/// <summary>
        
/// Object的空值
        
/// </summary>

        private object                    m_oMissing;
        
/// <summary>
        
/// Excel文件模板引用
        
/// </summary>

        private    string                    m_sReportTemplate;
        
/// <summary>
        
/// Excel报表文件工作表索引引用
        
/// </summary>

        private    int                        m_nReportIndex;
        
        
private bool bSaveOpen; 
        
private DateTime beforeTime;            //Excel启动之前时间
        private DateTime afterTime;                //Excel启动之后时间
        

    
        
        
/// <summary>
        
/// 当前工作区空间
        
/// </summary>

        //public Excel._Workbook workBook
        public Microsoft.Office.Interop.Excel.Workbook workBook

        
{
            
get{return m_oBook;}
        }

        
        
/// <summary>
        
/// 当前工作表
        
/// </summary>

        //public Excel._Worksheet workSheet
        public Microsoft.Office.Interop.Excel.Worksheet workSheet

        
{
            
get{return m_oSheet;}
        }

        
/// <summary>
        
/// Excel报表文件模板
        
/// </summary>

        public string ReportTemplate  
        

            
get return m_sReportTemplate; } 
            
set { m_sReportTemplate = value;} 
        }

    
        
/// <summary>
        
/// Excel报表文件工作表索引(注意其从1开始算起)
        
/// </summary>

        public int ReportIndex
        

            
get return m_nReportIndex; }
            
set { m_nReportIndex = value;}
        }



        
public bool SaveOpen
        
{
            
get {return bSaveOpen;}
            
set {bSaveOpen=value;}
        }

        
/// <summary>
        
/// Excel模板打印初始化
        
/// </summary>

        public ProcessReport()
        
{
            m_oExcelApp        
= null;
            m_oBooks        
= null;
            m_oBook            
= null;
            m_oSheet        
= null;
            m_oMissing        
= System.Reflection.Missing.Value;
            ReportIndex        
= 1;
            SaveOpen
=true;//默认为存盘
        }



    
        
/// <summary>
        
/// 打开Excel报表文件,并使其可见
        
/// </summary>

        public void OpenReportTempalte()
        
{
            
try
            
{
//                if(m_oExcelApp != null)
//                {
//                    CloseReportTemplate();
//                }
                beforeTime = DateTime.Now;
                m_oExcelApp 
= new Microsoft.Office.Interop.Excel.ApplicationClass();
                m_oExcelApp.Visible 
= false;
                afterTime 
= DateTime.Now;
                m_oBooks            
= m_oExcelApp.Workbooks;
            
                
// EXCEL 版本>= 10.0 OfficeXP以上
                m_oBook = m_oBooks.Open(m_sReportTemplate,m_oMissing,m_oMissing,
                    m_oMissing,m_oMissing,m_oMissing,m_oMissing,m_oMissing,m_oMissing, 
                    m_oMissing,m_oMissing,m_oMissing,m_oMissing,m_oMissing,m_oMissing);
                
// EXCEL 版本>= 10.0 OfficeXP以上

                
// EXCEL 版本<= 9.0 Office2000以下
//                            m_oBook = m_oBooks.Open(m_sReportTemplate, m_oMissing, m_oMissing,
//                                m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, 
//                                m_oMissing, m_oMissing, m_oMissing, m_oMissing);
                
// EXCEL 版本<= 9.0 Office2000以下

                m_oSheet 
= (Microsoft.Office.Interop.Excel.Worksheet)m_oBook.Worksheets[ReportIndex];//(Excel._Worksheet)m_oBook.Worksheets[ReportIndex];
            }

            
catch(Exception err)
            
{
                
throw err;
            }

        }


        
/// <summary>
        
/// 关闭服务器相关Excel引用进程
        
/// </summary>

        public void CloseReportTemplate()
        
{
            
try
            
{

                
if(m_oBook != null)
                    
if(SaveOpen)
                        m_oBook.Close(
true, m_sReportTemplate, m_oMissing);
                    
else//只是取数据的不用存盘
                        m_oBook.Close(false, m_oMissing, m_oMissing);
                    

                
if(m_oSheet != null)
                
{
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oSheet);
                    m_oSheet 
= null;
                }

                
if(m_oBook != null)
                
{
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oBook);
                    m_oBook 
= null;
                }

                m_oBooks.Close(); 
                
if(m_oBooks != null)
                
{
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oBooks);
                    m_oBooks 
= null;
                }

                
if(m_oExcelApp != null)
                
{
                    m_oExcelApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oExcelApp);
                    m_oExcelApp 
= null;        
                    
                }

                
//GC.Collect();

            }

            
catch(Exception err)
            
{
                
throw err;
            }

        }

        
/// <summary>
        
/// 关闭进程,必须调用
        
/// </summary>

        public void Dispose()
        
{
            
try
            
{

                
if(m_oBook != null)
                    
if(SaveOpen)
                        m_oBook.Close(
true, m_sReportTemplate, m_oMissing);
                    
else//只是取数据的不用存盘
                        m_oBook.Close(false, m_oMissing, m_oMissing);

                
if(m_oSheet != null)
                
{
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oSheet);
                    m_oSheet 
= null;
                }

                
if(m_oBook != null)
                
{
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oBook);
                    m_oBook 
= null;
                }

                m_oBooks.Close(); 
                
if(m_oBooks != null)
                
{
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oBooks);
                    m_oBooks 
= null;
                }

                
if(m_oExcelApp != null)
                
{
                    m_oExcelApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oExcelApp);
                    m_oExcelApp 
= null;        
                    
                }

                GC.Collect();

            }

            
catch(Exception err)
            
{
                
throw err;
            }

        }

        
/// <summary>
        
/// 访问Excel出错时结束Excel进程
        
/// </summary>

        public void KillExcelProcess()
        
{
            Process[] myProcesses;
            DateTime startTime;
            myProcesses 
= Process.GetProcessesByName("Excel");

            
//得不到Excel进程ID,暂时只能判断进程启动时间
            foreach(Process myProcess in myProcesses)
            
{
                startTime 
= myProcess.StartTime;

                
if(startTime > beforeTime && startTime < afterTime)
                
{
                    myProcess.Kill();
                }

            }

        }

    }

}

调用过程:
/// <summary>
    
/// 打印报表过程,使用Web Excel模板打印
    
/// </summary>

    private void Print2()
    
{
        
try
        
{
            ProcessReport oReport 
= new ProcessReport();    //Web Excel模板对象
            oReport.ReportTemplate = Server.MapPath("ReportTemplates"+ "\\RepairInfo.xls";    //模板
            oReport.ReportIndex = 1;
            
string sReportFile, sMessage;
            
if (!CreatReport(oReport, out sReportFile, out sMessage))   //生成Web Excel 报表
            {
                oReport.KillExcelProcess();
                
this.lblMessage.Text = sMessage;
                
return;
            }

            oReport.Dispose();
            Response.ContentType 
= "application/vnd.ms-excel";
            Response.AddHeader(
"Content-Disposition""inline;filename=MyReport.xls");
            
            
//如果输出为Word,修改为以下代码
            
//Response.ContentType = "application/ms-word" 
            
//Response.AddHeader("Content-Disposition", "inline;filename=test.doc") 
            System.Text.StringBuilder sb = new System.Text.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.WriteFile(Path.GetTempPath() 
+ sReportFile);//(sb.ToString());
            Response.End();

        }

        
catch (System.Threading.ThreadAbortException thrErr)
        
{
            System.Diagnostics.Debug.WriteLine(thrErr.Message 
+ "\r\n" + thrErr.StackTrace);
        }

        
catch (Exception err)
        
{
            
this.WriteErrorXML(err);
        }

    }


    
/// <summary>
    
/// 生成打印报表
    
/// </summary>
    
/// <param name="oReport">报表模板</param>
    
/// <param name="sReportFile">生成的报表路径</param>
    
/// <param name="sMessage">错误信息</param>
    
/// <returns></returns>

    private bool CreatReport(ProcessReport oReport, out string sReportFile, out string sMessage)
    
{
        sReportFile 
= null;
        sMessage    
= "";
        
string sDeleteFile = null;

        
try
        
{
            
//创建0字节空临时文件
            sReportFile = Path.GetTempFileName();
            File.Copy(oReport.ReportTemplate, sReportFile, 
true);
            oReport.ReportTemplate 
= sReportFile;
            oReport.OpenReportTempalte();

            
/************向Excel工作表写入数据************/
            oReport.workSheet.Cells[
3,2= txtRepairNO.Text;
            oReport.workSheet.Cells[
3,4= txtCompanyName.Text;
            oReport.workSheet.Cells[
4,2= txtCorporationName.Text;
            oReport.workSheet.Cells[
4,4= txtTaxNO.Text;
            oReport.workSheet.Cells[
5,2= txtOriSN.Text;
            oReport.workSheet.Cells[
5,4= txtRegName.Text;
            oReport.workSheet.Cells[
6,2= txtBrandName.Text;
            oReport.workSheet.Cells[
6,4= txtTypeName.Text;
            oReport.workSheet.Cells[
7,2= txtDispostRT.Text;
            oReport.workSheet.Cells[
7,4= txtIsKeepRep.Text;
            oReport.workSheet.Cells[
8,2= txtReportDate.Text;
            oReport.workSheet.Cells[
8,4= txtReportUser.Text;
            oReport.workSheet.Cells[
9,2= txtGetDate.Text;
            oReport.workSheet.Cells[
9,4= txtGetUser.Text;
            oReport.workSheet.Cells[
10,2= txtRepairDate.Text;
            oReport.workSheet.Cells[
10,4= txtDisposeRes.Text;
            oReport.workBook.Save();
            
/************向Excel工作表写入数据************/

            
return true;
        }

        
catch (System.Runtime.InteropServices.COMException err)
        
{
            oReport.KillExcelProcess();
            sMessage 
= "调用Excel报表失败!";
            System.Diagnostics.Debug.WriteLine(err.Message 
+ "\r\n" + err.StackTrace);
            
return false;
        }

        
catch (Exception ex)
        
{
            sMessage 
= ex.Message;
            
return false;
        }

        
finally
        
{
            sReportFile 
= sReportFile.Substring(sReportFile.LastIndexOf("\\"+ 1);
            
if (sDeleteFile != null)
                File.Delete(sDeleteFile);
        }

    }


posted @ 2008-07-03 15:40  nikytwo  阅读(1853)  评论(0编辑  收藏  举报