订舱报表打印测试

booking_auth_dev
wanghaomei 2 years ago
parent c9c65fd8be
commit 346e49b301

@ -27,16 +27,15 @@ namespace Myshipping.Report.Controllers
/// <summary>
/// 生成订舱报表结果
/// </summary>
/// <param name="templateFile">模板文件路径</param>
/// <param name="bookingId">订舱ID</param>
/// <param name="type">类型1pdf、2xlsx</param>
/// <returns></returns>
[HttpGet]
public ActionResult BookingReport(string templateFile, long bookingId, int type = 1)
[HttpPost]
public ActionResult BookingReport(long bookingId, int type = 1)
{
var resp = new RespCommonData();
if (System.IO.File.Exists(templateFile))
if (Request.Files.Count > 0)
{
var savePath = ConfigurationManager.AppSettings["ReportFileSavePath"];
if (string.IsNullOrEmpty(savePath)) //未配置保存路径时,使用当前目录
@ -49,9 +48,20 @@ namespace Myshipping.Report.Controllers
Directory.CreateDirectory(savePath);
}
var fileExt = Path.GetExtension(Request.Files[0].FileName).ToLower();
if (fileExt != ".frx")
{
resp.Success = false;
resp.Message = $"文件类型不正确:{fileExt}";
return Json(resp);
}
var tmpFile = Path.Combine(savePath, $"{Guid.NewGuid()}{fileExt}");
Request.Files[0].SaveAs(tmpFile);
//生成报表
FastReport.Report report = new FastReport.Report();
report.Load(templateFile);
report.Load(tmpFile);
foreach (FastReport.Data.JsonConnection.JsonDataSourceConnection conn in report.Dictionary.Connections)
{
@ -65,21 +75,23 @@ namespace Myshipping.Report.Controllers
if (type == 1) //pdf
{
var saveFile = Path.Combine(savePath, $"{bookingId}_{DateTime.Now.Ticks}.pdf");
var fileName = $"{bookingId}_{DateTime.Now.Ticks}.pdf";
var saveFile = Path.Combine(savePath, fileName);
PDFExport pdfExport = new PDFExport();
pdfExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = saveFile;
resp.Data = fileName;
}
else if (type == 2) //
{
var saveFile = Path.Combine(savePath, $"{bookingId}_{DateTime.Now.Ticks}.xlsx");
var fileName = $"{bookingId}_{DateTime.Now.Ticks}.xlsx";
var saveFile = Path.Combine(savePath, fileName);
Excel2007Export excelExport = new Excel2007Export();
excelExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = saveFile;
resp.Data = fileName;
}
else
{
@ -87,15 +99,38 @@ namespace Myshipping.Report.Controllers
resp.Message = "类型参数不正确";
}
return Json(resp, JsonRequestBehavior.AllowGet);
return Json(resp);
}
else
{
resp.Success = false;
resp.Message = "未找到模板文件";
resp.Message = "未上传模板文件";
}
return Json(resp);
}
/// <summary>
/// 根据文件名获取文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[HttpGet]
public ActionResult GetFile(string fileName)
{
var basePath = ConfigurationManager.AppSettings["ReportFileSavePath"];
if (string.IsNullOrEmpty(basePath)) //未配置保存路径时,使用当前目录
{
basePath = Server.MapPath("~/ReportFiles");
}
var saveFile = Path.Combine(basePath, fileName);
if (System.IO.File.Exists(saveFile))
{
return File(saveFile, "application/octet-stream", HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8")));
}
return Json(resp, JsonRequestBehavior.AllowGet);
return HttpNotFound();
}

Loading…
Cancel
Save