├── OpenXmlUtils ├── packages.config ├── app.Debug.config ├── app.config ├── OpenXmlUtils.nuspec ├── SpreadsheetField Types │ ├── DecimalNumberField.cs │ └── HyperlinkField.cs ├── SpreadsheetField.cs ├── NumberCell.cs ├── TextCell.cs ├── FormulaCell.cs ├── DateCell.cs ├── Properties │ └── AssemblyInfo.cs ├── SheetDefinition.cs ├── OpenXmlUtils.csproj ├── CustomStylesheet.cs └── Spreadsheet.cs ├── OpenXmlUtils.Tests ├── packages.config ├── app.Debug.config ├── app.Release.config ├── app.config ├── Properties │ └── AssemblyInfo.cs ├── OpenXmlUtils.Tests.csproj └── SpreadsheetUnitTest.cs ├── packages └── repositories.config ├── OpenXmlUtils.sln ├── .gitignore ├── README.md └── LICENSE /OpenXmlUtils/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /OpenXmlUtils.Tests/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /packages/repositories.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /OpenXmlUtils/app.Debug.config: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /OpenXmlUtils.Tests/app.Debug.config: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /OpenXmlUtils.Tests/app.Release.config: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /OpenXmlUtils/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /OpenXmlUtils.Tests/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /OpenXmlUtils/OpenXmlUtils.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $id$ 5 | $version$ 6 | $title$ 7 | $author$ 8 | $author$ 9 | false 10 | https://github.com/geoplex/openxmlutils 11 | $description$ 12 | Copyright 2014 13 | OpenXml DocumentFormat Spreadsheet 14 | 15 | -------------------------------------------------------------------------------- /OpenXmlUtils/SpreadsheetField Types/DecimalNumberField.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "SpreadsheetField.cs" 4 | // Purpose: "Represents a field in a spreadsheet" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | namespace OpenXmlUtils 24 | { 25 | public class DecimalNumberField : SpreadsheetField 26 | { 27 | public int DecimalPlaces { get; set; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /OpenXmlUtils/SpreadsheetField Types/HyperlinkField.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "SpreadsheetField.cs" 4 | // Purpose: "Represents a field in a spreadsheet" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | namespace OpenXmlUtils 24 | { 25 | public class HyperlinkField : SpreadsheetField 26 | { 27 | public string DisplayFieldName { get; set; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /OpenXmlUtils/SpreadsheetField.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "SpreadsheetField.cs" 4 | // Purpose: "Represents a field in a spreadsheet" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | namespace OpenXmlUtils 24 | { 25 | public class SpreadsheetField 26 | { 27 | public string Title { get; set; } 28 | public string FieldName { get; set; } 29 | public bool IgnoreFromTotals { get; set; } 30 | public bool CountNoneNullRowsForTotal { get; set; } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /OpenXmlUtils/NumberCell.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "NumberCell.cs" 4 | // Purpose: "A simple class for number cells" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | using DocumentFormat.OpenXml.Spreadsheet; 24 | 25 | namespace OpenXmlUtils 26 | { 27 | public class NumberCell : Cell 28 | { 29 | public NumberCell(string header, string text, int index) 30 | { 31 | DataType = CellValues.Number; 32 | CellReference = header + index; 33 | CellValue = new CellValue(text); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /OpenXmlUtils/TextCell.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "TextCell.cs" 4 | // Purpose: "A simple class for text cells" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | using DocumentFormat.OpenXml.Spreadsheet; 24 | 25 | namespace OpenXmlUtils 26 | { 27 | public class TextCell : Cell 28 | { 29 | public TextCell(string header, string text, int index) 30 | { 31 | DataType = CellValues.InlineString; 32 | CellReference = header + index; 33 | InlineString = new InlineString {Text = new Text {Text = text}}; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /OpenXmlUtils/FormulaCell.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "FormulaCell.cs" 4 | // Purpose: "A simple class for formula cells" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | using DocumentFormat.OpenXml.Spreadsheet; 24 | 25 | namespace OpenXmlUtils 26 | { 27 | public class FormulaCell : Cell 28 | { 29 | public FormulaCell(string header, string text, int index) 30 | { 31 | CellFormula = new CellFormula {CalculateCell = true, Text = text}; 32 | DataType = CellValues.Number; 33 | CellReference = header + index; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /OpenXmlUtils/DateCell.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "DateCell.cs" 4 | // Purpose: "A simple class for date cells" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | using System; 24 | using DocumentFormat.OpenXml.Spreadsheet; 25 | 26 | namespace OpenXmlUtils 27 | { 28 | public class DateCell : Cell 29 | { 30 | public DateCell(string header, DateTime dateTime, int index) 31 | { 32 | DataType = CellValues.Date; 33 | CellReference = header + index; 34 | StyleIndex = (UInt32)CustomStylesheet.CustomCellFormats.DefaultDate; 35 | CellValue = new CellValue(dateTime.ToString("yyyy-MM-dd")); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /OpenXmlUtils.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("OpenXmlUtils.Tests")] 9 | [assembly: AssemblyDescription("A test project for OpenXmlUtils assembly.")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("David McDonald")] 12 | [assembly: AssemblyProduct("OpenXmlUtils")] 13 | [assembly: AssemblyCopyright("Copyright 2014 Geoplex")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("97082b8c-3783-47dc-8aef-e31009c612d1")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /OpenXmlUtils/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("OpenXmlUtils")] 8 | [assembly: AssemblyDescription("Utility classes for the DocumentFormat.OpenXml library.\nCreate a spreadsheet using a collection of objects.\n\nhttps://github.com/geoplex/openxmlutils")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("David McDonald")] 11 | [assembly: AssemblyProduct("OpenXmlUtils")] 12 | [assembly: AssemblyCopyright("Copyright 2014 Geoplex")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("1805f575-682e-4924-8789-d51548134965")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.7")] 35 | [assembly: AssemblyFileVersion("1.0.0.7")] 36 | -------------------------------------------------------------------------------- /OpenXmlUtils/SheetDefinition.cs: -------------------------------------------------------------------------------- 1 | 2 | #region File Information 3 | // 4 | // File: "SheetDefinition.cs" 5 | // Purpose: "Defines a single sheet (or tab) in a xlxs spreadsheet." 6 | // Author: "Geoplex" 7 | // 8 | #endregion 9 | 10 | #region (c) Copyright 2014 Geoplex 11 | // 12 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 13 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 14 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 15 | // 16 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 17 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 18 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 19 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 20 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21 | // 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | 26 | namespace OpenXmlUtils 27 | { 28 | public class SheetDefinition 29 | { 30 | /// 31 | /// Name of the sheet (shown in the tab) 32 | /// 33 | public string Name { get; set; } 34 | 35 | /// 36 | /// Title of the sheet 37 | /// 38 | public string Title { get; set; } 39 | 40 | /// 41 | /// Subtitle of the sheet 42 | /// 43 | public string SubTitle { get; set; } 44 | 45 | /// 46 | /// Objects to display in the sheet 47 | /// 48 | public IList Objects { get; set; } 49 | 50 | /// 51 | /// Field names to extract from the objects and use as header names 52 | /// 53 | public List Fields { get; set; } 54 | 55 | /// 56 | /// Whether or not to include a row of calculated totals to the table 57 | /// 58 | public bool IncludeTotalsRow { get; set; } 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /OpenXmlUtils.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30723.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E36D29A7-0590-4BF2-842D-0BC64265590F}" 7 | ProjectSection(SolutionItems) = preProject 8 | README.md = README.md 9 | EndProjectSection 10 | EndProject 11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenXmlUtils", "OpenXmlUtils\OpenXmlUtils.csproj", "{5B803DEF-1248-4160-800C-F90287A807B2}" 12 | EndProject 13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenXmlUtils.Tests", "OpenXmlUtils.Tests\OpenXmlUtils.Tests.csproj", "{E5DF0F1A-E559-4355-8A9E-3F6B96050F53}" 14 | EndProject 15 | Global 16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 17 | Debug|Any CPU = Debug|Any CPU 18 | Debug|x64 = Debug|x64 19 | Release|Any CPU = Release|Any CPU 20 | Release|x64 = Release|x64 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {5B803DEF-1248-4160-800C-F90287A807B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {5B803DEF-1248-4160-800C-F90287A807B2}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {5B803DEF-1248-4160-800C-F90287A807B2}.Debug|x64.ActiveCfg = Debug|x64 26 | {5B803DEF-1248-4160-800C-F90287A807B2}.Debug|x64.Build.0 = Debug|x64 27 | {5B803DEF-1248-4160-800C-F90287A807B2}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {5B803DEF-1248-4160-800C-F90287A807B2}.Release|Any CPU.Build.0 = Release|Any CPU 29 | {5B803DEF-1248-4160-800C-F90287A807B2}.Release|x64.ActiveCfg = Release|x64 30 | {5B803DEF-1248-4160-800C-F90287A807B2}.Release|x64.Build.0 = Release|x64 31 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Debug|x64.ActiveCfg = Debug|x64 34 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Debug|x64.Build.0 = Debug|x64 35 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Release|Any CPU.ActiveCfg = Release|Any CPU 36 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Release|Any CPU.Build.0 = Release|Any CPU 37 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Release|x64.ActiveCfg = Release|x64 38 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53}.Release|x64.Build.0 = Release|x64 39 | EndGlobalSection 40 | GlobalSection(SolutionProperties) = preSolution 41 | HideSolutionNode = FALSE 42 | EndGlobalSection 43 | GlobalSection(NestedProjects) = preSolution 44 | {5B803DEF-1248-4160-800C-F90287A807B2} = {E36D29A7-0590-4BF2-842D-0BC64265590F} 45 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53} = {E36D29A7-0590-4BF2-842D-0BC64265590F} 46 | EndGlobalSection 47 | EndGlobal 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # MSTest test Results 20 | [Tt]est[Rr]esult*/ 21 | [Bb]uild[Ll]og.* 22 | 23 | #NUNIT 24 | *.VisualState.xml 25 | TestResult.xml 26 | 27 | *_i.c 28 | *_p.c 29 | *_i.h 30 | *.ilk 31 | *.meta 32 | *.obj 33 | *.pch 34 | *.pdb 35 | *.pgc 36 | *.pgd 37 | *.rsp 38 | *.sbr 39 | *.tlb 40 | *.tli 41 | *.tlh 42 | *.tmp 43 | *.tmp_proj 44 | *.log 45 | *.vspscc 46 | *.vssscc 47 | .builds 48 | *.pidb 49 | *.svclog 50 | *.scc 51 | 52 | # Chutzpah Test files 53 | _Chutzpah* 54 | 55 | # Visual C++ cache files 56 | ipch/ 57 | *.aps 58 | *.ncb 59 | *.opensdf 60 | *.sdf 61 | *.cachefile 62 | 63 | # Visual Studio profiler 64 | *.psess 65 | *.vsp 66 | *.vspx 67 | 68 | # TFS 2012 Local Workspace 69 | $tf/ 70 | 71 | # Guidance Automation Toolkit 72 | *.gpState 73 | 74 | # ReSharper is a .NET coding add-in 75 | _ReSharper*/ 76 | *.[Rr]e[Ss]harper 77 | *.DotSettings.user 78 | 79 | # JustCode is a .NET coding addin-in 80 | .JustCode 81 | 82 | # TeamCity is a build add-in 83 | _TeamCity* 84 | 85 | # DotCover is a Code Coverage Tool 86 | *.dotCover 87 | 88 | # NCrunch 89 | *.ncrunch* 90 | _NCrunch_* 91 | .*crunch*.local.xml 92 | 93 | # MightyMoose 94 | *.mm.* 95 | AutoTest.Net/ 96 | 97 | # Installshield output folder 98 | [Ee]xpress/ 99 | 100 | # DocProject is a documentation generator add-in 101 | DocProject/buildhelp/ 102 | DocProject/Help/*.HxT 103 | DocProject/Help/*.HxC 104 | DocProject/Help/*.hhc 105 | DocProject/Help/*.hhk 106 | DocProject/Help/*.hhp 107 | DocProject/Help/Html2 108 | DocProject/Help/html 109 | 110 | # Click-Once directory 111 | publish/ 112 | 113 | # Publish Web Output 114 | *.[Pp]ublish.xml 115 | *.azurePubxml 116 | 117 | # NuGet Packages Directory 118 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 119 | packages/* 120 | ## TODO: If the tool you use requires repositories.config, also uncomment the next line 121 | !packages/repositories.config 122 | 123 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 124 | # This line needs to be after the ignore of the build folder (and the packages folder if the line above has been uncommented) 125 | !packages/build/ 126 | 127 | # Windows Azure Build Output 128 | csx/ 129 | *.build.csdef 130 | 131 | # Windows Store app package directory 132 | AppPackages/ 133 | 134 | # Others 135 | *.Cache 136 | ClientBin/ 137 | [Ss]tyle[Cc]op.* 138 | ~$* 139 | *~ 140 | *.dbmdl 141 | *.dbproj.schemaview 142 | *.pfx 143 | *.publishsettings 144 | node_modules/ 145 | 146 | # RIA/Silverlight projects 147 | Generated_Code/ 148 | 149 | # Backup & report files from converting an old project file to a newer 150 | # Visual Studio version. Backup files are not needed, because we have git ;-) 151 | _UpgradeReport_Files/ 152 | Backup*/ 153 | UpgradeLog*.XML 154 | UpgradeLog*.htm 155 | 156 | # SQL Server files 157 | App_Data/*.mdf 158 | App_Data/*.ldf 159 | 160 | # Business Intelligence projects 161 | *.rdl.data 162 | *.bim.layout 163 | *.bim_*.settings 164 | 165 | # Microsoft Fakes 166 | FakesAssemblies/ 167 | 168 | # ========================= 169 | # Windows detritus 170 | # ========================= 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Nuget files 183 | *.nupkg -------------------------------------------------------------------------------- /OpenXmlUtils/OpenXmlUtils.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {5B803DEF-1248-4160-800C-F90287A807B2} 8 | Library 9 | Properties 10 | OpenXmlUtils 11 | OpenXmlUtils 12 | v4.5 13 | 512 14 | ..\ 15 | true 16 | 17 | 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | TRACE;DEBUG;LOCALHOST 23 | prompt 24 | 4 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE;LOCALHOST 31 | prompt 32 | 4 33 | 34 | 35 | bin\DEV\ 36 | 37 | 38 | bin\PROD\ 39 | 40 | 41 | bin\UAT\ 42 | 43 | 44 | x64 45 | bin\x64\Debug\ 46 | 47 | 48 | x64 49 | bin\x64\Release\ 50 | 51 | 52 | x64 53 | bin\x64\DEV\ 54 | 55 | 56 | x64 57 | bin\x64\PROD\ 58 | 59 | 60 | x64 61 | bin\x64\UAT\ 62 | 63 | 64 | bin\HOTFIX\ 65 | AnyCPU 66 | MinimumRecommendedRules.ruleset 67 | HOTFIX 68 | 69 | 70 | bin\x64\HOTFIX\ 71 | x64 72 | MinimumRecommendedRules.ruleset 73 | HOTFIX 74 | 75 | 76 | $([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\..\packages\SlowCheetah.2.5.12\tools\)) 77 | true 78 | $([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\Properties\SlowCheetah\SlowCheetah.Transforms.targets )) 79 | $(SlowCheetah_NuGetImportPath) 80 | 81 | 82 | true 83 | bin\TestAccount\ 84 | TRACE;DEBUG;LOCALHOST 85 | full 86 | AnyCPU 87 | prompt 88 | MinimumRecommendedRules.ruleset 89 | 90 | 91 | true 92 | bin\x64\TestAccount\ 93 | x64 94 | MinimumRecommendedRules.ruleset 95 | 96 | 97 | 98 | False 99 | ..\packages\DocumentFormat.OpenXml.2.5\lib\DocumentFormat.OpenXml.dll 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | true 126 | 127 | 128 | app.config 129 | True 130 | 131 | 132 | Designer 133 | 134 | 135 | 136 | 137 | 138 | 139 | 146 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OpenXMLUtils 2 | ============ 3 | 4 | Utility classes for the [Open XML SDK 2.5 for Office](http://msdn.microsoft.com/en-us/library/office/bb448854.aspx). 5 | 6 | Copyright (c) Geoplex All rights reserved. 7 | Licensed under the Apache License, Version 2.0. 8 | See License.txt in the project root for license information. 9 | 10 | Package 11 | ======= 12 | 13 | https://www.nuget.org/packages/OpenXmlUtils/ 14 | 15 | Documentation 16 | ============= 17 | 18 | Currently supports creation of simple spreadsheets from a collection of objects based on this: 19 | http://www.codeproject.com/Articles/97307/Using-C-and-Open-XML-SDK-for-Microsoft-Office. 20 | 21 | ##Example Usage 22 | 23 | Using a list of objects: 24 | ```c# 25 | var songs = 26 | new List 27 | { new Song { Artist = "Joy Devision", Title = "Disorder", Date = DateTime.Today, TimeSpan = TimeSpan.FromSeconds(3343), Int = 89453312L, Double = 4043.4545, Bool = false }, 28 | new Song { Artist = "Moderate", Title = "A New Error", Date = DateTime.Today, TimeSpan = TimeSpan.FromSeconds(34345), Int = 89563312L, Double = 5.6, Bool = true }, 29 | new Song { Artist = "Massive Attack", Title = "Paradise Circus", Date = DateTime.Today + TimeSpan.FromDays(53), TimeSpan = TimeSpan.FromSeconds(545), Int = 344334L, Double = 222.3, Bool = false }, 30 | new Song { Artist = "The Horrors", Title = "Still Life", Date = DateTime.Today - TimeSpan.FromDays(1), TimeSpan = TimeSpan.FromSeconds(22345), Int = 9497934L, Double = 33.4634444, Bool = true }, 31 | new Song { Artist = "Todd Terje", Title = "Inspector Norse", Date = DateTime.Today - TimeSpan.FromDays(356), TimeSpan = TimeSpan.FromSeconds(5565), Int = 34211343L, Double = 54.44444, Bool = false }, 32 | new Song { Artist = "Alpine", Title = "Hands", Date = DateTime.Today - TimeSpan.FromDays(5.5), TimeSpan = TimeSpan.FromSeconds(9907), Int = 32323333L, Double = 3445.44, Bool = false }, 33 | new Song { Artist = "Parquet Courts", Title = "Ducking and Dodging", Date = DateTime.Today - TimeSpan.FromDays(88.55), TimeSpan = TimeSpan.FromSeconds(8877), Int = 8088872L, Double = 44.0, Bool = false }, 34 | }; 35 | 36 | var fields = new List 37 | { 38 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 39 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 40 | new SpreadsheetField{ Title = "RandomDate", FieldName = "Date"}, 41 | new SpreadsheetField{ Title = "RandomTimeSpan", FieldName = "TimeSpan"}, 42 | new SpreadsheetField{ Title = "RandomInt", FieldName = "Int"}, 43 | new SpreadsheetField{ Title = "RandomDouble", FieldName = "Double"}, 44 | new SpreadsheetField{ Title = "RandomBool", FieldName = "Bool"} 45 | }; 46 | 47 | Spreadsheet.Create(@"C:\temp\songs.xlsx", 48 | new SheetDefinition 49 | { 50 | Fields = fields, 51 | Name = "Songs", 52 | SubTitle = DateTime.Today.ToLongDateString(), 53 | IncludeTotalsRow = true, 54 | Objects = songs 55 | }); 56 | ``` 57 | 58 | Using a list of dictionaries: 59 | ```c# 60 | var songs = 61 | new List 62 | { 63 | new Dictionary { { "Artist" , "Joy Devision"}, {"Title" , "Disorder"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 64 | new Dictionary { { "Artist" , "Moderate"}, {"Title" , "A New Error"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(34345)}, {"Int", 89563312L},{ "Double" , 5.6}, {"Bool" , true }}, 65 | new Dictionary { { "Artist" , "Massive Attack"}, {"Title" , "Paradise Circus"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(545)}, {"Int", 344334L},{ "Double" , 222.3}, {"Bool" , false }}, 66 | new Dictionary { { "Artist" , "The Horrors"}, {"Title" , "Still Life"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(1123)}, {"Int", 9497934L},{ "Double" , 33.4634444}, {"Bool" , true }}, 67 | new Dictionary { { "Artist" , "Todd Terje"}, {"Title" , "Inspector Norse"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(9973)}, {"Int", 34211343L},{ "Double" , 54.44444}, {"Bool" , false }}, 68 | new Dictionary { { "Artist" , "Alpine"}, {"Title" , "Hands"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3841)}, {"Int", 32323333L},{ "Double" , 3445.44}, {"Bool" , false }}, 69 | new Dictionary { { "Artist" , "Parquet Courts"}, {"Title" , "Ducking and Dodging"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(9973)}, {"Int", 8088872L},{ "Double" , 44.0}, {"Bool" , false }} 70 | }; 71 | 72 | var fields = new List 73 | { 74 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 75 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 76 | new SpreadsheetField{ Title = "RandomDate", FieldName = "Date"}, 77 | new SpreadsheetField{ Title = "RandomTimeSpan", FieldName = "TimeSpan"}, 78 | new SpreadsheetField{ Title = "RandomInt", FieldName = "Int"}, 79 | new SpreadsheetField{ Title = "RandomDouble", FieldName = "Double"}, 80 | new SpreadsheetField{ Title = "RandomBool", FieldName = "Bool"} 81 | }; 82 | 83 | Spreadsheet.Create(@"C:\temp\songs_dict.xlsx", 84 | new SheetDefinition 85 | { 86 | Fields = fields, 87 | Name = "Songs", 88 | IncludeTotalsRow = true, 89 | Objects = songs 90 | }); 91 | ``` 92 | 93 | Creating multiple sheets: 94 | ```c# 95 | Spreadsheet.Create(@"C:\temp\songs_multi.xlsx", 96 | new List> 97 | { 98 | new SheetDefinition 99 | { 100 | Fields = fields, 101 | Name = "1", 102 | IncludeTotalsRow = true, 103 | Objects = songs 104 | }, 105 | new SheetDefinition 106 | { 107 | Fields = fields, 108 | Name = "2", 109 | IncludeTotalsRow = true, 110 | Objects = songs2 111 | } 112 | }); 113 | ``` 114 | 115 | Hyperlinks: 116 | ```c# 117 | var songs = 118 | new List 119 | { 120 | new Song { Artist = "Parquet Courts", Title = "Ducking and Dodging", Url = "https://parquetcourts.wordpress.com", Hyperlink = "parquetcourts.wordpress.com"}, 121 | }; 122 | 123 | var fields = new List 124 | { 125 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 126 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 127 | new HyperlinkField{ Title = "Website", FieldName = "Url", DisplayFieldName = "Hyperlink"} 128 | }; 129 | 130 | Spreadsheet.Create(@"C:\temp\songs_hyperlinks.xlsx", 131 | new SheetDefinition 132 | { 133 | Fields = fields, 134 | Name = "Songs", 135 | SubTitle = DateTime.Today.ToLongDateString(), 136 | IncludeTotalsRow = true, 137 | Objects = songs 138 | }); 139 | ``` 140 | 141 | Row Grouping: 142 | ```c# 143 | var songs = 144 | new List 145 | { 146 | new Dictionary { { "Artist" , "Joy Devision"} }, 147 | new List { 148 | new Dictionary { { "Albumn" , "Closer"} }, 149 | new List { 150 | new Dictionary{ {"Title" , "Isolation"} }, 151 | new Dictionary{ {"Title" , "Colony"} }, 152 | new Dictionary{ {"Title" , "Decades"} }, 153 | }, 154 | new Dictionary { { "Albumn" , "Unknown Pleasures"} }, 155 | new List { 156 | new Dictionary{ {"Title" , "Disorder"} }, 157 | new Dictionary{ {"Title" , "Candidate"} }, 158 | new Dictionary{ {"Title" , "She's Lost Control"} } 159 | }, 160 | }, 161 | new Dictionary { { "Artist" , "Moderate"} }, 162 | new List { 163 | new Dictionary{ {"Title" , "A New Error"} }, 164 | new Dictionary{ {"Title" , "Rusty Nails"} }, 165 | new Dictionary{ {"Title" , "Seamonkey"} }, 166 | }, 167 | }; 168 | 169 | var fields = new List 170 | { 171 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 172 | new SpreadsheetField{ Title = "Albumn", FieldName = "Albumn"}, 173 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 174 | }; 175 | 176 | Spreadsheet.Create(@"C:\temp\songs_row_grouping.xlsx", 177 | new SheetDefinition 178 | { 179 | Fields = fields, 180 | Name = "Songs", 181 | IncludeTotalsRow = false, 182 | Objects = songs 183 | }); 184 | ``` 185 | -------------------------------------------------------------------------------- /OpenXmlUtils.Tests/OpenXmlUtils.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | {E5DF0F1A-E559-4355-8A9E-3F6B96050F53} 7 | Library 8 | Properties 9 | OpenXmlUtils.Tests 10 | OpenXmlUtils.Tests 11 | v4.5 12 | 512 13 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 10.0 15 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 16 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 17 | False 18 | UnitTest 19 | ..\ 20 | true 21 | 22 | 23 | true 24 | full 25 | false 26 | bin\Debug\ 27 | TRACE;DEBUG;LOCALHOST 28 | prompt 29 | 4 30 | 31 | 32 | pdbonly 33 | true 34 | bin\Release\ 35 | TRACE;LOCALHOST 36 | prompt 37 | 4 38 | 39 | 40 | bin\DEV\ 41 | 42 | 43 | bin\PROD\ 44 | 45 | 46 | bin\UAT\ 47 | 48 | 49 | x64 50 | bin\x64\Debug\ 51 | 52 | 53 | x64 54 | bin\x64\Release\ 55 | 56 | 57 | x64 58 | bin\x64\DEV\ 59 | 60 | 61 | x64 62 | bin\x64\PROD\ 63 | 64 | 65 | x64 66 | bin\x64\UAT\ 67 | 68 | 69 | bin\HOTFIX\ 70 | AnyCPU 71 | MinimumRecommendedRules.ruleset 72 | HOTFIX 73 | 74 | 75 | bin\x64\HOTFIX\ 76 | x64 77 | MinimumRecommendedRules.ruleset 78 | HOTFIX 79 | 80 | 81 | true 82 | bin\TestAccount\ 83 | TRACE;DEBUG;LOCALHOST 84 | full 85 | AnyCPU 86 | prompt 87 | MinimumRecommendedRules.ruleset 88 | 89 | 90 | true 91 | bin\x64\TestAccount\ 92 | x64 93 | MinimumRecommendedRules.ruleset 94 | 95 | 96 | 97 | False 98 | ..\packages\DocumentFormat.OpenXml.2.5\lib\DocumentFormat.OpenXml.dll 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | true 121 | 122 | 123 | app.config 124 | True 125 | 126 | 127 | app.config 128 | True 129 | 130 | 131 | 132 | 133 | 134 | {5b803def-1248-4160-800c-f90287a807b2} 135 | OpenXmlUtils 136 | 137 | 138 | 139 | 140 | 141 | 142 | False 143 | 144 | 145 | False 146 | 147 | 148 | False 149 | 150 | 151 | False 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 166 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Geoplex. http://www.geoplex.com.au/ 2 | 3 | Apache License 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "{}" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright 2014 Geoplex 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. -------------------------------------------------------------------------------- /OpenXmlUtils.Tests/SpreadsheetUnitTest.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "SpreadsheetUnitTest.cs" 4 | // Purpose: "Some basic tests to demonstrate the Spreadsheet wrapper class" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | using System; 24 | using System.Collections.Generic; 25 | using Microsoft.VisualStudio.TestTools.UnitTesting; 26 | 27 | namespace OpenXmlUtils.Tests 28 | { 29 | public class Song 30 | { 31 | public string Artist { get; set; } 32 | public string Title { get; set; } 33 | public double Double { get; set; } 34 | public long Int { get; set; } 35 | public bool Bool { get; set; } 36 | public DateTime Date { get; set; } 37 | public TimeSpan TimeSpan { get; set; } 38 | public double Decimal { get; set; } 39 | public string Url { get; set; } 40 | public string Hyperlink { get; set; } 41 | } 42 | 43 | [TestClass] 44 | public class SpreadsheetUnitTest 45 | { 46 | [TestMethod] 47 | public void TestObjectsToSpreadsheet() 48 | { 49 | var songs = 50 | new List 51 | { new Song { Artist = "Joy Devision", Title = "Disorder", Date = DateTime.Today, TimeSpan = TimeSpan.FromSeconds(3343), Int = 89453312L, Double = 4043.4545, Bool = false }, 52 | new Song { Artist = "Moderate", Title = "A New Error", Date = DateTime.Today, TimeSpan = TimeSpan.FromSeconds(34345), Int = 89563312L, Double = 5.6, Bool = true }, 53 | new Song { Artist = "Massive Attack", Title = "Paradise Circus", Date = DateTime.Today + TimeSpan.FromDays(53), TimeSpan = TimeSpan.FromSeconds(545), Int = 344334L, Double = 222.3, Bool = false }, 54 | new Song { Artist = "The Horrors", Title = "Still Life", Date = DateTime.Today - TimeSpan.FromDays(1), TimeSpan = TimeSpan.FromSeconds(22345), Int = 9497934L, Double = 33.4634444, Bool = true }, 55 | new Song { Artist = "Todd Terje", Title = "Inspector Norse", Date = DateTime.Today - TimeSpan.FromDays(356), TimeSpan = TimeSpan.FromSeconds(5565), Int = 34211343L, Double = 54.44444, Bool = false }, 56 | new Song { Artist = "Alpine", Title = "Hands", Date = DateTime.Today - TimeSpan.FromDays(5.5), TimeSpan = TimeSpan.FromSeconds(9907), Int = 32323333L, Double = 3445.44, Bool = false , Decimal = 023.0032334}, 57 | new Song { Artist = "Parquet Courts", Title = "Ducking and Dodging", Date = DateTime.Today - TimeSpan.FromDays(88.55), TimeSpan = TimeSpan.FromSeconds(8877), Int = 8088872L, Double = 44.0, Bool = false, Url = "https://parquetcourts.wordpress.com", Hyperlink = "parquetcourts.wordpress.com"}, 58 | }; 59 | 60 | var fields = new List 61 | { 62 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 63 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 64 | new SpreadsheetField{ Title = "RandomDate", FieldName = "Date"}, 65 | new SpreadsheetField{ Title = "RandomTimeSpan", FieldName = "TimeSpan"}, 66 | new SpreadsheetField{ Title = "RandomInt", FieldName = "Int"}, 67 | new SpreadsheetField{ Title = "RandomDouble", FieldName = "Double"}, 68 | new SpreadsheetField{ Title = "RandomBool", FieldName = "Bool"}, 69 | new DecimalNumberField{ Title = "RandomDecimal", FieldName = "Decimal", DecimalPlaces = 5}, 70 | new HyperlinkField{ Title = "Website", FieldName = "Url", DisplayFieldName = "Hyperlink"} 71 | }; 72 | 73 | Spreadsheet.Create(@"C:\temp\songs.xlsx", 74 | new SheetDefinition 75 | { 76 | Fields = fields, 77 | Name = "Songs", 78 | SubTitle = DateTime.Today.ToLongDateString(), 79 | IncludeTotalsRow = true, 80 | Objects = songs 81 | }); 82 | } 83 | 84 | [TestMethod] 85 | public void TestDictionariesToSpreadsheet() 86 | { 87 | var songs = 88 | new List 89 | { 90 | new Dictionary { { "Artist" , "Joy Devision"}, {"Title" , "Disorder"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 91 | new Dictionary { { "Artist" , "Moderate"}, {"Title" , "A New Error"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(34345)}, {"Int", 89563312L},{ "Double" , 5.6}, {"Bool" , true }}, 92 | new Dictionary { { "Artist" , "Massive Attack"}, {"Title" , "Paradise Circus"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(545)}, {"Int", 344334L},{ "Double" , 222.3}, {"Bool" , false }}, 93 | new Dictionary { { "Artist" , "The Horrors"}, {"Title" , "Still Life"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(1123)}, {"Int", 9497934L},{ "Double" , 33.4634444}, {"Bool" , true }}, 94 | new Dictionary { { "Artist" , "Todd Terje"}, {"Title" , "Inspector Norse"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(9973)}, {"Int", 34211343L},{ "Double" , 54.44444}, {"Bool" , false }}, 95 | new Dictionary { { "Artist" , "Alpine"}, {"Title" , "Hands"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3841)}, {"Int", 32323333L},{ "Double" , 3445.44}, {"Bool" , false }}, 96 | new Dictionary { { "Artist" , "Parquet Courts"}, {"Title" , "Ducking and Dodging"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(9973)}, {"Int", 8088872L},{ "Double" , 44.0}, {"Bool" , false }, {"Url", "https://parquetcourts.wordpress.com"}, {"Hyperlink", "parquetcourts.wordpress.com"}} 97 | }; 98 | 99 | var fields = new List 100 | { 101 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 102 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 103 | new SpreadsheetField{ Title = "RandomDate", FieldName = "Date"}, 104 | new SpreadsheetField{ Title = "RandomTimeSpan", FieldName = "TimeSpan"}, 105 | new SpreadsheetField{ Title = "RandomInt", FieldName = "Int"}, 106 | new SpreadsheetField{ Title = "RandomDouble", FieldName = "Double"}, 107 | new SpreadsheetField{ Title = "RandomBool", FieldName = "Bool"}, 108 | new HyperlinkField{ Title = "Website", FieldName = "Url", DisplayFieldName = "Hyperlink"} 109 | }; 110 | 111 | Spreadsheet.Create(@"C:\temp\songs_dict.xlsx", 112 | new SheetDefinition 113 | { 114 | Fields = fields, 115 | Name = "Songs", 116 | IncludeTotalsRow = true, 117 | Objects = songs 118 | }); 119 | } 120 | 121 | [TestMethod] 122 | public void TestMultipleSheets() 123 | { 124 | var songs = 125 | new List 126 | { new Song { Artist = "Joy Devision", Title = "Disorder", Date = DateTime.Today, TimeSpan = TimeSpan.FromSeconds(3434), Int = 89453312L, Double = 4043.4545, Bool = false }, 127 | new Song { Artist = "Moderate", Title = "A New Error", Date = DateTime.Today, TimeSpan = TimeSpan.FromSeconds(6576), Int = 89563312L, Double = 5.6, Bool = true }, 128 | new Song { Artist = "Massive Attack", Title = "Paradise Circus", Date = DateTime.Today + TimeSpan.FromDays(53), TimeSpan = TimeSpan.FromSeconds(9974), Int = 344334L, Double = 222.3, Bool = false }, 129 | new Song { Artist = "The Horrors", Title = "Still Life", Date = DateTime.Today - TimeSpan.FromDays(1), TimeSpan = TimeSpan.FromSeconds(9935), Int = 9497934L, Double = 33.4634444, Bool = true }, 130 | }; 131 | 132 | var songs2 = 133 | new List 134 | { new Song { Artist = "Todd Terje", Title = "Inspector Norse", Date = DateTime.Today - TimeSpan.FromDays(356), TimeSpan = TimeSpan.FromSeconds(9009), Int = 34211343L, Double = 54.44444, Bool = false }, 135 | new Song { Artist = "Alpine", Title = "Hands", Date = DateTime.Today - TimeSpan.FromDays(5.5), TimeSpan = TimeSpan.FromSeconds(8836), Int = 32323333L, Double = 3445.44, Bool = false }, 136 | new Song { Artist = "Parquet Courts", Title = "Ducking and Dodging", Date = DateTime.Today - TimeSpan.FromDays(88.55), TimeSpan = TimeSpan.FromSeconds(1162), Int = 8088872L, Double = 44.0, Bool = false }, 137 | }; 138 | 139 | var fields = new List 140 | { 141 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 142 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 143 | new SpreadsheetField{ Title = "RandomDate", FieldName = "Date"}, 144 | new SpreadsheetField{ Title = "RandomTimeSpan", FieldName = "TimeSpan"}, 145 | new SpreadsheetField{ Title = "RandomInt", FieldName = "Int"}, 146 | new SpreadsheetField{ Title = "RandomDouble", FieldName = "Double"}, 147 | new SpreadsheetField{ Title = "RandomBool", FieldName = "Bool"} 148 | }; 149 | 150 | Spreadsheet.Create(@"C:\temp\songs_multi.xlsx", 151 | new List> 152 | { 153 | new SheetDefinition 154 | { 155 | Fields = fields, 156 | Name = "1", 157 | IncludeTotalsRow = true, 158 | Objects = songs 159 | }, 160 | new SheetDefinition 161 | { 162 | Fields = fields, 163 | Name = "2", 164 | IncludeTotalsRow = true, 165 | Objects = songs2 166 | } 167 | }); 168 | } 169 | 170 | [TestMethod] 171 | public void TestRowGrouping() 172 | { 173 | var songs = 174 | new List 175 | { 176 | new Dictionary { { "Artist" , "Joy Devision"} }, 177 | new List { 178 | new Dictionary { { "Albumn" , "Closer"} }, 179 | new List { 180 | new Dictionary{ {"Title" , "Isolation"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 181 | new Dictionary{ {"Title" , "Colony"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 182 | new Dictionary{ {"Title" , "Decades"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 183 | }, 184 | new Dictionary { { "Albumn" , "Unknown Pleasures"} }, 185 | new List { 186 | new Dictionary{ {"Title" , "Disorder"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 187 | new Dictionary{ {"Title" , "Candidate"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 188 | new Dictionary{ {"Title" , "She's Lost Control"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }} 189 | }, 190 | }, 191 | new Dictionary { { "Artist" , "Moderate"} }, 192 | new List { 193 | new Dictionary{ {"Title" , "A New Error"}, {"Albumn" , "Moderat"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 194 | new Dictionary{ {"Title" , "Rusty Nails"}, {"Albumn" , "Moderat"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 195 | new Dictionary{ {"Title" , "Seamonkey"}, {"Albumn" , "Moderat"}, {"Date" , DateTime.Today}, {"TimeSpan" , TimeSpan.FromSeconds(3343)}, {"Int" ,89453312L},{ "Double" , 4043.4545}, {"Bool" , false }}, 196 | }, 197 | }; 198 | 199 | var fields = new List 200 | { 201 | new SpreadsheetField{ Title = "Artist", FieldName = "Artist"}, 202 | new SpreadsheetField{ Title = "Albumn", FieldName = "Albumn"}, 203 | new SpreadsheetField{ Title = "Title", FieldName = "Title"}, 204 | new SpreadsheetField{ Title = "RandomDate", FieldName = "Date"}, 205 | new SpreadsheetField{ Title = "RandomTimeSpan", FieldName = "TimeSpan"}, 206 | new SpreadsheetField{ Title = "RandomInt", FieldName = "Int"}, 207 | new SpreadsheetField{ Title = "RandomDouble", FieldName = "Double"}, 208 | new SpreadsheetField{ Title = "RandomBool", FieldName = "Bool"}, 209 | }; 210 | 211 | Spreadsheet.Create(@"C:\temp\songs_row_grouping.xlsx", 212 | new SheetDefinition 213 | { 214 | Fields = fields, 215 | Name = "Songs", 216 | IncludeTotalsRow = false, 217 | Objects = songs 218 | }); 219 | } 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /OpenXmlUtils/CustomStylesheet.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "CustomStylesheet.cs" 4 | // Purpose: "Defines how a spreadsheet will look." 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | using System.Drawing; 24 | using DocumentFormat.OpenXml; 25 | using DocumentFormat.OpenXml.Spreadsheet; 26 | using Color = System.Drawing.Color; 27 | using Font = DocumentFormat.OpenXml.Spreadsheet.Font; 28 | 29 | namespace OpenXmlUtils 30 | { 31 | public class CustomStylesheet : Stylesheet 32 | { 33 | public enum CustomCellFormats : uint 34 | { 35 | // these are referenced by index, must be added in this order 36 | DefaultText = 0, 37 | DefaultDate, 38 | DefaultNumber2DecimalPlace, 39 | DefaultNumber5DecimalPlace, 40 | DefaultDateTime, 41 | HeaderText, 42 | TotalsNumber, 43 | TotalsNumber2DecimalPlace, 44 | TotalsText, 45 | TitleText, 46 | SubtitleText, 47 | Duration, 48 | TotalsDuration, 49 | Hyperlink 50 | } 51 | 52 | public CustomStylesheet() 53 | { 54 | NumberingFormat nfDateTime; 55 | NumberingFormat nf5Decimal; 56 | NumberingFormat nfDuration; 57 | NumberingFormat nfTotalDuration; 58 | 59 | Append(CreateNumberingFormats(out nfDateTime, out nf5Decimal, out nfDuration, out nfTotalDuration)); 60 | Append(CreateFonts()); 61 | Append(CreateFills()); 62 | Append(CreateBorders()); 63 | Append(CreateCellStyleFormats()); 64 | Append(CreateCellFormats(nfDateTime, nf5Decimal, nfDuration, nfTotalDuration)); 65 | Append(CreateCellStyles()); 66 | Append(CreateDifferentialFormats()); 67 | Append(CreateTableStyles()); 68 | } 69 | 70 | private static TableStyles CreateTableStyles() 71 | { 72 | var tss = new TableStyles(); 73 | tss.Count = 0; 74 | tss.DefaultTableStyle = StringValue.FromString("TableStyleMedium9"); 75 | tss.DefaultPivotStyle = StringValue.FromString("PivotStyleLight16"); 76 | return tss; 77 | } 78 | 79 | private static DifferentialFormats CreateDifferentialFormats() 80 | { 81 | var dfs = new DifferentialFormats(); 82 | dfs.Count = 0; 83 | return dfs; 84 | } 85 | 86 | private static CellStyles CreateCellStyles() 87 | { 88 | var css = new CellStyles(); 89 | 90 | // cell style 0 91 | var cs = new CellStyle(); 92 | cs.Name = StringValue.FromString("Normal"); 93 | cs.FormatId = 0; 94 | cs.BuiltinId = 0; 95 | css.AppendChild(cs); 96 | css.Count = UInt32Value.FromUInt32((uint) css.ChildElements.Count); 97 | return css; 98 | } 99 | 100 | /// 101 | /// Ensure cell formats are added in the order specified by the enumeration 102 | /// 103 | private static CellFormats CreateCellFormats(NumberingFormat nfDateTime, NumberingFormat nf5Decimal, 104 | NumberingFormat nfDuration, NumberingFormat nfTotalDuration) 105 | { 106 | var cfs = new CellFormats(); 107 | 108 | // CustomCellFormats.DefaultText 109 | var cf = new CellFormat(); 110 | cf.NumberFormatId = 0; 111 | cf.FontId = 0; 112 | cf.FillId = 0; 113 | cf.BorderId = 0; 114 | cf.FormatId = 0; 115 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(false); 116 | cfs.AppendChild(cf); 117 | 118 | // CustomCellFormats.DefaultDate 119 | cf = new CellFormat(); 120 | cf.NumberFormatId = 14; // mm-dd-yy 121 | cf.FontId = 0; 122 | cf.FillId = 0; 123 | cf.BorderId = 0; 124 | cf.FormatId = 0; 125 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 126 | cfs.AppendChild(cf); 127 | 128 | // CustomCellFormats.DefaultNumber2DecimalPlace 129 | cf = new CellFormat(); 130 | cf.NumberFormatId = 4; // #,##0.00 131 | cf.FontId = 0; 132 | cf.FillId = 0; 133 | cf.BorderId = 0; 134 | cf.FormatId = 0; 135 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 136 | cfs.AppendChild(cf); 137 | 138 | // CustomCellFormats.DefaultNumber5DecimalPlace 139 | cf = new CellFormat(); 140 | cf.NumberFormatId = nf5Decimal.NumberFormatId; 141 | cf.FontId = 0; 142 | cf.FillId = 0; 143 | cf.BorderId = 0; 144 | cf.FormatId = 0; 145 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 146 | cfs.AppendChild(cf); 147 | 148 | // CustomCellFormats.DefaultDateTime 149 | cf = new CellFormat(); 150 | cf.NumberFormatId = nfDateTime.NumberFormatId; 151 | cf.FontId = 0; 152 | cf.FillId = 0; 153 | cf.BorderId = 0; 154 | cf.FormatId = 0; 155 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 156 | cfs.AppendChild(cf); 157 | 158 | // CustomCellFormats.HeaderText 159 | cf = new CellFormat(); 160 | cf.NumberFormatId = 0; 161 | cf.FontId = 1; 162 | cf.FillId = 2; 163 | cf.BorderId = 0; 164 | cf.FormatId = 0; 165 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(false); 166 | cfs.AppendChild(cf); 167 | 168 | // CustomCellFormats.TotalsNumber 169 | cf = new CellFormat(); 170 | cf.NumberFormatId = 0; 171 | cf.FontId = 0; 172 | cf.FillId = 3; 173 | cf.BorderId = 2; 174 | cf.FormatId = 0; 175 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 176 | cfs.AppendChild(cf); 177 | 178 | // CustomCellFormats.TotalsNumber2DecimalPlace 179 | cf = new CellFormat(); 180 | cf.NumberFormatId = 4; // #,##0.00 181 | cf.FontId = 0; 182 | cf.FillId = 3; 183 | cf.BorderId = 2; 184 | cf.FormatId = 0; 185 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 186 | cfs.AppendChild(cf); 187 | 188 | // CustomCellFormats.TotalsText 189 | cf = new CellFormat(); 190 | cf.NumberFormatId = 49; // @ 191 | cf.FontId = 0; 192 | cf.FillId = 3; 193 | cf.BorderId = 2; 194 | cf.FormatId = 0; 195 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 196 | cfs.AppendChild(cf); 197 | 198 | // CustomCellFormats.TitleText 199 | cf = new CellFormat(); 200 | cf.NumberFormatId = 0; 201 | cf.FontId = 2; 202 | cf.FillId = 0; 203 | cf.BorderId = 0; 204 | cf.FormatId = 0; 205 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(false); 206 | cf.Alignment = new Alignment 207 | { 208 | Vertical = new EnumValue(VerticalAlignmentValues.Bottom) 209 | }; 210 | cfs.AppendChild(cf); 211 | 212 | // CustomCellFormats.SubtitleText 213 | cf = new CellFormat(); 214 | cf.NumberFormatId = 0; 215 | cf.FontId = 3; 216 | cf.FillId = 0; 217 | cf.BorderId = 0; 218 | cf.FormatId = 0; 219 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(false); 220 | cf.Alignment = new Alignment 221 | { 222 | Vertical = new EnumValue(VerticalAlignmentValues.Top) 223 | }; 224 | cfs.AppendChild(cf); 225 | 226 | // CustomCellFormats.Duration 227 | cf = new CellFormat(); 228 | cf.NumberFormatId = nfDuration.NumberFormatId; // [h]:mm 229 | cf.FontId = 0; 230 | cf.FillId = 0; 231 | cf.BorderId = 0; 232 | cf.FormatId = 0; 233 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 234 | cf.Alignment = new Alignment 235 | { 236 | Horizontal = new EnumValue(HorizontalAlignmentValues.Right) 237 | }; 238 | cfs.AppendChild(cf); 239 | 240 | // CustomCellFormats.TotalsNumber 241 | cf = new CellFormat(); 242 | cf.NumberFormatId = nfTotalDuration.NumberFormatId; // d:h:mm 243 | cf.FontId = 0; 244 | cf.FillId = 3; 245 | cf.BorderId = 2; 246 | cf.FormatId = 0; 247 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(true); 248 | cf.Alignment = new Alignment 249 | { 250 | Horizontal = new EnumValue(HorizontalAlignmentValues.Right) 251 | }; 252 | cfs.AppendChild(cf); 253 | 254 | // CustomCellFormats.Hyperlink 255 | cf = new CellFormat(); 256 | cf.NumberFormatId = 0; 257 | cf.FontId = 4; 258 | cf.FillId = 0; 259 | cf.BorderId = 0; 260 | cf.FormatId = 0; 261 | cf.ApplyNumberFormat = BooleanValue.FromBoolean(false); 262 | cfs.AppendChild(cf); 263 | 264 | cfs.Count = UInt32Value.FromUInt32((uint) cfs.ChildElements.Count); 265 | return cfs; 266 | } 267 | 268 | private static NumberingFormats CreateNumberingFormats(out NumberingFormat nfDateTime, 269 | out NumberingFormat nf5Decimal, out NumberingFormat nfDuration, out NumberingFormat nfTotalDuration) 270 | { 271 | // built-in formats go up to 164 272 | uint iExcelIndex = 164; 273 | 274 | var nfs = new NumberingFormats(); 275 | nfDateTime = new NumberingFormat(); 276 | nfDateTime.NumberFormatId = UInt32Value.FromUInt32(iExcelIndex++); 277 | nfDateTime.FormatCode = StringValue.FromString("dd/mm/yyyy hh:mm:ss"); 278 | nfs.AppendChild(nfDateTime); 279 | 280 | nf5Decimal = new NumberingFormat(); 281 | nf5Decimal.NumberFormatId = UInt32Value.FromUInt32(iExcelIndex++); 282 | nf5Decimal.FormatCode = StringValue.FromString("#,##0.00000"); 283 | nfs.AppendChild(nf5Decimal); 284 | 285 | nfDuration = new NumberingFormat(); 286 | nfDuration.NumberFormatId = UInt32Value.FromUInt32(iExcelIndex++); 287 | nfDuration.FormatCode = StringValue.FromString("[h]:mm"); 288 | nfs.AppendChild(nfDuration); 289 | 290 | nfTotalDuration = new NumberingFormat(); 291 | nfTotalDuration.NumberFormatId = UInt32Value.FromUInt32(iExcelIndex++); 292 | nfTotalDuration.FormatCode = StringValue.FromString("d:h:mm"); 293 | nfs.AppendChild(nfTotalDuration); 294 | 295 | nfs.Count = UInt32Value.FromUInt32((uint) nfs.ChildElements.Count); 296 | return nfs; 297 | } 298 | 299 | private static CellStyleFormats CreateCellStyleFormats() 300 | { 301 | var csfs = new CellStyleFormats(); 302 | 303 | // cell style 0 304 | var cf = new CellFormat(); 305 | cf.NumberFormatId = 0; 306 | cf.FontId = 0; 307 | cf.FillId = 0; 308 | cf.BorderId = 0; 309 | csfs.AppendChild(cf); 310 | csfs.Count = UInt32Value.FromUInt32((uint) csfs.ChildElements.Count); 311 | return csfs; 312 | } 313 | 314 | private static Borders CreateBorders() 315 | { 316 | var borders = new Borders(); 317 | 318 | // boarder index 0 319 | var border = new Border(); 320 | border.LeftBorder = new LeftBorder(); 321 | border.RightBorder = new RightBorder(); 322 | border.TopBorder = new TopBorder(); 323 | border.BottomBorder = new BottomBorder(); 324 | border.DiagonalBorder = new DiagonalBorder(); 325 | borders.AppendChild(border); 326 | 327 | // boarder Index 1 328 | border = new Border(); 329 | border.LeftBorder = new LeftBorder(); 330 | border.LeftBorder.Style = BorderStyleValues.Thin; 331 | border.RightBorder = new RightBorder(); 332 | border.RightBorder.Style = BorderStyleValues.Thin; 333 | border.TopBorder = new TopBorder(); 334 | border.TopBorder.Style = BorderStyleValues.Thin; 335 | border.BottomBorder = new BottomBorder(); 336 | border.BottomBorder.Style = BorderStyleValues.Thin; 337 | border.DiagonalBorder = new DiagonalBorder(); 338 | borders.AppendChild(border); 339 | 340 | // boarder Index 2 341 | border = new Border(); 342 | border.LeftBorder = new LeftBorder(); 343 | border.RightBorder = new RightBorder(); 344 | border.TopBorder = new TopBorder(); 345 | border.TopBorder.Style = BorderStyleValues.Thin; 346 | border.BottomBorder = new BottomBorder(); 347 | border.BottomBorder.Style = BorderStyleValues.Thin; 348 | border.DiagonalBorder = new DiagonalBorder(); 349 | borders.AppendChild(border); 350 | 351 | borders.Count = UInt32Value.FromUInt32((uint) borders.ChildElements.Count); 352 | return borders; 353 | } 354 | 355 | private static Fills CreateFills() 356 | { 357 | // fill 0 358 | var fills = new Fills(); 359 | var fill = new Fill(); 360 | var patternFill = new PatternFill {PatternType = PatternValues.None}; 361 | fill.PatternFill = patternFill; 362 | fills.AppendChild(fill); 363 | 364 | // fill 1 (in-built fill) 365 | fill = new Fill(); 366 | patternFill = new PatternFill { PatternType = PatternValues.Gray125 }; 367 | fill.PatternFill = patternFill; 368 | fills.AppendChild(fill); 369 | 370 | // fill 2 371 | fill = new Fill(); 372 | patternFill = new PatternFill(); 373 | patternFill.PatternType = PatternValues.Solid; 374 | var fillColor = Color.LightSkyBlue; 375 | patternFill.ForegroundColor = new ForegroundColor { Rgb = HexBinaryValueFromColor(fillColor) }; 376 | patternFill.BackgroundColor = new BackgroundColor { Rgb = HexBinaryValueFromColor(fillColor) }; 377 | fill.PatternFill = patternFill; 378 | fills.AppendChild(fill); 379 | 380 | // fill 3 381 | fill = new Fill(); 382 | patternFill = new PatternFill(); 383 | patternFill.PatternType = PatternValues.Solid; 384 | fillColor = Color.Orange; 385 | patternFill.ForegroundColor = new ForegroundColor { Rgb = HexBinaryValueFromColor(fillColor) }; 386 | patternFill.BackgroundColor = new BackgroundColor { Rgb = HexBinaryValueFromColor(fillColor) }; 387 | fill.PatternFill = patternFill; 388 | fills.AppendChild(fill); 389 | 390 | fills.Count = UInt32Value.FromUInt32((uint) fills.ChildElements.Count); 391 | return fills; 392 | } 393 | 394 | private static Fonts CreateFonts() 395 | { 396 | var fts = new Fonts(); 397 | 398 | // font 0 399 | var ft = new Font(); 400 | var ftn = new FontName {Val = StringValue.FromString("Arial")}; 401 | var ftsz = new FontSize {Val = DoubleValue.FromDouble(11)}; 402 | ft.FontName = ftn; 403 | ft.FontSize = ftsz; 404 | fts.AppendChild(ft); 405 | 406 | // font 1 407 | ft = new Font(); 408 | ftn = new FontName { Val = StringValue.FromString("Arial") }; 409 | ftsz = new FontSize { Val = DoubleValue.FromDouble(12) }; 410 | ft.FontName = ftn; 411 | ft.FontSize = ftsz; 412 | ft.Bold = new Bold(); 413 | fts.AppendChild(ft); 414 | 415 | // font 2 416 | ft = new Font(); 417 | ftn = new FontName { Val = StringValue.FromString("Arial") }; 418 | ftsz = new FontSize { Val = DoubleValue.FromDouble(18) }; 419 | ft.FontName = ftn; 420 | ft.FontSize = ftsz; 421 | ft.Bold = new Bold(); 422 | fts.AppendChild(ft); 423 | 424 | // font 3 425 | ft = new Font(); 426 | ftn = new FontName { Val = StringValue.FromString("Arial") }; 427 | ftsz = new FontSize { Val = DoubleValue.FromDouble(14) }; 428 | ft.FontName = ftn; 429 | ft.FontSize = ftsz; 430 | fts.AppendChild(ft); 431 | 432 | // font 4 433 | ft = new Font(); 434 | ftn = new FontName { Val = StringValue.FromString("Arial") }; 435 | ftsz = new FontSize { Val = DoubleValue.FromDouble(11) }; 436 | var fontColor = Color.MediumBlue; 437 | ft.Color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = HexBinaryValueFromColor(fontColor) }; 438 | ft.FontName = ftn; 439 | ft.FontSize = ftsz; 440 | fts.AppendChild(ft); 441 | 442 | fts.Count = UInt32Value.FromUInt32((uint) fts.ChildElements.Count); 443 | return fts; 444 | } 445 | 446 | private static HexBinaryValue HexBinaryValueFromColor(Color fillColor) 447 | { 448 | return new HexBinaryValue 449 | { 450 | Value = 451 | ColorTranslator.ToHtml( 452 | Color.FromArgb( 453 | fillColor.A, 454 | fillColor.R, 455 | fillColor.G, 456 | fillColor.B)).Replace("#", "") 457 | }; 458 | } 459 | } 460 | } -------------------------------------------------------------------------------- /OpenXmlUtils/Spreadsheet.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // 3 | // File: "Spreadsheet.cs" 4 | // Purpose: "Create xlxs spreadsheet files" 5 | // Author: "Geoplex" 6 | // 7 | #endregion 8 | 9 | #region (c) Copyright 2014 Geoplex 10 | // 11 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 12 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 13 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 14 | // 15 | // IN NO EVENT SHALL GEOPLEX BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 16 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 17 | // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE 18 | // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN 19 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 | // 21 | #endregion 22 | 23 | using System; 24 | using System.Collections.Generic; 25 | using System.Linq; 26 | using DocumentFormat.OpenXml; 27 | using DocumentFormat.OpenXml.Packaging; 28 | using DocumentFormat.OpenXml.Spreadsheet; 29 | 30 | namespace OpenXmlUtils 31 | { 32 | public class Spreadsheet 33 | { 34 | /// 35 | /// Write xlsx spreadsheet file of a list of T objects 36 | /// Maximum of 24 columns 37 | /// 38 | /// Type of objects passed in 39 | /// Full path filename for the new spreadsheet 40 | /// A sheet definition used to create the spreadsheet 41 | public static void Create( 42 | string fileName, 43 | SheetDefinition def) 44 | { 45 | // open a template workbook 46 | using (var myWorkbook = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)) 47 | { 48 | // create workbook part 49 | var workbookPart = myWorkbook.AddWorkbookPart(); 50 | 51 | // add stylesheet to workbook part 52 | var stylesPart = myWorkbook.WorkbookPart.AddNewPart(); 53 | Stylesheet styles = new CustomStylesheet(); 54 | styles.Save(stylesPart); 55 | 56 | // create workbook 57 | var workbook = new Workbook(); 58 | 59 | // add work sheet 60 | var sheets = new Sheets(); 61 | sheets.AppendChild(CreateSheet(1, def, workbookPart)); 62 | workbook.AppendChild(sheets); 63 | 64 | // add workbook to workbook part 65 | myWorkbook.WorkbookPart.Workbook = workbook; 66 | myWorkbook.WorkbookPart.Workbook.Save(); 67 | myWorkbook.Close(); 68 | } 69 | } 70 | 71 | /// 72 | /// Write xlsx spreadsheet file of a list of T objects 73 | /// Maximum of 24 columns 74 | /// 75 | /// Type of objects passed in 76 | /// Full path filename for the new spreadsheet 77 | /// A list of sheet definitions used to create the spreadsheet 78 | public static void Create( 79 | string fileName, 80 | IEnumerable> defs) 81 | { 82 | // open a template workbook 83 | using (var myWorkbook = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)) 84 | { 85 | // create workbook part 86 | var workbookPart = myWorkbook.AddWorkbookPart(); 87 | 88 | // add stylesheet to workbook part 89 | var stylesPart = myWorkbook.WorkbookPart.AddNewPart(); 90 | Stylesheet styles = new CustomStylesheet(); 91 | styles.Save(stylesPart); 92 | 93 | // create workbook 94 | var workbook = new Workbook(); 95 | 96 | // add work sheets 97 | var sheets = new Sheets(); 98 | var list = defs.ToList(); 99 | for (var i = 0; i < list.Count(); i++) 100 | { 101 | sheets.AppendChild(CreateSheet(i+1, list[i], workbookPart)); 102 | } 103 | workbook.AppendChild(sheets); 104 | 105 | // add workbook to workbook part 106 | myWorkbook.WorkbookPart.Workbook = workbook; 107 | myWorkbook.WorkbookPart.Workbook.Save(); 108 | myWorkbook.Close(); 109 | } 110 | } 111 | 112 | private static Sheet CreateSheet(int sheetIndex, SheetDefinition def, WorkbookPart workbookPart) 113 | { 114 | // create worksheet part 115 | var worksheetPart = workbookPart.AddNewPart(); 116 | var worksheetId = workbookPart.GetIdOfPart(worksheetPart); 117 | 118 | // variables 119 | var numCols = def.Fields.Count; 120 | var numRows = def.Objects.Count; 121 | var az = new List(Enumerable.Range('A', 'Z' - 'A' + 1).Select(i => (Char) i).ToArray()); 122 | var headerCols = az.GetRange(0, numCols); 123 | var hasTitleRow = def.Title != null; 124 | var hasSubtitleRow = def.SubTitle != null; 125 | var titleRowCount = hasTitleRow ? 1 + (hasSubtitleRow ? 1 : 0) : hasSubtitleRow ? 1 : 0; 126 | 127 | // get the worksheet data 128 | int firstTableRow; 129 | var sheetData = CreateSheetData(def.Objects, def.Fields, headerCols, def.IncludeTotalsRow, def.Title, 130 | def.SubTitle, 131 | out firstTableRow); 132 | 133 | // populate column metadata 134 | var columns = new Columns(); 135 | for (var col = 0; col < numCols; col++) 136 | { 137 | var width = ColumnWidth(sheetData, col, titleRowCount); 138 | columns.AppendChild(CreateColumnMetadata((UInt32) col + 1, (UInt32) numCols + 1, width)); 139 | } 140 | 141 | // populate worksheet 142 | var worksheet = new Worksheet(); 143 | worksheet.AppendChild(columns); 144 | worksheet.AppendChild(sheetData); 145 | 146 | // add an auto filter 147 | worksheet.AppendChild(new AutoFilter 148 | { 149 | Reference = 150 | String.Format("{0}{1}:{2}{3}", headerCols.First(), firstTableRow - 1, headerCols.Last(), 151 | numRows + titleRowCount + 1) 152 | }); 153 | 154 | // add worksheet to worksheet part 155 | worksheetPart.Worksheet = worksheet; 156 | worksheetPart.Worksheet.Save(); 157 | 158 | return new Sheet { Name = def.Name, SheetId = (UInt32)sheetIndex, Id = worksheetId }; 159 | } 160 | 161 | private static double ColumnWidth(SheetData sheetData, int col, int titleRowCount) 162 | { 163 | var rows = sheetData.ChildElements.ToList(); 164 | if (col == 0) 165 | { 166 | rows = sheetData.ChildElements.ToList().GetRange(titleRowCount, sheetData.ChildElements.Count - titleRowCount); 167 | } 168 | 169 | var maxLength = (from row in rows 170 | where row.ChildElements.Count > col 171 | select row.ChildElements[col] 172 | into cell 173 | where cell.GetType() != typeof (FormulaCell) 174 | select cell.InnerText.Length).Concat(new[] {0}).Max(); 175 | var width = maxLength*0.9 + 5; 176 | return width; 177 | } 178 | 179 | private static SheetData CreateSheetData(IList< T> objects, List fields, 180 | List headerCols, bool includedTotalsRow, string sheetTitle, string sheetSubTitle, 181 | out int firstTableRow) 182 | { 183 | var sheetData = new SheetData(); 184 | var fieldNames = fields.Select(f => f.Title).ToList(); 185 | var numCols = headerCols.Count; 186 | var rowIndex = 0; 187 | firstTableRow = 0; 188 | Row row; 189 | 190 | // create title 191 | if (sheetTitle != null) 192 | { 193 | rowIndex++; 194 | row = CreateTitle(sheetTitle, headerCols, ref rowIndex); 195 | sheetData.AppendChild(row); 196 | } 197 | 198 | // create subtitle 199 | if (sheetSubTitle != null) 200 | { 201 | rowIndex++; 202 | row = CreateSubTitle(sheetSubTitle, headerCols, ref rowIndex); 203 | sheetData.AppendChild(row); 204 | } 205 | 206 | // create the header 207 | rowIndex++; 208 | row = CreateHeader(fieldNames, headerCols, ref rowIndex); 209 | sheetData.AppendChild(row); 210 | 211 | if (objects.Count == 0) 212 | return sheetData; 213 | 214 | // create a row for each object and set the columns for each field 215 | firstTableRow = rowIndex + 1; 216 | CreateTable(objects, ref rowIndex, numCols, fields, headerCols, sheetData); 217 | 218 | // create an additional row with summed totals 219 | if (includedTotalsRow) 220 | { 221 | rowIndex++; 222 | AppendTotalsRow(objects, rowIndex, firstTableRow, numCols, fields, headerCols, sheetData); 223 | } 224 | 225 | return sheetData; 226 | } 227 | 228 | private static Row CreateTitle(string title, List headerCols, ref int rowIndex) 229 | { 230 | var header = new Row {RowIndex = (uint) rowIndex, Height = 40, CustomHeight = true}; 231 | var c = new TextCell(headerCols[0].ToString(), title, rowIndex) 232 | { 233 | StyleIndex = (UInt32)CustomStylesheet.CustomCellFormats.TitleText 234 | }; 235 | header.Append(c); 236 | 237 | return header; 238 | } 239 | 240 | private static Row CreateSubTitle(string title, List headerCols, ref int rowIndex) 241 | { 242 | var header = new Row { RowIndex = (uint)rowIndex, Height = 28, CustomHeight = true }; 243 | 244 | var c = new TextCell(headerCols[0].ToString(), title, rowIndex) 245 | { 246 | StyleIndex = (UInt32)CustomStylesheet.CustomCellFormats.SubtitleText 247 | }; 248 | header.Append(c); 249 | 250 | return header; 251 | } 252 | 253 | private static Row CreateHeader(IList headerNames, List headerCols, ref int rowIndex) 254 | { 255 | var header = new Row {RowIndex = (uint) rowIndex}; 256 | 257 | for (var col = 0; col < headerCols.Count; col++) 258 | { 259 | var c = new TextCell(headerCols[col].ToString(), headerNames[col], rowIndex) 260 | { 261 | StyleIndex = (UInt32) CustomStylesheet.CustomCellFormats.HeaderText 262 | }; 263 | header.Append(c); 264 | } 265 | return header; 266 | } 267 | 268 | private static void CreateTable(IList objects, ref int rowIndex, int numCols, 269 | List fields, List headers, SheetData sheetData, bool hidden=false, int outline=0) 270 | { 271 | // for each object 272 | foreach (var rowObj in objects) 273 | { 274 | // row group? 275 | var list = rowObj as IList; 276 | if (list != null) 277 | { 278 | CreateTable(list, ref rowIndex, numCols, fields, headers, sheetData, true, outline+1); 279 | continue; 280 | } 281 | 282 | rowIndex++; 283 | 284 | // create a row 285 | var row = new Row 286 | { 287 | RowIndex = (uint)rowIndex, 288 | Collapsed = new BooleanValue(false), 289 | OutlineLevel = new ByteValue((byte)outline), 290 | Hidden = new BooleanValue(hidden) 291 | }; 292 | 293 | int col; 294 | 295 | // populate columns using supplied objects 296 | for (col = 0; col < numCols; col++) 297 | { 298 | var field = fields[col]; 299 | var columnObj = GetColumnObject(field.FieldName, rowObj); 300 | if (columnObj == null || columnObj == DBNull.Value) continue; 301 | 302 | Cell cell; 303 | 304 | if (field.GetType() == typeof (HyperlinkField)) 305 | { 306 | var displayColumnObj = GetColumnObject(((HyperlinkField)field).DisplayFieldName, rowObj); 307 | cell = CreateHyperlinkCell(rowIndex, headers, columnObj, displayColumnObj, col); 308 | } 309 | else if (field.GetType() == typeof(DecimalNumberField)) 310 | { 311 | cell = CreateDecimalNumberCell(rowIndex, headers, columnObj, ((DecimalNumberField)field).DecimalPlaces, col); 312 | } 313 | else 314 | { 315 | cell = CreateCell(rowIndex, headers, columnObj, col); 316 | } 317 | 318 | row.AppendChild(cell); 319 | 320 | } // for each column 321 | 322 | sheetData.AppendChild(row); 323 | } 324 | } 325 | 326 | private static Cell CreateHyperlinkCell(int rowIndex, List headers, object columnObj, object displayColumnObj, int col) 327 | { 328 | return new FormulaCell(headers[col].ToString(), 329 | String.Format(@"HYPERLINK(""{0}"", ""{1}"")", columnObj, displayColumnObj), rowIndex) 330 | { 331 | StyleIndex = (UInt32) CustomStylesheet.CustomCellFormats.Hyperlink 332 | }; 333 | } 334 | 335 | private static Cell CreateDecimalNumberCell(int rowIndex, List headers, object columnObj, int decimalPlaces, int col) 336 | { 337 | // TODO: decimal places other than 5 338 | return new NumberCell(headers[col].ToString(), columnObj.ToString(), rowIndex) 339 | { 340 | StyleIndex = (UInt32)CustomStylesheet.CustomCellFormats.DefaultNumber5DecimalPlace 341 | }; 342 | } 343 | 344 | private static Cell CreateCell(int rowIndex, List headers, object columnObj, int col) 345 | { 346 | Cell cell; 347 | if (columnObj is string) 348 | { 349 | cell = new TextCell(headers[col].ToString(), columnObj.ToString(), rowIndex); 350 | } 351 | else if (columnObj is bool) 352 | { 353 | var value = (bool) columnObj ? "Yes" : "No"; 354 | cell = new TextCell(headers[col].ToString(), value, rowIndex); 355 | } 356 | else if (columnObj is DateTime) 357 | { 358 | cell = new DateCell(headers[col].ToString(), (DateTime) columnObj, rowIndex); 359 | } 360 | else if (columnObj is TimeSpan) 361 | { 362 | var ts = (TimeSpan) columnObj; 363 | // excel stores time as "fraction of hours in a day" 364 | cell = new NumberCell(headers[col].ToString(), (ts.TotalHours/24).ToString(), rowIndex) 365 | { 366 | StyleIndex = (UInt32) CustomStylesheet.CustomCellFormats.Duration 367 | }; 368 | } 369 | else if (columnObj is decimal || columnObj is double) 370 | { 371 | cell = new NumberCell(headers[col].ToString(), columnObj.ToString(), rowIndex) 372 | { 373 | StyleIndex = (UInt32) CustomStylesheet.CustomCellFormats.DefaultNumber2DecimalPlace 374 | }; 375 | } 376 | else 377 | { 378 | long value; 379 | if (long.TryParse(columnObj.ToString(), out value)) 380 | { 381 | cell = new NumberCell(headers[col].ToString(), columnObj.ToString(), rowIndex); 382 | } 383 | else 384 | { 385 | cell = new TextCell(headers[col].ToString(), columnObj.ToString(), rowIndex); 386 | } 387 | } 388 | return cell; 389 | } 390 | 391 | private static object GetColumnObject(string fieldName, T rowObj) 392 | { 393 | // is the object a dictionary? 394 | var dict = rowObj as IDictionary; 395 | if (dict != null) 396 | { 397 | object value; 398 | return dict.TryGetValue(fieldName, out value) ? value : null; 399 | } 400 | 401 | // get the properties for this object type 402 | var properties = GetPropertyInfo(); 403 | if (!properties.Contains(fieldName)) 404 | return null; 405 | 406 | var myf = rowObj.GetType().GetProperty(fieldName); 407 | if (myf == null) 408 | return null; 409 | 410 | var obj = myf.GetValue(rowObj, null); 411 | return obj; 412 | } 413 | 414 | private static void AppendTotalsRow(IList objects, int rowIndex, int firstTableRow, int numCols, 415 | List fields, 416 | List headers, 417 | SheetData sheetData) 418 | { 419 | var fieldNames = fields.Select(f => f.FieldName).ToList(); 420 | var rowObj = objects[0]; 421 | var total = new Row {RowIndex = (uint) rowIndex}; 422 | 423 | for (var col = 0; col < numCols; col++) 424 | { 425 | var field = fields[col]; 426 | if (field.IgnoreFromTotals) 427 | { 428 | total.AppendChild(new TextCell(headers[col].ToString(), string.Empty, rowIndex) 429 | { 430 | StyleIndex = (UInt32)CustomStylesheet.CustomCellFormats.TotalsText 431 | }); 432 | continue; 433 | } 434 | 435 | var columnObject = GetColumnObject(fieldNames[col], rowObj); 436 | 437 | // look through objects until we have a value for this column 438 | var row = 0; 439 | while (columnObject == null || columnObject == DBNull.Value) 440 | { 441 | if (objects.Count <= ++row) 442 | break; 443 | columnObject = GetColumnObject(fieldNames[col], objects[row]); 444 | } 445 | 446 | if (field.CountNoneNullRowsForTotal) 447 | { 448 | total.AppendChild(CreateRowTotalFomulaCell(rowIndex, firstTableRow, headers, col, 449 | (UInt32)CustomStylesheet.CustomCellFormats.TotalsNumber, true)); 450 | } 451 | 452 | if (col == 0) 453 | { 454 | total.AppendChild(new TextCell(headers[col].ToString(), "Total", rowIndex) 455 | { 456 | StyleIndex = (UInt32) CustomStylesheet.CustomCellFormats.TotalsText 457 | }); 458 | } 459 | else if (columnObject is decimal || columnObject is double) 460 | { 461 | total.AppendChild(CreateRowTotalFomulaCell(rowIndex, firstTableRow, headers, col, 462 | (UInt32) CustomStylesheet.CustomCellFormats.TotalsNumber2DecimalPlace)); 463 | } 464 | else if (columnObject is TimeSpan) 465 | { 466 | total.AppendChild(CreateRowTotalFomulaCell(rowIndex, firstTableRow, headers, col, 467 | (UInt32)CustomStylesheet.CustomCellFormats.TotalsDuration)); 468 | } 469 | else 470 | { 471 | long value; 472 | if (columnObject != null && 473 | long.TryParse(columnObject.ToString(), out value)) 474 | { 475 | total.AppendChild(CreateRowTotalFomulaCell(rowIndex, firstTableRow, headers, col, 476 | (UInt32) CustomStylesheet.CustomCellFormats.TotalsNumber)); 477 | } 478 | else 479 | { 480 | total.AppendChild(new TextCell(headers[col].ToString(), string.Empty, rowIndex) 481 | { 482 | StyleIndex = (UInt32) CustomStylesheet.CustomCellFormats.TotalsText 483 | }); 484 | } 485 | } 486 | } // for each column 487 | sheetData.AppendChild(total); 488 | } 489 | 490 | private static FormulaCell CreateRowTotalFomulaCell(int rowIndex, int firstTableRow, List headers, int col, UInt32 styleIndex, bool countNonBlank = false) 491 | { 492 | var headerCol = headers[col].ToString(); 493 | var firstRow = headerCol + firstTableRow; 494 | var lastRow = headerCol + (rowIndex - 1); 495 | return CreateFormulaCell(rowIndex, headers, col, styleIndex, countNonBlank, firstRow, lastRow); 496 | } 497 | 498 | private static FormulaCell CreateFormulaCell(int rowIndex, List headers, int col, uint styleIndex, 499 | bool countNonBlank, string firstCell, string lastCell) 500 | { 501 | var formula = (countNonBlank ? "COUNTA" : "SUM") + "(" + firstCell + ":" + lastCell + ")"; 502 | return new FormulaCell(headers[col].ToString(), formula, rowIndex) {StyleIndex = styleIndex}; 503 | } 504 | 505 | private static List GetPropertyInfo() 506 | { 507 | var propertyInfos = typeof (T).GetProperties(); 508 | return propertyInfos.Select(propertyInfo => propertyInfo.Name).ToList(); 509 | } 510 | 511 | private static Column CreateColumnMetadata(UInt32 startColumnIndex, UInt32 endColumnIndex, double width) 512 | { 513 | var column = new Column 514 | { 515 | Min = startColumnIndex, 516 | Max = endColumnIndex, 517 | BestFit = true, 518 | Width = width, 519 | }; 520 | return column; 521 | } 522 | } 523 | } --------------------------------------------------------------------------------