diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj b/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj index 4fe60b4..9f6d804 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj @@ -1,5 +1,5 @@  - + Debug x86 @@ -10,7 +10,7 @@ Properties ClosedXML_Sandbox ClosedXML_Sandbox - v4.0 + v4.5.1 512 @@ -30,6 +30,7 @@ DEBUG;TRACE prompt 4 + false AnyCPU @@ -39,6 +40,7 @@ TRACE prompt 4 + false true @@ -55,6 +57,7 @@ true ;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules true + false bin\x64\Release\ @@ -71,6 +74,7 @@ true ;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules true + false true @@ -87,6 +91,7 @@ true ;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules false + false bin\Release\ @@ -103,12 +108,14 @@ true ;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules true + false True + @@ -119,8 +126,10 @@ + + diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/OneRow.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/OneRow.cs new file mode 100644 index 0000000..0dde282 --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/OneRow.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.ComponentModel.DataAnnotations; + +namespace ClosedXML_Sandbox +{ + public class OneRow + { + [Display(Name = "Col01")] + public int Col01 { get; set; } + + [Display(Name = "Col02")] + public string Col02 { get; set; } + + [Display(Name = "Col03")] + public DateTime? Col03 { get; set; } + + [Display(Name = "Col04")] + public TimeSpan? Col04 { get; set; } + + [Display(Name = "Col05")] + public TimeSpan? Col05 { get; set; } + + [Display(Name = "Col06")] + public TimeSpan? Col06 { get; set; } + + [Display(Name = "Col07")] + public TimeSpan? Col07 { get; set; } + + [Display(Name = "Col08")] + public TimeSpan? Col08 { get; set; } + + [Display(Name = "Col09")] + public string Col09 { get; set; } + + [Display(Name = "Col10")] + public decimal Col10 { get; set; } + + [Display(Name = "Col11")] + public string Col11 { get; set; } + + [Display(Name = "Col12")] + public bool Col12 { get; set; } + + [Display(Name = "Col13")] + public bool Col13 { get; set; } + + [Display(Name = "Col14")] + public string Col14 { get; set; } + + [Display(Name = "Col15")] + public bool Col15 { get; set; } + + [Display(Name = "Col16")] + public string Col16 { get; set; } + + [Display(Name = "Col17")] + public string Col17 { get; set; } + + [Display(Name = "Col18")] + public DateTime? Col18 { get; set; } + + [Display(Name = "Col19")] + public string Col19 { get; set; } + + [Display(Name = "Col20")] + public string Col20 { get; set; } + + [Display(Name = "Col21")] + public string Col21 { get; set; } + + [Display(Name = "Col22")] + public string Col22 { get; set; } + + [Display(Name = "Col23")] + public string Col23 { get; set; } + + [Display(Name = "Col24")] + public bool Col24 { get; set; } + + [Display(Name = "Col25")] + public int? Col25 { get; set; } + + [Display(Name = "Col26")] + public string Col26 { get; set; } + } +} \ No newline at end of file diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/PerformanceRunner.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/PerformanceRunner.cs new file mode 100644 index 0000000..97d3a31 --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/PerformanceRunner.cs @@ -0,0 +1,128 @@ +using ClosedXML.Excel; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; + +namespace ClosedXML_Sandbox +{ + internal class PerformanceRunner + { + public static void TimeAction(Action action) + { + var stopwatch = Stopwatch.StartNew(); + action(); + Console.WriteLine("Action done in " + stopwatch.Elapsed); + } + + const int rowCount = 5000; + public static void RunInsertTable() + { + var rows = new List(); + + for (int i = 0; i < rowCount; i++) + { + var row = GenerateRow(); + rows.Add(row); + } + + var workbook = new XLWorkbook(); + var worksheet = workbook.Worksheets.Add("Sheet 1"); + worksheet.Cell(1, 1).InsertTable(rows); + + CreateMergedCell(worksheet); + + worksheet.Columns().AdjustToContents(); + + EmulateSave(workbook); + } + + private static void CreateMergedCell(IXLWorksheet worksheet) + { + worksheet.Cell(rowCount + 2, 1).Value = "Merged cell"; + var range = worksheet.Range(rowCount + 2, 1, rowCount + 2, 2); + range.Row(1).Merge(); + } + + private static void EmulateSave(XLWorkbook workbook) + { + using (MemoryStream memoryStream = new MemoryStream()) + { + workbook.SaveAs(memoryStream); + memoryStream.Seek(0, SeekOrigin.Begin); + Console.WriteLine("Total bytes = " + memoryStream.ToArray().Length); + } + } + + private static Random rnd = new Random(); + + private static T GenerateRow() where T : new() + { + var row = new T(); + + var rowProps = row.GetType().GetProperties(); + + var strings = rowProps.Where(p => p.PropertyType == typeof(string)); + var decimals = rowProps.Where(p => p.PropertyType == typeof(decimal)); + var ints = rowProps.Where(p => p.PropertyType == typeof(int) || p.PropertyType == typeof(int?)); + var dates = rowProps.Where(p => p.PropertyType == typeof(DateTime?)); + var timeSpans = rowProps.Where(p => p.PropertyType == typeof(TimeSpan?)); + var booleans = rowProps.Where(p => p.PropertyType == typeof(bool)); + + // Format strings + var tmpString = new StringBuilder(); + var tmpStringLength = rnd.Next(5, 50); + for (int x = 0; x <= tmpStringLength; x++) + { + tmpString.Append((char)(rnd.Next(48, 120))); + } + foreach (var str in strings) + { + str.SetValue(row, tmpString.ToString()); + } + + // Format decimals + var tmpDec = (decimal)(rnd.Next(-10000, 100000) / (Math.Pow(10.0, rnd.Next(1, 4)))); + + foreach (var dec in decimals) + { + dec.SetValue(row, tmpDec); + } + + // Format ints + var tmpInt = rnd.Next(-1000, 10000); + + foreach (var intValue in ints) + { + intValue.SetValue(row, tmpInt); + } + + // Format dates + var tmpDate = new DateTime(2012, 1, 1, 1, 1, 1); + tmpDate = tmpDate.AddSeconds(rnd.Next(-10000, 100000)); + foreach (var dt in dates) + { + dt.SetValue(row, tmpDate); + } + + // Format timespans + var tmpTimespan = new TimeSpan(rnd.Next(1, 24), rnd.Next(1, 60), rnd.Next(1, 60)); + + foreach (var ts in timeSpans) + { + ts.SetValue(row, tmpTimespan); + } + + // Format booleans + var tmpBool = (rnd.Next(0, 2) > 0); + foreach (var bl in booleans) + { + bl.SetValue(row, tmpBool); + } + + return row; + } + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs index abacd10..f01eb1d 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs @@ -15,9 +15,13 @@ { private static void Main(string[] args) { - var wb = new XLWorkbook(); - wb.SaveAs(@"c:\temp\saved.xlsx"); - Console.WriteLine("Done"); + //var wb = new XLWorkbook(); + //wb.SaveAs(@"c:\temp\saved.xlsx"); + //Console.WriteLine("Done"); + PerformanceRunner.TimeAction(PerformanceRunner.RunInsertTable); + + Console.WriteLine("Press any key to continue"); + Console.ReadKey(); } } } diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/app.config b/ClosedXML/ClosedXML/ClosedXML_Sandbox/app.config index cb2586b..22c902a 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/app.config +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/app.config @@ -1,3 +1,3 @@ - +