从SQL查询生成和导出分层数据

从SQL查询生成和导出分层数据,sql,vb.net,visual-studio,Sql,Vb.net,Visual Studio,我需要按会计年度从sql server数据库中查询9年的数据,然后根据以下要求将数据分解为excel文件 Excel工作表->位置 Excel选项卡->商品 章节->运输方法 行->客户详细信息 该行将需要以下信息: 客户名称,FY14蒲式耳,FY14排名,FY13蒲式耳,FY13 排名……等 我已经能够将我的信息构建到我所称的locationdetail级别 Public Class LocationDetail Public Sub New(ByVal commodityid As

我需要按会计年度从sql server数据库中查询9年的数据,然后根据以下要求将数据分解为excel文件

Excel工作表->位置

Excel选项卡->商品

章节->运输方法

行->客户详细信息

该行将需要以下信息:

客户名称,FY14蒲式耳,FY14排名,FY13蒲式耳,FY13 排名……等

我已经能够将我的信息构建到我所称的locationdetail级别

Public Class LocationDetail

    Public Sub New(ByVal commodityid As String, ByVal commodityname As String, ByVal fiscalyear As String, ByVal shipmode As String, customerid As String, ByVal customername As String, ByVal quantityinlbs As Single, ByVal bushels As Single)

        Me.CommodityId = commodityid
        Me.CommodityName=commodityname
        Me.FiscalYear=fiscalyear
        Me.ShipMode=shipmode
        Me.CustomerId=customerid
        Me.CustomerName=customername
        Me.QuantityInLbs=quantityinlbs
        Me.Bushels=bushels

    End Sub


#Region " CommodityId "

    Private _commodityId As String
    Public Property CommodityId() As String
        Get
            Return _commodityId
        End Get
        Set(ByVal value As String)
            _commodityId = value
        End Set
    End Property

#End Region

#Region " CommodityName "

    Private _commodityName As String

    Public Property CommodityName() As String
        Get
            Return _commodityName
        End Get
        Set(ByVal value As String)
            _commodityName = value
        End Set
    End Property

#End Region

#Region " CommodityCollection "

    Private _commodityCollection As ICollection(Of Commodity)

    Public Property CommodityCollection() As ICollection(Of Commodity)
        Get
            Return _commodityCollection
        End Get
        Set(ByVal value As ICollection(Of Commodity))
            _commodityCollection = value
        End Set
    End Property

#End Region

#Region " FiscalYear "

    Private _fiscalYear As String

    Public Property FiscalYear() As String
        Get
            Return _fiscalYear
        End Get
        Set(ByVal value As String)
            _fiscalYear = value
        End Set
    End Property

#End Region

#Region " ShipMode "

    Private _shipMode As String

    Public Property ShipMode() As String
        Get
            Return _shipMode
        End Get
        Set(ByVal value As String)
            _shipMode = value
        End Set
    End Property

#End Region

#Region " CustomerId "

    Private _customerId As String

    Public Property CustomerId() As String
        Get
            Return _customerId
        End Get
        Set(ByVal value As String)
            _customerId = value
        End Set
    End Property

#End Region

#Region " CustomerName "

    Private _customerName As String

    Public Property CustomerName() As String
        Get
            Return _customerName
        End Get
        Set(ByVal value As String)
            _customerName = value
        End Set
    End Property

#End Region

#Region " QuantityInLbs "

    Private _quantityInLbs As Single

    Public Property QuantityInLbs() As Single
        Get
            Return _quantityInLbs
        End Get
        Set(ByVal value As Single)
            _quantityInLbs = value
        End Set
    End Property

#End Region

#Region " Bushels "

    Private _bushels As Single

    Public Property Bushels() As Single
        Get
            Return _bushels
        End Get
        Set(ByVal value As Single)
            _bushels = value
        End Set
    End Property

#End Region

#Region " Rank "

    Private _rank As Integer

    Public Property Rank() As Integer
        Get
            Return _rank
        End Get
        Set(ByVal value As Integer)
            _rank = value
        End Set
    End Property

#End Region



End Class
我当前的策略是为每个级别的信息构建类,并为该级别以下的信息创建子类集合,例如:

LocationDetail would change to an ICollection<Commodity>
Commodity would have an ICollection<ShipMode> 
ShipMode would have an ICollection<Customer> 
Customer would have an ICollection<FiscalYears>
LocationDetail将更改为ICollection
商品将有一个ICollection
ShipMode将有一个ICollection
客户将有一个ICollection
会计年度应具有会计年度、数量、等级、等级

我的想法是,这将允许我根据需要使用层次结构来构建每个集合的excel电子表格

在我开始这项工作之前,我需要知道是否有更简单的方法来完成这项工作?是否有更好的选项以我需要的格式输出数据

我也用SQL标记了这一点,我想也许有更好的方法直接从SQL实现这一点

附加信息和澄清


我不能使用存储过程,这是一个供应商数据库,我们对该数据库具有只读访问权限。因为我使用的是VisualStudio2008,所以我只能访问EF的初始版本,它不是很健壮。我在提取需要呈现的数据方面没有任何问题。我真的在寻找关于将其从我正在检索的格式转换为我需要呈现的格式的最佳方式的信息。我几乎感觉到我在尝试旋转数据,但不是同时旋转所有数据。请随时问我任何进一步的澄清,因为我意识到这不是一个简单的简单问题,但我认为很多人都面临过类似的问题。

我过去曾做过一些类似的事情,我已经完成了一些不同的变化

带类的存储过程(ado.net)(旧式)

具有类和Linq到SQL的实体框架

具有类和存储过程的实体框架

带类的无约束

对我来说,到目前为止最简单的方法是使用实体框架、类和LINQtoSQL

基本上,EF映射到您的类,然后使用Linq到SQL来切换您的数据,但您确定这是正确的。如果你有时间,我会研究EF和linqtosql


显然,我要提出的另一点是,当你开始考虑分层数据的概念时,如果你有一些严重的继承权,SSAS实际上是处理这种性质数据的地方。(cubing)我以前也这样做过,但我不建议采用这种方法,因为这需要花费大量的时间来进行适当的设置,并且可能会对您的任务造成过大的影响。

我最终创建了一个RankedListingClass,它基本上迭代了从主查询返回的数据,基于保证唯一数据的多个字段创建复合唯一密钥,然后根据该密钥对每个唯一项进行排序。然后,我使用这个新类根据需要生成excel文档


谢天谢地linq,否则会有更多的foreach循环遍布各地

嗨,谢谢你的回复。我在将数据输入我的应用程序时没有任何问题。实际上,我使用的是自定义sql查询和sqldatareader。这似乎是我正在做的最有效的方法,因为它实际上有数万行,即使使用我正在使用的sql分组也是如此。我会更新我的问题,希望能让我的问题更清楚。