Skip to content

Commit dad26a0

Browse files
author
Daniel Peichev
committedSep 29, 2016
Add strongly typed column model
1 parent 6dbaa83 commit dad26a0

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
*.tmp
2+
*.gpState
3+
*.pdb
4+
*.suo
5+
*.swp
6+
*.user
7+
*.mdb
8+
*.suo
9+
obj/
10+
bin/
11+
packages/
12+
.DS_Store
13+
.vs/
14+
Scripts/
15+
Content/dataviz/
16+
Content/mobile/
17+
Content/shared/
18+
Content/web/
19+
Content/*.json

‎grid/grid-dpl-integration/GridExcelSpreadProcessing/Controllers/HomeController.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv;
77
using Telerik.Windows.Documents.Spreadsheet.Model;
88
using Newtonsoft.Json;
9+
using ExportHelpers;
10+
using System.Collections.Generic;
911

1012
namespace GridExcelSpreadProcessing.Controllers
1113
{
@@ -26,22 +28,22 @@ public JsonResult Export(string model, string data, string format, string title)
2628
Worksheet worksheet = workbook.ActiveWorksheet;
2729
worksheet.Name = title;
2830

29-
var modelObject = JsonConvert.DeserializeObject<dynamic>(model);
31+
var modelObject = JsonConvert.DeserializeObject<IList<ColumnSettings>>(model);
3032
var dataObject = JsonConvert.DeserializeObject<dynamic>(data);
3133
var columnCount = modelObject.Count;
3234

3335
for (int idx = 0; idx < columnCount; idx++)
3436
{
3537
var modelCol = modelObject[idx];
36-
string columnName = modelCol.title ?? modelCol.field;
38+
string columnName = modelCol.Title ?? modelCol.Field;
3739
worksheet.Cells[0, idx].SetValue(columnName);
3840
}
3941

4042
for (int rowIdx = 1; rowIdx < dataObject.Count; rowIdx++)
4143
{
4244
for (int colIdx = 0; colIdx < modelObject.Count; colIdx++)
4345
{
44-
worksheet.Cells[rowIdx, colIdx].SetValue(dataObject[rowIdx - 1][modelObject[colIdx].field.ToString()].ToString());
46+
worksheet.Cells[rowIdx, colIdx].SetValue(dataObject[rowIdx - 1][modelObject[colIdx].Field].Value);
4547
}
4648
}
4749

‎grid/grid-dpl-integration/GridExcelSpreadProcessing/Controllers/StreamController.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Web.Mvc;
44
using Telerik.Documents.SpreadsheetStreaming;
55
using Newtonsoft.Json;
6+
using ExportHelpers;
7+
using System.Collections.Generic;
68

79
namespace GridExcelSpreadProcessing.Controllers
810
{
@@ -15,7 +17,7 @@ public ActionResult Index()
1517

1618
public JsonResult Export(string model, string data, string format, string title)
1719
{
18-
var modelObject = JsonConvert.DeserializeObject<dynamic>(model);
20+
var modelObject = JsonConvert.DeserializeObject<IList<ColumnSettings>>(model);
1921
var dataObject = JsonConvert.DeserializeObject<dynamic>(data);
2022

2123
SpreadDocumentFormat exportFormat = format == "CSV" ? SpreadDocumentFormat.Csv : SpreadDocumentFormat.Xlsx;
@@ -30,7 +32,7 @@ public JsonResult Export(string model, string data, string format, string title)
3032
for (int idx = 0; idx < modelObject.Count; idx++)
3133
{
3234
var modelCol = modelObject[idx];
33-
string columnName = modelCol.title ?? modelCol.field;
35+
string columnName = modelCol.Title ?? modelCol.Field;
3436
using (ICellExporter cell = row.CreateCellExporter())
3537
{
3638
cell.SetValue(columnName);
@@ -45,7 +47,7 @@ public JsonResult Export(string model, string data, string format, string title)
4547
{
4648
using (ICellExporter cell = row.CreateCellExporter())
4749
{
48-
cell.SetValue(dataObject[rowIdx][modelObject[colIdx].field.ToString()].ToString());
50+
cell.SetValue(dataObject[rowIdx][modelObject[colIdx].Field].Value);
4951
}
5052
}
5153
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace ExportHelpers
2+
{
3+
internal class ColumnSettings
4+
{
5+
public string Title { get; set; }
6+
public string Width { get; set; }
7+
public string Field { get; set; }
8+
public string Format { get; set; }
9+
public bool Hidden { get; set; }
10+
}
11+
}

‎grid/grid-dpl-integration/GridExcelSpreadProcessing/GridExcelSpreadProcessing.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<Compile Include="App_Start\RouteConfig.cs" />
136136
<Compile Include="Controllers\StreamController.cs" />
137137
<Compile Include="Controllers\HomeController.cs" />
138+
<Compile Include="ExportHelpers\ColumnSettings.cs" />
138139
<Compile Include="Global.asax.cs">
139140
<DependentUpon>Global.asax</DependentUpon>
140141
</Compile>

0 commit comments

Comments
 (0)
Please sign in to comment.