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
{
///
/// 打印控制
///
public class PrintController : Controller
{
///
/// 模板路径
///
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(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(msg)
};
return vr;
}
#endregion
#region 向打印模板绑定信息
public string BingInfoToTemplate(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 获取打印模板
///
/// 获取打印模板
///
/// 模板文件名
/// 分段名
/// 打印模板
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 contentList = new List();
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() == "")
{
isStart = true;
continue;
}
if (c.Trim() == "")
{
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
}
}