Today we will look at my XLS Creator Class. It is very similar to my CSV Creator class discussed in an earlier post (BITE SIZE C# – CSV FILE CREATOR), except that it creates a well formed XLS file instead of a CSV file.
It is a static class contained in the Core.XLS namespace and contains 1 static method ExportToExcel. Like the CSV Creator class it uses a generic list of objects to construct a XLS file. The method also takes 2 string values xlsNameWithExt and sheetName.
xlsNameWithExt which is used to define the name and location to save the created XLS file and sheetName which is used to set the sheet name the data will be inserted on.
Here is the code:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; namespace Core.XLS { public static class XlsCreator { public static void ExportToExcel<T>(List<T> list, string xlsNameWithExt, string sheetName) { var columnCount = 0; var StartTime = DateTime.Now; var rowData = new StringBuilder(); var properties = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance); rowData.Append("<Row ss:StyleID=\"s62\">"); foreach (var p in properties) { if (p.Name.ToUpper() == "ENTITYSTATE" || p.Name.ToUpper() == "ENTITYKEY") continue; if (p.PropertyType.Name != "EntityCollection`1" && p.PropertyType.Name != "EntityReference`1") { var type = "String"; columnCount++; rowData.Append("<Cell><Data ss:Type=\"" + type + "\">" + p.Name + "</Data></Cell>"); } else break; } rowData.Append("</Row>"); foreach (var item in list) { rowData.Append("<Row>"); for (var x = 0; x < columnCount; x++) //each (PropertyInfo p in properties) { var o = properties[x].GetValue(item, null); var value = o == null ? "" : o.ToString(); rowData.Append("<Cell><Data ss:Type=\"String\">" + value + "</Data></Cell>"); } rowData.Append("</Row>"); } var sheet = @"<?xml version=""1.0""?> <?mso-application progid=""Excel.Sheet""?> <Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel"" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"" xmlns:html=""http://www.w3.org/TR/REC-html40""> <DocumentProperties xmlns=""urn:schemas-microsoft-com:office:office""> <Author>MSADMIN</Author> <LastAuthor>MSADMIN</LastAuthor> <Created>2011-07-12T23:40:11Z</Created> <Company>Microsoft</Company> <Version>12.00</Version> </DocumentProperties> <ExcelWorkbook xmlns=""urn:schemas-microsoft-com:office:excel""> <WindowHeight>6600</WindowHeight> <WindowWidth>12255</WindowWidth> <WindowTopX>0</WindowTopX> <WindowTopY>60</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID=""Default"" ss:Name=""Normal""> <Alignment ss:Vertical=""Bottom""/> <Borders/> <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID=""s62""> <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000"" ss:Bold=""1""/> </Style> </Styles> <Worksheet ss:Name=""" + sheetName + @"""> <Table ss:ExpandedColumnCount=""" + (properties.Count() + 1) + @""" ss:ExpandedRowCount=""" + (list.Count() + 1) + @""" x:FullColumns=""1"" x:FullRows=""1"" ss:DefaultRowHeight=""15""> " + rowData + @" </Table> <WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel""> <PageSetup> <Header x:Margin=""0.3""/> <Footer x:Margin=""0.3""/> <PageMargins x:Bottom=""0.75"" x:Left=""0.7"" x:Right=""0.7"" x:Top=""0.75""/> </PageSetup> <Print> <ValidPrinterInfo/> <HorizontalResolution>300</HorizontalResolution> <VerticalResolution>300</VerticalResolution> </Print> <Selected/> <Panes> <Pane> <Number>3</Number> <ActiveCol>2</ActiveCol> </Pane> </Panes> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> <Worksheet ss:Name=""Sheet2""> <Table ss:ExpandedColumnCount=""1"" ss:ExpandedRowCount=""1"" x:FullColumns=""1"" x:FullRows=""1"" ss:DefaultRowHeight=""15""> </Table> <WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel""> <PageSetup> <Header x:Margin=""0.3""/> <Footer x:Margin=""0.3""/> <PageMargins x:Bottom=""0.75"" x:Left=""0.7"" x:Right=""0.7"" x:Top=""0.75""/> </PageSetup> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> <Worksheet ss:Name=""Sheet3""> <Table ss:ExpandedColumnCount=""1"" ss:ExpandedRowCount=""1"" x:FullColumns=""1"" x:FullRows=""1"" ss:DefaultRowHeight=""15""> </Table> <WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel""> <PageSetup> <Header x:Margin=""0.3""/> <Footer x:Margin=""0.3""/> <PageMargins x:Bottom=""0.75"" x:Left=""0.7"" x:Right=""0.7"" x:Top=""0.75""/> </PageSetup> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> </Workbook>"; using (var sw = new StreamWriter(xlsNameWithExt)) { sw.Write(sheet); } } } }
Hi James,
I’m finding it hard to read the code in your posts because you just dump the text. Could you maybe use a plugin or wrap it in a source code tag ie: .
Another option would be to add a copy on your github account…
LikeLike
Hi Vaughan,
Thanks for the feedback. Unfortunately WordPress has discontinued support for plugins on sites they host due to security concerns. Also the sourcecode tag is giving me undesirable results with my selected theme. For now I have modified my site css for the <code> tag, to hopefully make it a bit more readable until I find a more permanent solution.
LikeLike
Okay cool. Thanks for the effort still. Btw if you wanted to host it yourself it’s not too pricey. Your blog got me to eventually get around to hosting my own one. R39 a month for webafrica hosting (there are lots of options) and $10 a year for a domain.
LikeLike