1

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

1
  • 4
    So what exactly is stopping you? HTML is a well defined markup language, writing files to disk is pretty straight forward using the System.IO.File class (though there are other ways). Commented Jan 11, 2011 at 17:17

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, a bug! SO's syntax highlighter doesn't acts accordingly to the @ operator with c#!!!
Yeah I just noticed that!! had to change c:\ to c: since @"c:\" didnt work lol
0

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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.