private FlatDataSource CreateDataSource(Stream fileStream)
{
FlatDataSource flatDataSource = new FlatDataSource();
ExcelDataConnectionSettings excelDataConnectionSettings =
new ExcelDataConnectionSettings
{
FileStream = fileStream,
GeneratedTypeName = "ExcelData",
WorksheetName = "Sheet1"
};
string excelStringFormat = "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)";
string systemValueFormat = "{0:C3}";
ExcelDataConnectionSettings.AddExcelCellFormatMapping(
excelStringFormat, systemValueFormat);
flatDataSource.Cube = DataSourceBase.GenerateInitialCube("ExcelData");
//the following three lines select the
//row and column hierarchies
//and also the measures. Use your own names
//in order for this to work
flatDataSource.Rows =
DataSourceBase.GenerateInitialItems("[Region].[Region]");
flatDataSource.Columns =
DataSourceBase.GenerateInitialItems("[Date].[Date]");
flatDataSource.Measures =
DataSourceBase.GenerateInitialItems("Units");
//you could also skip them and use the data selector
flatDataSource.ConnectionSettings = excelDataConnectionSettings;
flatDataSource.CubesSettings.Add(
new CubeMetadata
{
DataTypeFullName = "ExcelData",
DisplayName = "Sheet1 Data"
});
HierarchyDescriptor stringDataDescriptor =
new HierarchyDescriptor
{
AppliesToPropertiesOfType = typeof(string)
};
stringDataDescriptor.AddLevel<string>(s => "All","All values");
stringDataDescriptor.AddLevel<string>(s => s, "Members");
HierarchyDescriptor dateTimeDescriptor =
new HierarchyDescriptor
{
AppliesToPropertiesOfType = typeof(DateTime)
};
dateTimeDescriptor.AddLevel<DateTime>(date => "All Dates", "All Values");
dateTimeDescriptor.AddLevel<DateTime>(date => date.Year, "Years");
dateTimeDescriptor.AddLevel<DateTime>(date => date.Date, "Members");
flatDataSource.HierarchyDescriptors.Add(stringDataDescriptor);
flatDataSource.HierarchyDescriptors.Add(dateTimeDescriptor);
return flatDataSource;
}