C#調用水晶報表以及報表導出


初次研究,僅寫了幾個常用的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using CrystalDecisions.Windows.Forms;

namespace Util
{
    public class CrystalReport
    {

        private ReportDocument report = null;
        private CrystalReportViewer crv = null;

        public CrystalReport(string rptFile)
        {
            report = new ReportDocument();
            report.Load(rptFile);
        }

        /// <summary>
        /// 設置報表的連接數據源
        /// </summary>
        /// <param name="serverName"></param>
        /// <param name="dataBase"></param>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        public void SetDataSource(string serverName, string dataBase, string userId, string password)
        {
            ConnectionInfo connInfo = new ConnectionInfo();
            connInfo.ServerName = serverName;
            connInfo.DatabaseName = dataBase;
            connInfo.UserID = userId;
            connInfo.Password = password;

            foreach (Table table in report.Database.Tables)
            {
                TableLogOnInfo info = table.LogOnInfo;
                info.ConnectionInfo = connInfo;
                table.ApplyLogOnInfo(info);
            }
        }

        /// <summary>
        /// 將報表導出為字節數組,以便於寫成文件
        /// </summary>
        /// <returns></returns>
        public byte[] ExportData()
        {
            byte[] data= null;
            using (Stream sm = report.ExportToStream(ExportFormatType.PortableDocFormat))
            {
                int len = 1024, num = 0;
                data = new byte[sm.Length];
                byte[] b = new byte[1024];
                while ((len = sm.Read(b, 0, 1024)) != 0)
                {
                    Array.Copy(b, 0, data, num, len);
                    num += len;
                }

                sm.Close();
            }

            return data;
        }

        /// <summary>
        /// 導出到文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>true為導出成功,false為導出失敗</returns>
        public bool ExportToFile(string fileName)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Create);

                byte[] data = ExportData();
                if (data != null)
                {
                    fs.Write(data, 0, data.Length);
                }
                fs.Close();
                return true;
            }
            catch (Exception ex)
            {
                fs.Close();
                throw ex;
                return false;
            }
        }

        /// <summary>
        /// 獲取報表預覽器,將其添加到窗體的控件組中即可顯示
        /// </summary>
        /// <returns></returns>
        public CrystalReportViewer GetViewer()
        {
            if (crv == null)
            {
                crv = new CrystalReportViewer();
            }
            crv.ReportSource = report;
            return crv;
        }

        public void Dispose()
        {
            if (crv != null)
            {
                crv.Dispose();
            }
        }
    }

    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM