服务器做新版本的模板,导出PDF

using FastReport;
using FastReport.Export.Pdf;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace VilnkLib
{
    public class PrintHelp
    {
      public static string reportPath = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 打印方法
        /// </summary>
        /// <param name="data">打印数据</param>
        /// <param name="TN">模板名称</param>
        /// <param name="PN">打印份数</param>
        /// <param name="pv">打印类型,预览v,直接打印P</param>
        /// <returns></returns>
        public static string getPrint(DataSet dataSet, string TN, DateTime? effective = null)
        {
            JsonParser Jsp = new JsonParser();
            Jsp.data = dataSet;
            Jsp.TN = TN;
            Jsp.pv = "v";
            Jsp.report = "";
            Jsp.PN = "1".ToString();
            string sql = "";
            if (!effective.HasValue)
            {
                effective = DateTime.Parse("2019-09-02");
            }
            if (effective < DateTime.Parse("2019-10-01"))
            {
                effective = DateTime.Parse("2019-10-01");
            }
            if (true)
            {
                sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "' AND effective<='" + effective + "' ORDER BY effective desc";
            }
            else
            {
                sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "'  ORDER BY effective desc";
            }
            string fileName = "/PrintFile/" + TN;
            string path = PrintHelp.reportPath + "/PrintFile/" + TN + ".frl";
            DataTable dts = DBCtr.SqlTable(sql);
            if (dts.Rows.Count > 0)
            {
                path = PrintHelp.reportPath + dts.Rows[0][0].ToString();
                fileName = dts.Rows[0][0].ToString().Replace("/", "_").Replace("\\", "_");
            }
            else
            {
                if (File.Exists(path))
                {
                    DBCtr.ExeScalar("INSERT INTO [VL_Sys_ReportFile]([FileName],[Version],[effective],[FileURL],[AddUser],[AddTime])VALUES('" + TN + "','1.0','2019-10-01','/PrintFile/" + TN + ".frl','sys',getdate())"); 
                } 
            }
            Jsp.report = FileToBase64String(path);
            var iso = new IsoDateTimeConverter();
            iso.DateTimeFormat = "yyyy年MM月dd日 HH:mm:ss";
            return Newtonsoft.Json.JsonConvert.SerializeObject(Jsp, iso).Replace(" 00:00:00", "");

        }

        /// <summary>
        /// 文件转base64
        /// </summary>
        /// <returns>base64字符串</returns>
        public static string FileToBase64String(string Path)
        {
            FileStream fsForRead = new FileStream(Path, FileMode.Open);//文件路径
            string base64Str = "";
            try
            {
                //读写指针移到距开头10个字节处
                fsForRead.Seek(0, SeekOrigin.Begin);
                byte[] bs = new byte[fsForRead.Length];
                int log = Convert.ToInt32(fsForRead.Length);
                //从文件中读取10个字节放到数组bs中
                fsForRead.Read(bs, 0, log);
                base64Str = Convert.ToBase64String(bs);
                return base64Str;
            }
            catch (Exception ex)
            {
                
                return ex.Message;
            }
            finally
            {
                fsForRead.Close();
            }
        }
            public static string getExpPDFQY(DataSet dataSet, string TN) {

            string sql = "";
           
                sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "'  ORDER BY effective desc";
            
            string path = PrintHelp.reportPath + "/PrintFile/" + TN + ".frl";
            DataTable dts = DBCtr.SqlTable(sql);
            if (dts.Rows.Count > 0)
            {
                path = dts.Rows[0][0].ToString();
            }
            else
            {
                if (File.Exists(path))
                {
                    DBCtr.ExeScalar("INSERT INTO [VL_Sys_ReportFile]([FileName],[Version],[effective],[FileURL],[AddUser],[AddTime])VALUES('" + TN + "','1.0','2015-12-01','/PrintFile/" + TN + ".frl','sys',getdate())");
                }
            }
            if (File.Exists(PrintHelp.reportPath + path))
            {
                path = PrintHelp.reportPath + path;
            }
            else
            {
                path = System.Configuration.ConfigurationSettings.AppSettings["FJURL"].ToString() + "/" + path; ;

            }

            Report report = new Report();
            report.Load(path);
            report.RegisterData(dataSet);

            report.Prepare();
            PDFExport export = new PDFExport();
            string str = "/ExpReports/" + TN + DateTime.Now.ToString("yyMMddHHmmss") + ".pdf";
            // export the report
            report.Export(export, reportPath + str);

            // free resources used by report
            report.Dispose();

            return str;
        }

        /// <summary>
        /// 打印方法
        /// </summary>
        /// <param name="data">打印数据</param>
        /// <param name="TN">模板名称</param>
        /// <param name="PN">打印份数</param>
        /// <param name="pv">打印类型,预览v,直接打印P</param>
        /// <returns></returns>
        public static string getExpPDF(DataSet dataSet, string TN, DateTime? effective)
        {
            try
            {
               string strss=HttpContext.Current.Request.QueryString["pattern"];
                if (strss == "design") {
                    return getPrint(dataSet, TN,effective);
                }
                string sql = "";
                if (!effective.HasValue) {
                    effective = DateTime.Parse("2019-09-02");
                }
                if (effective < DateTime.Parse("2019-10-01"))
                {
                    effective = DateTime.Parse("2019-10-01");
                }
                if (true)
                {
                    sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "' AND effective<='" + effective + "' ORDER BY effective desc";
                }
                else
                {
                    sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "'  ORDER BY effective desc";
                }
                string fileName = "/PrintFile/"+TN;
              string path = PrintHelp.reportPath + "/PrintFile/" + TN + ".frl";
                DataTable dts = DBCtr.SqlTable(sql);
                if (dts.Rows.Count > 0)
                {
                    path = PrintHelp.reportPath + dts.Rows[0][0].ToString();
                    fileName = dts.Rows[0][0].ToString().Replace("/","_").Replace("\\", "_");
                }
                else
                {
                    if (File.Exists(path))
                    {
                        DBCtr.ExeScalar("INSERT INTO [VL_Sys_ReportFile]([FileName],[Version],[effective],[FileURL],[AddUser],[AddTime])VALUES('" + TN + "','1.0','2019-10-01','/PrintFile/" + TN + ".frl','sys',getdate())");


                    }


                }
                string guid = Guid.NewGuid().ToString("N");
                Report report = new Report();
                report.Load(path);
                report.RegisterData(dataSet);
                report.Prepare();


                fileName = fileName.Replace("/", "_").Replace("\\", "_");
                PDFExport export = new PDFExport();
                string str = "/ExpReports/" + fileName.Replace(".","_").Replace("@", "_").Replace(",", "_") + DateTime.Now.ToString("yyMMddHHmmss") + ".pdf";
                // export the report
                report.Export(export, reportPath + str);

                // free resources used by report
                report.Dispose();

                return str;
            }
            catch (Exception ee)
            {

                return ee.Message;
            }
        

    } 
    }

    [Serializable]
    public class JsonParser
    {
        /// <summary>
        /// 导出类型  空即打印
        /// </summary>
        public string report { get; set; }
        /// <summary>
        /// 打印类型  默认预览 v 直接打印 P
        /// </summary>
        public string pv { get; set; }
        /// <summary>
        /// 打印份数 整数 默认1
        /// </summary>
        public string PN { get; set; }
        /// <summary>
        /// 模版名称
        /// </summary>
        public string TN { get; set; }
        /// <summary>
        /// 打印数据 DataSet DataTable 包含tableName
        /// </summary>
        public DataSet data { get; set; }
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值