![](/img/trans.png)
[英]How to create invincible windows application using c# and windows services
[英]Create HTML Document and place table in it using C# (Windows Application)
我想创建一个HTML文档并使用C#创建表init。 我不想使用ASP或类似的东西。 我想通过使用C#Windows应用程序来做到这一点。
创建的文档不应使用MS Word或不依赖于任何其他应用程序并将其保存到任何文件夹(C:\\)等。它完全独立于任何其他MS产品,可以在任何PC上运行
这样的东西:)
String exportdirectory = "c:";
StreamWriter sw;
sw = File.CreateText(exportDirectory + "filename.html");
sw.WriteLine("<table>");
sw.WriteLine("<tr>");
sw.WriteLine("<td>");
sw.WriteLine("contents of table!");
sw.WriteLine("</td>");
sw.WriteLine("</tr>");
sw.WriteLine("</table>");
sw.Close();
这只是创建一个字符串(可能使用StringBuilder)并附加数据,然后使用StreamWriter保存它。 如果你想要更通用的东西,尝试使用某种stringtemplate,例如http://www.antlr.org/wiki/display/ST/StringTemplate+Documentation ,这可以让你隔离你的“视图”部分应用程序允许某种配置轻松驱动输出生成。
好吧,不清楚为什么你有这样的要求或为什么你提到MS Word ...它没有意义......但你想要的是你的程序来创建一个HTMl文件..(你甚至不需要ASP.NEt为此..它用于Web服务器编程...)
我最好的猜测是你希望根据一些用户要求以HTML格式发出数据。 为此...只需使用HTML扩展创建一个文件,然后开始向其发送HTML特定标签。 很少有人提到过如何做到这一点。 一个快速和肮脏的方式将是这样的事情......
public void CreateFile()
{
StringBuilder sb = new StringBuilder();
sb.Append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd \"> \r\n ");
sb.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
sb.Append("<head>\r\n");
sb.Append("<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />\r\n");
sb.Append("<title>My HTMl Page</title>\r\n");
sb.Append("</head>\r\n");
sb.Append("<body>\r\n");
sb.Append("<table>\r\n");
//If you are emiting some data like a list of items
foreach (var item in Items)
{
sb.Append("<tr>\r\n");
sb.Append("<td>\r\n");
sb.Append(item.ToString());
sb.Append("</td>\r\n");
sb.Append("</tr>\r\n");
}
sb.Append("</table>\r\n");
sb.Append("</body>\r\n");
sb.Append("</html>\r\n");
System.IO.StreamWriter sr = new System.IO.StreamWriter("Your file path .html");
sr.Write(sb.ToString());
}
还有一个System.Web.UI.HTML32TextWriter类专门用于此目的,但你不需要任何ASP,所以......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.