2014-04-23 37 views
35

我試圖使用導出爲Excell,PDF,TextFile生成報告。那麼我在MVC中這樣做。我有一類我命名SPBatch(這是我的存儲過程的確切名稱在我的SQL),它包含以下內容:DataSet在Export中不支持System.Nullable <>

public string BatchNo { get; set; } 
public string ProviderName { get; set; } 
public Nullable<System.Int32> NoOfClaims { get; set; } 
public Nullable<System.Int32> TotalNoOfClaims { get; set; } 
public Nullable<System.Decimal> TotalBilled { get; set; } 
public Nullable<System.Decimal> TotalInputtedBill { get; set; } 
public Nullable<System.DateTime> DateCreated { get; set; } 
public Nullable<System.DateTime> DateSubmitted { get; set; } 
public Nullable<System.DateTime> DueDate { get; set; } 
public string Status { get; set; } 
public string RefNo { get; set; } 
public string BatchStatus { get; set; } 
public string ClaimType { get; set; } 

,你可以看到我的一些列的聲明爲空。它順利地從表格中搜索和顯示結果。我有幾個按鈕下面這對出口和每次我嘗試在Excel導出時的圖像按鈕,我總是得到這個問題「集不支持System.Nullable <>」在我的這部分代碼:

foreach (MemberInfo mi in miArray) 
{ 
    if (mi.MemberType == MemberTypes.Property) 
    { 
     PropertyInfo pi = mi as PropertyInfo; 
     dt.Columns.Add(pi.Name, pi.PropertyType); //where the error pop's up. 

    } 
    else if (mi.MemberType == MemberTypes.Field) 
    { 
     FieldInfo fi = mi as FieldInfo; 
     dt.Columns.Add(fi.Name, fi.FieldType); 
    } 
} 

該錯誤在評論中顯示。你能幫我做些什麼嗎?我嘗試在代碼中添加DBNull,但仍然收到相同的錯誤。我試圖刪除我的SPBatch中的Nullable,但我得到一個錯誤,有些表需要聲明爲Nullable。

我該怎麼辦?

+0

我不會使用'DataSet',因爲它似乎不支持您的需求建議。 –

回答

84

嘗試

dt.Columns.Add(pi.Name, Nullable.GetUnderlyingType(
      pi.PropertyType) ?? pi.PropertyType); 
+1

我嘗試過,但我得到錯誤「對象引用未設置爲對象的實例。」在Columns [column] .SetOrdinal(Index); –

+2

我不能在代碼示例中看到這樣的代碼! – Damith

+1

以及在這邊'公共無效SetColumn(字符串列,字符串columnName) { Columns [column] .SetOrdinal(Index); // here Columns [column] .ColumnName = columnName; Index ++; }' –

1

我會尋找可空,並用它可以不像一個DateTime空字符串替換它。

foreach (PropertyInfo pi in properties) 
{ 
    if (pi.PropertyType.Name.Contains("Nullable")) 
     myDataType = typeof(String); 
    else 
     myDataType = pi.PropertyType; 
} 

下面是一個完整的版本:

private DataTable CreateDataTable(PropertyInfo[] properties) 
{ 
    DataTable dt = new DataTable(); 
    DataColumn dc = null; 
    foreach (PropertyInfo pi in properties) 
    { 
     dc = new DataColumn(); 
     dc.ColumnName = pi.Name; 

     if (pi.PropertyType.Name.Contains("Nullable")) 
      dc.DataType = typeof(String); 
     else 
      dc.DataType = pi.PropertyType; 

     // dc.DataType = pi.PropertyType; 
     dt.Columns.Add(dc); 
    } 
    return dt; 
} 
+1

我應該在哪裏插入這段代碼? –

+2

那麼,如果底層數據在特定行中包含DateTime呢? –

+1

不存在值的可爲空的DateTime將在Excel結尾轉換爲NULL。 –

3

多虧了產生一個DataTable和一些黑客周圍的C#版本,我可以提供在VB這個答案 - 我把它放在這裏,因爲我在使用一個簡單的數據層的時候,想要從存儲過程中獲得一個可過濾的數據集有很多麻煩。我希望它能幫助別人!

注:使用情況是你希望使用BindingSource.Filter =「一些查詢字符串」:

Imports System.Reflection 

Public Module Extenders 
<System.Runtime.CompilerServices.Extension> 
Public Function ToDataTable(Of T)(collection As IEnumerable(Of T), tableName As String) As DataTable 
    Dim tbl As DataTable = ToDataTable(collection) 
    tbl.TableName = tableName 
    Return tbl 
End Function 

<System.Runtime.CompilerServices.Extension> 
Public Function ToDataTable(Of T)(collection As IEnumerable(Of T)) As DataTable 
    Dim dt As New DataTable() 

    Dim tt As Type = GetType(T) 
    Dim pia As PropertyInfo() = tt.GetProperties() 

    'Create the columns in the DataTable 

    For Each pi As PropertyInfo In pia 
     Dim a = 
If(Nullable.GetUnderlyingType(pi.PropertyType), pi.PropertyType) 
     dt.Columns.Add(pi.Name, If(Nullable.GetUnderlyingType(pi.PropertyType), pi.PropertyType)) 
    Next 

    'Populate the table 

    For Each item As T In collection 
     Dim dr As DataRow = dt.NewRow() 
     dr.BeginEdit() 

     For Each pi As PropertyInfo In pia 

      dr(pi.Name) = If(Nullable.GetUnderlyingType(pi.PropertyType) Is GetType(DateTime), DBNull.Value, pi.GetValue(item, Nothing)) 
     Next 
     dr.EndEdit() 
     dt.Rows.Add(dr) 
    Next 
    Return dt 
End Function 

End Module 
+1

這太棒了。正是我需要的。我做了一個小的改變,這可能對其他人有用... dr(pi.Name)= If(pi.GetValue(item,Nothing),DBNull。價值) 這似乎可以讓它工作更好一些可空日期 – user1751825

相關問題