Tag: xml stylesheet
Generating Excel (XmlSpreadSheet) in C# Part 2.
by mysticslayer on Nov.11, 2007, under Programming
In part 1 we started to explain a couple of things in our Excel xml spreadsheet. So let’s start how to explain it in code.
First of all we need a StringBuilder. With a StringBuilder we can append all the data we want. Let’s build a couple if simple functions. Everything is written in C#, because it’s a common written language in .NET. So if you would like to write in VB.NET you have to convert it to VB.Net.
First we start with the header of our Xml Spreadsheet:
-
// We need a newline after each rule we’ve made. So this will make it in our StringBuilder.
-
private string _Newline = “\n“;
-
-
// Returns the _NewLine
-
protected string NewLine
-
{
-
get
-
{
-
return _NewLine;
-
}
-
}
-
protected string StartHeader
-
{
-
get
-
{
-
StringBuilder sb = new StringBuilder();
-
-
sb.Append(“< ?xml version=\”1.0\”?>” + NewLine);
-
sb.Append(“< ?mso-application progid=\”Excel.Sheet\”?>” + NewLine);
-
sb.Append(“<workbook xmlns=\”urn:schemas-microsoft-com:office:spreadsheet\”“ + NewLine);
-
sb.Append(“xmlns:o=\”urn:schemas-microsoft-com:office:office\”“ + NewLine);
-
sb.Append(“xmlns:x=\”urn:schemas-microsoft-com:office:excel\”“ + NewLine);
-
sb.Append(“xmlns:ss=\”urn:schemas-microsoft-com:office:spreadsheet\”“ + NewLine);
-
sb.Append(“xmlns:html=\”http://www.w3.org/TR/REC-html40\”>” + NewLine);
-
-
return sb.ToString();
-
}
-
}
-
-
</workbook>
So we have written our first part of our spreadsheet header. Let’s see what we’ve got now. Ah yes. Now our DocumentProperties we can write in our stringbuilder.
-
protected string DocumentProperties
-
{
-
get
-
{
-
StringBuilder sb = new StringBuilder();
-
-
sb.Append(“<documentproperties xmlns=\”urn:schemas-microsoft-com:office:office\”>” + NewLine);
-
sb.Append(“<author>Author</author>” + NewLine);
-
sb.Append(“<lastauthor>LastAuthor</lastauthor>” + NewLine);
-
sb.Append(“<created>” + DateTime.Now + “</created>” + NewLine);
-
sb.Append(“<version>12.00</version>” + NewLine);
-
sb.Append(“</documentproperties>” + NewLine);
-
-
return sb.ToString();
-
}
-
}
So we have written now our DocumentProperties. Now we can write our ExcelWorkbook properties. There are a couple of things important. Don’t forget to put ProtectStructure and ProtectWindows in your ExcelWorkbook properties. These are important things we need to use.
-
protected string ExcelWorkbook
-
{
-
get
-
{
-
StringBuilder sb = new StringBuilder();
-
-
sb.Append(“<excelworkbook xmlns=\”urn:schemas-microsoft-com:office:excel\”>” + NewLine);
-
sb.Append(“<windowheight>8130</windowheight>” + NewLine);
-
sb.Append(“<windowwidth>18015</windowwidth>” + NewLine);
-
sb.Append(“<windowtopx>360</windowtopx>” + NewLine);
-
sb.Append(“<windowtopy>75</windowtopy>” + NewLine);
-
sb.Append(“<calculation>ManualCalculation</calculation>” + NewLine);
-
sb.Append(“<donotcalculatebeforesave />” + NewLine);
-
sb.Append(“<protectstructure>False</protectstructure>” + NewLine);
-
sb.Append(“<protectwindows>False</protectwindows>” + NewLine);
-
sb.Append(“</excelworkbook>” + NewLine);
-
-
return sb.ToString();
-
}
-
}
We have done our first part of our document. Now we can see allready that we have done alot of work to write our header for our document. Let’s start with our Styles. You can add styles in a couple of manners to your document. You can write an external document that we can add to our project. Or you can add this by code. Well like you write a stylesheet for your webpage, I wrote a styles.config to add to our project.
You can add this in by the following code part.
-
-
private string Styles(string filename)
-
{
-
System.IO.StreamReader sr;
-
-
string sLine;
-
string sText = string.Empty;
-
-
sr = System.IO.File.OpenText(filename);
-
sLine = sr.ReadLine();
-
-
sText = sLine;
-
-
while(sLine != null)
-
{
-
sLine = sr.ReadLine();
-
sText += sLine + “\n“;
-
-
}
-
-
sr.Close();
-
-
return sText;
-
}
This was the part for reading our styles.config into a StreamReader and return it as string. Well we have done the most of things now. Let’s start with the main part. The main part is making the ExcelWorksheet and the table with data. In Html you can write a table like this:
-
<table>
-
<tr><td></td></tr><tr>
-
</tr><tr><td></td></tr><tr>
-
</tr></table>
Well the xml spreadsheet is almost the same. Let’s see how to make a simple template of it.
-
<table>
-
<row>
-
<cell><data></data></cell>
-
<cell><data></data></cell>
-
</row>
-
</table>
There are not many differences between html and xml spreadsheet. But it’s important to know that you don’t forget any closing tags. In particular big documents it’s a problem to find the problem. Well let’s start with some simple data to put it in our document.
-
protected string _data
-
{
-
get
-
{
-
StringBuilder sb = new StringBuilder();
-
-
sb.Append(“<worksheet ss:Name=\”Sheet1\”>”);
-
sb.Append(“<table ss:ExpandedColumnCount=\”12\” ss:ExpandedRowCount=\”1\” x:FullColumns=\”1\” x:FullRows=\”1\” ss:DefaultRowHeight=\”15\”>”);
-
sb.Append(“<column ss:Index=\”2\” ss:StyleID=\”s62\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<column ss:StyleID=\”s63\” ss:Width=\”53.25\”/>”);
-
sb.Append(“<column ss:StyleID=\”s65\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<column ss:StyleID=\”s66\” ss:AutoFitWidth=\”\” ss:Width=\”67.5\”/>”);
-
sb.Append(“<column ss:StyleID=\”s67\” ss:Width=\”54.75\”/>”);
-
sb.Append(“<column ss:StyleID=\”s68\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<column ss:StyleID=\”s69\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<column ss:StyleID=\”s70\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<column ss:StyleID=\”s71\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<column ss:StyleID=\”s72\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<column ss:StyleID=\”s73\” ss:AutoFitWidth=\”\”/>”);
-
sb.Append(“<row>”);
-
sb.Append(“<cell><data ss:Type=\”String\”>General</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”Number\”>0</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”Number\”>12345</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”String\”>€</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”DateTime\”>2007-03-14T00:00:00.000</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”DateTime\”>1899-12-31T13:30:00.000</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”Number\”>1</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”Number\”>0.33333333333333331</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”Number\”>0.33333333333333331</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”String\”>text</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”String\”>Breda</data></cell>”);
-
sb.Append(“<cell><data ss:Type=\”Number\”>12345</data></cell>”);
-
sb.Append(“</row>”);
-
sb.Append(“</table>”);
-
-
return sb.ToString();
-
}
-
}
-
</worksheet>
On part 3 we’re going further. Cause the fact is that WordPress doesn’t allow me to write alot of text in one article…