1.页面
2 后台
/// <summary>
/// 打印
/// </summary>
/// <param name="sHospitalID"></param>
/// <param name="sDepartmentID"></param>
/// <param name="sExamType"></param>
/// <param name="sYear"></param>
/// <returns></returns>
[AjaxMethod]
public string GetHTML(string sYear,string sHiddenExamType)
{
DataTable dtStatistics = GetMaskulinTotal(sYear, sHiddenExamType, true);
return new SystemManage().GetHTML(dtStatistics, "XXX统计", string.Format("{0}-01", sYear), string.Format("{0}-12", sYear),40);
}
3.核心
/// <summary>
/// 打印方法重载 添加自定义单元格高度
/// </summary>
/// <param name="dtData"></param>
/// <param name="sTitle"></param>
/// <param name="sStartDate"></param>
/// <param name="sEndDate"></param>
/// <returns></returns>
public string GetHTML(DataTable dtData, string sTitle, string sStartDate, string sEndDate,int height)
{
if (dtData == null) return null;
StringBuilder strBuil = new StringBuilder();
strBuil.Append(string.Format(" <table width='96%' border='0' cellspacing='0' cellpadding='0' style='margin-top:0px;' valign='top'><tr><td align='left' style=''>时间范围:{0}~{1}</td><td style='' align='right'>打印日期:{2}</td></tr><tr><td colspan='2' style='font-size:14px;' align='center'>{3}{4} {5}</td></tr></table>",
sStartDate, sEndDate, DateTime.Now.ToTimeString(), UserInfo.HospitalName, UserInfo.DepartmentName, sTitle));
strBuil.Append("<table cellpadding='0' align='center' cellspacing='0' width='96%' style='border-right:solid 1px #000;border-bottom:solid 1px #000;margin-bottom:0px;'><tr>");
//取所有列名
for (int i = 0; i < dtData.Columns.Count; i++)
{
strBuil.Append("<td align='center' style='border-top: solid 1px #000;border-left: solid 1px #000; height:"+height+"px;'>" + dtData.Columns[i].Caption + "</td>");
}
strBuil.Append("</tr>");
//取每行数据
for (int j = 0; j < dtData.Rows.Count; j++)
{
strBuil.Append("<tr>");
for (int i = 0; i < dtData.Columns.Count; i++)
{
string sContent = dtData.Rows[j][i].ToString();
//避免内容为空时打印出来没有边线的情况
if (string.IsNullOrEmpty(sContent))
{
sContent = " ";
}
strBuil.Append("<td align='center' style='border-top: solid 1px #000;border-left: solid 1px #000; height:" + height + "px;'>" + sContent + "</td>");
}
strBuil.Append("</tr>");
}
strBuil.Append("</table>");
return strBuil.ToString();
}
4思路 点击打印按钮 响应前台JS方法 前台JS方法调用后台异步刷新方法 返回打印内容table 赋值给页面上隐藏域.html(x.value) 最后调用JS打印方法Print() 方法 完成操作