简体   繁体   English

使用C#创建HTML文档并将表放入其中(Windows应用程序)

[英]Create HTML Document and place table in it using C# (Windows Application)

I want to create a HTML document and create table init using C#. 我想创建一个HTML文档并使用C#创建表init。 I don't want to use ASP or any thing like that. 我不想使用ASP或类似的东西。 I want to do this by using C# Windows Application. 我想通过使用C#Windows应用程序来做到这一点。

The created document should not use MS Word or may not depend on any other app and save it to any folder (C:\\) etc. It is totally independent of any other MS product and can run in any PC 创建的文档不应使用MS Word或不依赖于任何其他应用程序并将其保存到任何文件夹(C:\\)等。它完全独立于任何其他MS产品,可以在任何PC上运行

Something like this :) 这样的东西:)

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();

This is just creating a string ( maybe using a StringBuilder ) and appending data, then save it with a StreamWriter. 这只是创建一个字符串(可能使用StringBuilder)并附加数据,然后使用StreamWriter保存它。 If you want something more versatile, try to use some sort of stringtemplate, http://www.antlr.org/wiki/display/ST/StringTemplate+Documentation for example, this could allow you to isolate the "view" portion of your application allowing some sort of configuration to easily drive the output generation. 如果你想要更通用的东西,尝试使用某种stringtemplate,例如http://www.antlr.org/wiki/display/ST/StringTemplate+Documentation ,这可以让你隔离你的“视图”部分应用程序允许某种配置轻松驱动输出生成。

Well its not clear why you have such requirement or why are u mentioning MS Word... it doesn't make sense... But what you want is your programme to Create a HTMl File.. (you dont even need ASP.NEt for that.. its for web server programming...) 好吧,不清楚为什么你有这样的要求或为什么你提到MS Word ...它没有意义......但你想要的是你的程序来创建一个HTMl文件..(你甚至不需要ASP.NEt为此..它用于Web服务器编程...)

My best guess is you want something to emit data in HTML format according to some user requirements. 我最好的猜测是你希望根据一些用户要求以HTML格式发出数据。 For that.. Simply just create a file with HTML Extension and start Emitting HTML specific tags to it. 为此...只需使用HTML扩展创建一个文件,然后开始向其发送HTML特定标签。 Few guys have already mentioned how to do that. 很少有人提到过如何做到这一点。 A quick and Dirty way would be somthing like this... 一个快速和肮脏的方式将是这样的事情......

        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());             
    }

There is also a System.Web.UI.HTML32TextWriter class that is specially ment for this purpose but you didn't wanted anything from ASP, so... 还有一个System.Web.UI.HTML32TextWriter类专门用于此目的,但你不需要任何ASP,所以......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM