Skip to content

Commit 702a16d

Browse files
committed
add project files
1 parent 26a04f2 commit 702a16d

File tree

11 files changed

+619
-1
lines changed

11 files changed

+619
-1
lines changed

CS/SpreadsheetApiDataBinding.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35825.156 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpreadsheetApiDataBinding", "SpreadsheetApiDataBinding\SpreadsheetApiDataBinding.csproj", "{CCAD0A23-980B-4138-A1D0-B9521863B6E0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {A42EF82C-956E-4A03-8287-BB29C2C29492}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using DevExpress.Spreadsheet;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SpreadsheetApiDataBinding
9+
{
10+
public class MyWeatherConverter : IBindingRangeValueConverter
11+
{
12+
public object ConvertToObject(CellValue value, Type requiredType, int columnIndex)
13+
{
14+
if (requiredType == typeof(DateTime))
15+
return value.DateTimeValue;
16+
if (requiredType == typeof(Weather))
17+
{
18+
if (requiredType == typeof(Weather))
19+
{
20+
Weather w;
21+
if (Enum.TryParse(value.TextValue, out w)) return w;
22+
return Weather.Undefined;
23+
}
24+
else
25+
return value.TextValue;
26+
}
27+
if (requiredType == typeof(List<HourlyReport>))
28+
return new List<HourlyReport>();
29+
return value.TextValue;
30+
}
31+
public CellValue TryConvertFromObject(object value)
32+
{
33+
if (value is DateTime)
34+
{
35+
return ((DateTime)value).ToString("MMM-dd");
36+
}
37+
if (value is Weather)
38+
{
39+
return value.ToString();
40+
}
41+
if (value is List<HourlyReport>)
42+
{
43+
var hourly = (List<HourlyReport>)value;
44+
if (hourly.Count == 0) return "Undefined";
45+
var high = hourly
46+
.OrderByDescending(p => p.Temperature)
47+
.FirstOrDefault()
48+
.Temperature;
49+
var low = hourly
50+
.OrderBy(p => p.Temperature)
51+
.FirstOrDefault()
52+
.Temperature;
53+
return String.Format("High - {0}, Low - {1}", high, low);
54+
}
55+
56+
return CellValue.TryCreateFromObject(value);
57+
}
58+
}
59+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using DevExpress.Spreadsheet;
2+
using System.Diagnostics;
3+
4+
namespace SpreadsheetApiDataBinding
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
Workbook workbook = new Workbook();
11+
workbook.CreateNewDocument();
12+
13+
// Bind a first worksheet to a BindingList
14+
BindWeatherReportToRange(MyWeatherReportSource.DataAsBindingList, workbook.Worksheets[0]);
15+
16+
// Bind a second worksheet to a List
17+
workbook.Worksheets.Add();
18+
BindWeatherReportToTable(MyWeatherReportSource.Data, workbook.Worksheets[1]);
19+
20+
// Bind a data source to the fixed table on the third worksheet
21+
workbook.Worksheets.Add();
22+
BindWeatherReportToFixedTable(MyWeatherReportSource.Data, workbook.Worksheets[2]);
23+
workbook.Worksheets[0].DataBindings.Error += DataBindings_Error;
24+
workbook.SaveDocument("DataBindings.xlsx");
25+
Process.Start(new ProcessStartInfo("DataBindings.xlsx") { UseShellExecute = true });
26+
}
27+
28+
private static void DataBindings_Error(object sender, DataBindingErrorEventArgs e)
29+
{
30+
Console.WriteLine(String.Format("Error at worksheet.Rows[{0}].\n The error is : {1}", e.RowIndex, e.ErrorType.ToString()), "Binding Error");
31+
}
32+
33+
private static void BindWeatherReportToRange(object weatherDatasource, Worksheet worksheet)
34+
{
35+
// Check for range conflicts.
36+
CellRange bindingRange = worksheet.Range["A1:C5"];
37+
var dataBindingConflicts = worksheet.DataBindings.
38+
Where(binding => (binding.Range.RightColumnIndex >= bindingRange.LeftColumnIndex)
39+
|| (binding.Range.BottomRowIndex >= bindingRange.TopRowIndex));
40+
if (dataBindingConflicts.Count() > 0)
41+
{
42+
Console.WriteLine("Cannot bind the range to data.\r\nThe worksheet contains other binding ranges which may conflict.", "Range Conflict");
43+
return;
44+
}
45+
46+
// Specify the binding options.
47+
ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
48+
dsOptions.ImportHeaders = true;
49+
dsOptions.CellValueConverter = new MyWeatherConverter();
50+
dsOptions.SkipHiddenRows = true;
51+
52+
// Bind the data source to the worksheet range.
53+
WorksheetDataBinding sheetDataBinding = worksheet.DataBindings.BindToDataSource(weatherDatasource, bindingRange, dsOptions);
54+
55+
// Adjust the column width.
56+
sheetDataBinding.Range.AutoFitColumns();
57+
}
58+
59+
private static void BindWeatherReportToTable(object weatherDatasource, Worksheet worksheet)
60+
{
61+
CellRange bindingRange = worksheet["A1:C5"];
62+
// Remove all data bindings bound to the specified data source.
63+
worksheet.DataBindings.Remove(weatherDatasource);
64+
65+
// Specify the binding options.
66+
ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
67+
dsOptions.ImportHeaders = true;
68+
dsOptions.CellValueConverter = new MyWeatherConverter();
69+
dsOptions.SkipHiddenRows = true;
70+
71+
// Create a table and bind the data source to the table.
72+
try
73+
{
74+
WorksheetTableDataBinding sheetDataBinding = worksheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions);
75+
sheetDataBinding.Table.Style = worksheet.Workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium14];
76+
77+
// Adjust the column width.
78+
sheetDataBinding.Range.AutoFitColumns();
79+
}
80+
catch (Exception e)
81+
{
82+
Console.WriteLine(e.Message, "Binding Exception");
83+
}
84+
}
85+
86+
private static void BindWeatherReportToFixedTable(object weatherDatasource, Worksheet worksheet)
87+
{
88+
// Remove all data bindings bound to the specified data source.
89+
worksheet.DataBindings.Remove(weatherDatasource);
90+
91+
CellRange bindingRange = worksheet["A1:C5"];
92+
93+
// Specify the binding options.
94+
ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
95+
dsOptions.ImportHeaders = true;
96+
dsOptions.CellValueConverter = new MyWeatherConverter();
97+
dsOptions.SkipHiddenRows = true;
98+
99+
// Create a table and bind the data source to the table.
100+
try
101+
{
102+
Table boundTable = worksheet.Tables.Add(weatherDatasource, bindingRange, dsOptions);
103+
boundTable.Style = worksheet.Workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium15];
104+
105+
// Adjust the column width.
106+
boundTable.Range.AutoFitColumns();
107+
}
108+
catch (Exception e)
109+
{
110+
Console.WriteLine(e.Message, "Binding Exception");
111+
}
112+
}
113+
}
114+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="DevExpress.Document.Processor" Version="24.2.5" />
12+
<PackageReference Include="DevExpress.Drawing.Skia" Version="24.2.5" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System.ComponentModel;
2+
3+
namespace SpreadsheetApiDataBinding
4+
{
5+
public class WeatherReport
6+
{
7+
[DisplayName("Date")]
8+
public DateTime Date { get; set; }
9+
[DisplayName("Weather Condition")]
10+
public Weather Weather { get; set; }
11+
[DisplayName("Max and Min Temperature")]
12+
public List<HourlyReport> HourlyReport { get; set; }
13+
}
14+
15+
public class HourlyReport
16+
{
17+
public int Hour { get; set; }
18+
public int Temperature { get; set; }
19+
}
20+
21+
public enum Weather
22+
{
23+
Sunny,
24+
Cloudy,
25+
Windy,
26+
Gloomy,
27+
Foggy,
28+
Misty,
29+
Rainy,
30+
Undefined
31+
}
32+
public static class MyWeatherReportSource
33+
{
34+
private static Random rand = new System.Random();
35+
static List<WeatherReport> data;
36+
static BindingList<WeatherReport> dataBindingList;
37+
38+
public static List<WeatherReport> Data
39+
{
40+
get
41+
{
42+
if (data == null)
43+
{
44+
data = GetReport();
45+
}
46+
return data;
47+
}
48+
}
49+
public static BindingList<WeatherReport> DataAsBindingList
50+
{
51+
get
52+
{
53+
if (dataBindingList == null)
54+
{
55+
dataBindingList = new BindingList<WeatherReport>(Data);
56+
}
57+
return dataBindingList;
58+
}
59+
}
60+
public static List<WeatherReport> GetReport()
61+
{
62+
var report = new List<WeatherReport>();
63+
64+
report.Add(new WeatherReport()
65+
{
66+
Date = DateTime.Today,
67+
Weather = Weather.Rainy,
68+
HourlyReport = GenerateRandomHourlyReport()
69+
});
70+
71+
report.Add(new WeatherReport()
72+
{
73+
Date = DateTime.Today.AddDays(-1),
74+
Weather = Weather.Cloudy,
75+
HourlyReport = GenerateRandomHourlyReport()
76+
});
77+
78+
report.Add(new WeatherReport()
79+
{
80+
Date = DateTime.Today.AddDays(-2),
81+
Weather = Weather.Sunny,
82+
HourlyReport = GenerateRandomHourlyReport()
83+
});
84+
85+
report.Add(new WeatherReport()
86+
{
87+
Date = DateTime.Today.AddDays(-3),
88+
Weather = Weather.Gloomy,
89+
HourlyReport = GenerateRandomHourlyReport()
90+
});
91+
return report;
92+
}
93+
public static List<HourlyReport> GenerateRandomHourlyReport()
94+
{
95+
var report = new List<HourlyReport>();
96+
97+
for (int i = 0; i < 24; i++)
98+
{
99+
var hourlyReport = new HourlyReport();
100+
hourlyReport.Hour = i;
101+
hourlyReport.Temperature = rand.Next(30);
102+
report.Add(hourlyReport);
103+
}
104+
return report;
105+
}
106+
public static void Reload()
107+
{
108+
data = GetReport();
109+
}
110+
}
111+
}

VB/SpreadsheetApiDataBinding.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.13.35825.156 d17.13
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "SpreadsheetApiDataBinding", "SpreadsheetApiDataBinding\SpreadsheetApiDataBinding.vbproj", "{CCAD0A23-980B-4138-A1D0-B9521863B6E0}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{CCAD0A23-980B-4138-A1D0-B9521863B6E0}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {A42EF82C-956E-4A03-8287-BB29C2C29492}
23+
EndGlobalSection
24+
EndGlobal

0 commit comments

Comments
 (0)