一、新建一个c#窗体项目,命名为VT
二、首先制作一个html模板如下,命名为Untitled-2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p>
<table style="width:50%;" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
<tbody>
<tr>
<td>
序号
</td>
<td>
姓名
</td>
<td>
成绩
</td>
</tr>
<vt:foreach from="$Student" item="student" index="i">
<tr>
<td>
{$:i}号
</td>
<td>
{$:student.name}
</td>
<td>
{$:student.score}分
</td>
</tr>
</vt:foreach>
</tbody>
</table>
<br />
</p>
</body>
</html>
将其放在项目debug中
三、添加一个button,c#代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VTemplate.Engine;
using VTemplate.WebTester;
namespace VT
{
public partial class Form1 : Form
{
List<Student> listStudent = new List<Student>();
public Form1()
{
InitializeComponent();
Student student = new Student();
student.name = "张三";
student.score = 90;
listStudent.Add(student);
Student student1 = new Student();
student1.name = "李四";
student1.score = 78;
listStudent.Add(student1);
Student student2 = new Student();
student2.name = "张三";
student2.score = 89;
listStudent.Add(student2);
listStudent.Add(student2);
listStudent.Add(student2);
listStudent.Add(student2);
listStudent.Add(student2);
listStudent.Add(student2);
listStudent.Add(student2);
//document.Render(context.Response.Output)
}
private void button1_Click(object sender, EventArgs e)
{
TemplateDocument document = new TemplateDocument(Application.StartupPath + "\\Untitled-2.html", Encoding.UTF8, TemplateDocumentConfig.Default);
document.Variables.SetValue("Student", listStudent);
document.RenderTo(Application.StartupPath + "\\Untitled-2 - 副本.html", Encoding.UTF8);
}
}
}
其中的student类如下定义
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VT
{
class Student
{
public string name { get; set; }
public double score { get; set; }
}
}
四、效果如下
小结:这里推荐一个html在线编译器,个人感觉很好用
http://www.kindsoft.net/demo.php