Oct 30, 2011

Generating an Excel file with C # without having Excel installed.
On many occasions we needed to generate Excel files. The user likes Excel and that means that applications give the ability to save your data as Excel.
Usually we set a reference from our program to Excel and generate the file, with varying success ...
The problem arises when the process is carried on a server, which normally is not installed Office, and no way to convince any human system administrator to install it (and very good it does!).
So what can we do? The solution is more ingenious than technical, but it works perfectly. Excel is able to interpret HTML files as if they were normal Excel file. Since HTML files are plain text files, we just have to generate our html file but give it the extension *. xls Excel.
The following example creates two files, both in html format but with an *. html extension and another extension *. xls. As we can see, the *. xls file is displayed as an Excel file.


The following image shows the extension *. html file viewed in Internet Explorer


The same file with extension *. xls Microsoft Excel seen. The ability to use CSS files allows us to generate very showy.


The following list shows the code C # required to generate this example.


 using System;  
using System.IO;
using System.Text;
 namespace Devjoker  
{

public class Main
{
public static void Main ()
{
GenExcell GenExcell ge = new ();
ge.DoExcell ("nuevo_file.html");
ge.DoExcell ("nuevo_file.xls");
}
}

GenExcell internal class
{
StreamWriter w;


public int DoExcell (string path)
{
FileStream fs = new FileStream (path, FileMode.Create,
FileAccess.ReadWrite);
w = new StreamWriter (fs);
EscribeCabecera ();

for (int i = 0; i <20 ; i + +)
{
EscribeLinea (i);
}

EscribePiePagina ();
w.Close ();
return 0;
}

public void EscribeCabecera ()
{
StringBuilder html = new StringBuilder ();
html.Append ("<! DOCTYPE HTML PUBLIC \ "- / / W3C / / DTD
HTML 4.0 Transitional / / EN \ ">");
html.Append ("<html>");
html.Append ("<head>");
html.Append ("<title > www.devjoker.com </ title> ");
html.Append ("<meta http-equiv = \" Content-Type \ "
content = \ "text / html; charset = UTF-8 \" /> ") ;
html.Append ("</ head>");
html.Append ("<body>");
html.Append ("<p>");
html.Append ("<table>");
html.Append ( "<tr style = \" font-weight:
bold, font-size: 12px, color: white; \ ">");
html.Append ("<td> </ td>
<td bgcolor = \ "Blue \" > Title of the table: </ td> ");
html.Append ("<td bgcolor=\"Blue\"> Iteration: </ td>");
html.Append ("</ tr>");

w . Write (html.ToString ());
}

public void EscribeLinea (int i)
{
bgcolor string = "", fontcolor = "";
if (i% 2 == 0)
{
bgcolor = "bgcolor = \" lightblue \ " ";
fontcolor = "style = \" font-size: 10px, color: white; \ "";
}
w.Write (@ "<tr> <td> </ td> <td {2} cell {3}> Title: {0}
</ td> <td {2} {3}> Cell Value: {1} </ td> </ tr> "
, I. ToString (), i.ToString (), bgcolor, fontcolor) ;
}

public void EscribePiePagina ()
{
StringBuilder html = new StringBuilder ();
html.Append ("</ table>");
html.Append ("</ p>");
html.Append ("</ body>" );
html.Append ("</ html>");
w.Write (html.ToString ());
}

}
 } 

As we see, the program is as simple as opening a file and write the html code you want on it. Logically though the program is written in C # is easily portable to any other language. 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes