打印员工资料卡

本节内容可参考示例文件"CaseStudy\WebViewer\打印资料卡.Table"。

本节的任务是批量打印员工资料卡。

AfterLoad事件代码如下,请注意是如何动态批量生成员工资料卡并插入到网页中的:

'''Async

'
定义样式和网页模板
Dim
html As String = <![CDATA[
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>
员工资料卡</title>
<style>
/*
全局容器样式 */
.container {
width: 600px;
margin: 0 auto;
font-family: Arial, sans-serif;
}
 /*
标题样式 */
h1 {
text-align: center;
margin-bottom: 5px;
}
/*
编号样式 */
.number {
text-align: right;
margin-bottom: 10px;
}
/*
表格整体样式 */
table {
border-collapse: collapse; /*
合并边框,避免重复线 */
width: 100%;
border: 2px solid #000; /*
外框加粗 */
}
/*
单元格通用样式 */
td {
border: 1px solid #000; /*
单元格细边框 */
padding: 8px; /*
内边距,让内容不贴边 */
vertical-align: top; /*
垂直方向顶部对齐 */
}
/*
标签列(如姓名”“部门)样式 */
.label {
background-color: #f0f0f0; /*
浅灰背景区分标签与内容 */
font-weight: bold;
}
/*
备注行样式 */
.remark {
line-height: 1.5; /*
行高增加可读性 */
height: 150px;
}
/*
新增打印样式:禁止资料卡跨页打印 */
@media print {
  /*
单个员工资料卡整体不可拆分到两页 */
  .container {
    break-inside: avoid;
  }
}
</style>
</head>
<body>
<!--
注意网页主体内容是一个标记【body,接下来根据表格模板动态合成内容替换之 -->
body
</body>
</html>
]]>
.Value

'
定义表格模板
Dim
htable As String = <![CDATA[
<div class="container">
<h1>
员工资料卡</h1>
<div class="number">
编号: 2</div>
<table>
<tr>
<td class="label" style="width: 20%;">
姓名</td>
<td style="width: 35%;">
【姓名】</td>
<td class="label" style="width: 20%;">
出生日期</td>
<td style="width: 35%;">
【出生日期】</td>
<td rowspan="4" style="width: 20%; text-align: center; vertical-align: middle;">
<img src="custom://Attachments/
【照片】" alt="照片" style="width: 150px; height: auto;">
</td>
</tr>
<tr>
<td class="label">
部门</td>
<td>
【部门】</td>
<td class="label">
雇用日期</td>
<td>
【雇用日期】</td>
</tr>
<tr>
<td class="label">
性别</td>
<td>
【性别】</td>
<td class="label">
职务</td>
<td>
【职务】</td>
</tr>
<tr>
<td class="label">
地址</td>
<td colspan="2">
【地址】</td>
</tr>
<tr>
<td class="label">
家庭电话</td>
<td>
【家庭电话】</td>
<td class="label">
办公电话</td>
<td>
【办公电话】</td>
</tr>
<tr>
<td colspan="5" class="remark">

【备注】

</td>
</tr>
</table>
</div>
]]>
.Value

'批量生成员工资料卡
Dim
wv As WebViewer = e.Form.Controls("WebViewer1").WebViewer
Await
wv.EnsureCoreWebView2Async(Nothing)
Dim
sb As New StringBuilder()
For
Each r As Row In Tables("员工").Rows
   
Dim tbl As String = htable.Replace("【姓名】", r("姓名")).Replace("【部门】", r("部门"))
    tbl = tbl.Replace(
"【职务】", r("职务")).Replace("【性别】", r("性别")).Replace("【出生日期】", format(r("出生日期"), "yyyy-M-d"))
    tbl = tbl.Replace(
"【雇佣日期】", format(r("雇佣日期"), "yyyy-M-d")).Replace("【地址】", r("地址")).Replace("【家庭电话】", r("家庭电话"))
    tbl = tbl.Replace(
"【办公电话】", r("办公电话")).Replace("【照片】", r("照片")).Replace("【备注】", r("备注"))
    sb.AppendLine(tbl)

Next

wv.NavigateToString(html.Replace(
"body", sb.ToString))


本页地址:http://www.foxtable.com/webhelp/topics/7006.htm