CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Building an Excel sheet in C# the easy way

My application had to produce a worksheet for Excel. At first I took Visual Studio Tools for Office (aka Visto). Very very nice but it gave me a hard time in registration problems with the Office COM servers used. Fearing a deployment nightmare I took the easy classical road: just create a CSV text file.

A CSV file is quite simple

  • A text line is a row in the sheet. CR/LF is the next row
  • Columns are delimited by ;
  • In a row you only specify the columns you need. A CSV file can be jagged

The good thing is that you're not limited to outputting plain data. The contents of every cell in an Excel sheet can be represented as a plain string. An Excel formula is a plain string which starts with "=". This snippet writes a 3 column sheet-row for every row of data. You will recognize the second column as a formula.

StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false);


DataView dv = new DataView(data.Regel);
 

lc++;
for (int i = 0; i < dv.Count; i++)
{

     DataRowView dr = dv[i];

     sw.Write(dr["Breed"]);
     sw.Write(";");

     if (isTweeDimensionaal)
        sw.Write(string.Format("=(G{0}/1000)*(H{0}/1000)", lc));
     else
        sw.Write(string.Format("=G{0}/1000", lc));
     sw.Write(";");
     sw.Write(dr["Aantal"]);
     sw.Write(";");

     sw.WriteLine();
     lc++;

}
 

The ; character not only separates the columns in a CSV file it is also separates the parameters of an Excel function. These would lead to an unexpected splitting of a column. To prevent this you wrap the contents of a column in double string quotes.

sw.Write(string.Format("\"=MAX((C{0}/1000)*(D{0}/1000);E{0})\"", lc));
 

It takes extra backslashes to escape the quotes, but Excel does calculate the maximum just as intended.

A CSV cannot contain layout, charts, cross-sheet references and the like. But for something as simple as the thing I needed it's hasta la Visto.


Published Oct 20 2005, 04:21 PM by pvanooijen
Filed under:

Comments

Richard Dudley said:

Carlos's ExcelXML writer is a thing of beauty (and free):

http://www.carlosag.net/Tools/ExcelXmlWriter/Default.aspx
# October 20, 2005 4:55 PM

Jon Galloway said:

You can also create an HTML table, set the filename to blah.xls, and Excel will open it as a worksheet. That's handy if you need formatting - both colors and things like numeric formatting. Very useful for web apps, but it works for desktop, too.

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q271572

Still, for building Excel the easy way CSV is the undisputed king.
# October 21, 2005 12:59 AM

Christopher Steen said:

A Look at ASP.NET 2.0's Provider Model [Via: ]
ASP.NET Podcast #23 - Spang, Atlas and
life.... [Via:...
# October 21, 2005 8:29 AM

Jason Haley said:

# October 21, 2005 8:44 AM

Loc Hoang said:

This article saves my life. Thanks.
# June 1, 2006 5:14 PM

Max said:

Umm... Don't CSV files use comas, not semi-colons, to separate columns? Isn't that why CSV stands for "Comma-Separated Values" ?

(I did try with semi-colons, but it did not separate the columns. Commas do, however.)

# March 5, 2008 3:13 PM

pvanooijen said:

Eh yes, the name is Comma separated but the actual delimiter used does depend on the culure. Just like it's 1.5 in US culture and 1,5 in Dutch. (Both stand for one and a half)

# March 6, 2008 5:37 AM

Barba said:

Hi,

Try to use GemBox.Spreadsheet (www.gemboxsoftware.com/GBSpreadsheet.htm) - it is .NET component that supports importing/exporting to XLS, XLSX, CSV and HTML files. There is also a free version at: www.gemboxsoftware.com/GBSpreadsheetFree.htm. Free version comes with 150 rows limit.

Mario

GemBox Software

--

GemBox.Spreadsheet for .NET - Easily read and write Excel (XLS, XLSX or CSV)

or export to HTML files from your .NET apps. See www.gemboxsoftware.com/GBSpreadsheet.htm

--

# June 3, 2008 7:07 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News