2014-04-23 33 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

相关问题