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; }
}
}
服务器做新版本的模板,导出PDF
最新推荐文章于 2024-07-15 17:39:15 发布