开发者

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#. I don't want to use ASP or any thing like that. I want to do this by using C# Windows Application.

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


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. 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.


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...)

My best guess is you want something to emit data in HTML format according to some user requirements. For that.. Simply just create a file with HTML Extension and start Emitting HTML specific tags to it. 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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜