You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.3 KiB
C#

using DS.WMS.PrintApi.Model;
using DS.WMS.PrintApi.Utils;
using FastReport.Data.JsonConnection;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using System.IO;
using System.Text;
using System;
using System.Threading.Tasks;
namespace DS.WMS.PrintApi.Service
{
public class OpenPrintService : IOpenPrintService
{
public async Task<PrintDataResult> GetOpenJsonPrintInfo(OpenJsonPrintReq req)
{
try
{
using (var db = SqlSugarUtil.GetInstance())
{
var template = db.Queryable<SysPrintTemplate>().Filter(null,true).Where(x => x.Id == req.TemplateId).First();
if (template == null)
{
return await Task.FromResult(PrintDataResult.Failed("打印模板不存在!"));
}
if (template.IsUseDataSource)
{
return await Task.FromResult(PrintDataResult.Failed("非Json打印接口!"));
}
try
{
var basePath = String.Empty;
var savePath = "wwwroot/PrintTempFile";
var fileName = DateTime.Now.Ticks;
var printFileName = $"{fileName}.frx";
var printFile = Path.Combine(savePath, printFileName);
//写入CRX文件
using (FileStream fs = new FileStream(printFile, FileMode.Create))
{
Byte[] info = new UTF8Encoding(true).GetBytes(template.PrintJsonContent);
fs.Write(info, 0, info.Length);
}
//生成报表
FastReport.Report report = new FastReport.Report();
report.Load(printFile);
var dataSource = report.Dictionary.Connections[0] as JsonDataSourceConnection;
var str = new FastReport.Data.JsonConnection.JsonDataSourceConnectionStringBuilder();
str.Json = req.JsonDataStr;
dataSource.ConnectionString = str.ConnectionString;
report.Prepare();
var printName = string.Empty;
var saveFile = string.Empty;
//var saveFile = Path.Combine(savePath, printName);
if (req.PrintType == "1")
{
printName = $"{fileName}.pdf";
saveFile = Path.Combine(savePath, printName);
PDFExport pdfExport = new PDFExport();
pdfExport.Export(report, saveFile);
}
else if (req.PrintType == "2")
{
printName = $"{fileName}.xlsx";
saveFile = Path.Combine(savePath, printName);
Excel2007Export excelExport = new Excel2007Export();
excelExport.Export(report, saveFile);
}
else if (req.PrintType == "3")
{
printName = $"{fileName}.doc";
saveFile = Path.Combine(savePath, printName);
Word2007Export wordExport = new Word2007Export();
wordExport.Export(report, saveFile);
}
else
{
return await Task.FromResult(PrintDataResult.Failed("非法打印格式!"));
}
return await Task.FromResult(PrintDataResult.OK(printName));
}
catch (Exception e)
{
return await Task.FromResult(PrintDataResult.Failed(e.Message));
}
}
}
catch (Exception ex)
{
//Logger.Warn(ex, "方法: GetOpenJsonPrintInfo");
return await Task.FromResult(PrintDataResult.Failed(ex.Message));
}
}
}
}