You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
6.1 KiB
221 lines
6.1 KiB
4 years ago
|
using System;
|
||
|
using System.Text;
|
||
|
using System.IO;
|
||
|
using System.Reflection;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Web;
|
||
|
using System.Web.Mvc;
|
||
|
using QMAPP.Common.Web.Models;
|
||
|
|
||
|
namespace QMAPP.Common.Web.Controllers
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 打印控制
|
||
|
/// </summary>
|
||
|
public class PrintController : Controller
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 模板路径
|
||
|
/// </summary>
|
||
|
public static string TemplatePath { get; set; }
|
||
|
//
|
||
|
// GET: /Print/
|
||
|
|
||
|
#region 输出导入数据
|
||
|
|
||
|
public ActionResult PrintHtml(PrintInfoModel model)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
//纸张设置
|
||
|
if (string.IsNullOrEmpty(model.PageName) == false)
|
||
|
{
|
||
|
model.PageWidth = "0";
|
||
|
model.PageHeight = "0";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
model.PageName = "A4";
|
||
|
model.PageWidth = "2100";
|
||
|
model.PageHeight = "2970";
|
||
|
}
|
||
|
|
||
|
|
||
|
if (string.IsNullOrEmpty(model.TaskName) == true)
|
||
|
model.TaskName = "";
|
||
|
|
||
|
model.Top = string.IsNullOrEmpty(model.Top) == true ? "0" : model.Top;
|
||
|
model.Left = string.IsNullOrEmpty(model.Left) == true ? "0" : model.Left;
|
||
|
|
||
|
model.Right = string.IsNullOrEmpty(model.Right) == true ? "0" : model.Right;
|
||
|
model.Bottom = string.IsNullOrEmpty(model.Bottom) == true ? "0" : model.Bottom;
|
||
|
|
||
|
model.BuildPrint();
|
||
|
|
||
|
ViewResult vr = null;
|
||
|
|
||
|
vr = new ViewResult
|
||
|
{
|
||
|
ViewName = "PublicPrint",
|
||
|
MasterName = "PrintMaster",
|
||
|
ViewData = new ViewDataDictionary<PrintInfoModel>(model)
|
||
|
};
|
||
|
|
||
|
return vr;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
return DealException(ex);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 打印异常处理
|
||
|
|
||
|
public ActionResult DealException(Exception ex)
|
||
|
{
|
||
|
QMFrameWork.Log.LogManager.LogHelper.Error(new QMFrameWork.Log.LogInfo
|
||
|
{
|
||
|
ClientIP = "",
|
||
|
UserName = AccountController.GetLoginInfo().UserName,
|
||
|
Info = "打印",
|
||
|
ErrorInfo = ex
|
||
|
});
|
||
|
|
||
|
ViewResult vr = null;
|
||
|
|
||
|
string msg = "";
|
||
|
|
||
|
if (ex.Message.IndexOf("data") >= 0)
|
||
|
{
|
||
|
msg = "获取打印数据时出现异常。";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
msg = ex.Message;
|
||
|
}
|
||
|
|
||
|
vr = new ViewResult
|
||
|
{
|
||
|
ViewName = "MsgOutput",
|
||
|
MasterName = "Site",
|
||
|
ViewData = new ViewDataDictionary<string>(msg)
|
||
|
};
|
||
|
|
||
|
return vr;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 向打印模板绑定信息
|
||
|
|
||
|
public string BingInfoToTemplate<T>(string templateHtml,T info) where T : new()
|
||
|
{
|
||
|
Type type = null;
|
||
|
try
|
||
|
{
|
||
|
type = typeof(T);
|
||
|
PropertyInfo[] pArray = type.GetProperties();
|
||
|
|
||
|
|
||
|
//绑定值
|
||
|
foreach (var item in pArray)
|
||
|
{
|
||
|
string columnName = item.Name;
|
||
|
object value = item.GetValue(info, null);//获取值
|
||
|
|
||
|
if (value != null)
|
||
|
{
|
||
|
templateHtml = templateHtml.Replace("@" + columnName, value.ToString());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
templateHtml = templateHtml.Replace("@" + columnName,"");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return templateHtml;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取打印模板
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取打印模板
|
||
|
/// </summary>
|
||
|
/// <param name="templateName">模板文件名</param>
|
||
|
/// <param name="sectionName">分段名</param>
|
||
|
/// <returns>打印模板</returns>
|
||
|
public string GetPrintTemplate(string templateName, string sectionName)
|
||
|
{
|
||
|
StringBuilder sBuilder = new StringBuilder();
|
||
|
string fileName = TemplatePath + templateName + ".html";
|
||
|
bool isStart = false;
|
||
|
bool isEnd = false;
|
||
|
try
|
||
|
{
|
||
|
|
||
|
//读取模板页内容
|
||
|
FileInfo file = new FileInfo(fileName);
|
||
|
List<string> contentList = new List<string>();
|
||
|
using (StreamReader sr = file.OpenText())
|
||
|
{
|
||
|
string strLog = "";
|
||
|
strLog = sr.ReadLine();
|
||
|
while (strLog != null)
|
||
|
{
|
||
|
contentList.Add(strLog);
|
||
|
strLog = sr.ReadLine();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//读取段落内容
|
||
|
foreach (string c in contentList)
|
||
|
{
|
||
|
if (c.Trim() == "<!--start_" + sectionName + "-->")
|
||
|
{
|
||
|
isStart = true;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (c.Trim() == "<!--end_" + sectionName + "-->")
|
||
|
{
|
||
|
isEnd = true;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (isStart == true)
|
||
|
{
|
||
|
sBuilder.AppendLine(c);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw new Exception("获取打印模板出现异常。");
|
||
|
}
|
||
|
|
||
|
if (sBuilder.ToString() == "" || isStart == false || isEnd == false)
|
||
|
{
|
||
|
throw new Exception("未成功获取打印模板!");
|
||
|
}
|
||
|
|
||
|
return sBuilder.ToString();
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
}
|
||
|
}
|