officefileapi-devexpress-dot-spreadsheet-dot-worksheetextensions-dot-import-x28-worksheet-ienumerable-int32-int32-boolean-dataimportoptions-x29.md
Imports data from a collection.
You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this method in production code.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Docs.v25.2.dll
NuGet Package : DevExpress.Document.Processor
public static void Import(
this Worksheet sheet,
IEnumerable source,
int firstRowIndex,
int firstColumnIndex,
bool isVertical,
DataImportOptions options
)
<ExtensionAttribute>
Public Shared Sub Import(
sheet As Worksheet,
source As IEnumerable,
firstRowIndex As Integer,
firstColumnIndex As Integer,
isVertical As Boolean,
options As DataImportOptions
)
| Name | Type | Description |
|---|---|---|
| sheet | Worksheet |
A Worksheet that is the worksheet to which the data is imported.
| | source | IEnumerable |
An object that exposes the IEnumerable interface provided by a collection of objects being imported.
| | firstRowIndex | Int32 |
An integer that is the row index of the start cell in which the imported data will be inserted.
| | firstColumnIndex | Int32 |
An integer that is the column index of the start cell in which the imported data will be inserted.
| | isVertical | Boolean |
true , to insert imported data vertically; otherwise, false
| | options | DataImportOptions |
A DataImportOptions object containing data import options, parameters and converter.
|
List<TestObject> list = new List<TestObject>();
list.Add(new TestObject(1, "1", true));
list.Add(new TestObject(2, "2", false));
worksheet.Import(list, 0, 0, new DataSourceImportOptions() { Converter = new TestDataValueConverter() });
Dim list As New List(Of TestObject)()
list.Add(New TestObject(1, "1", True))
list.Add(New TestObject(2, "2", False))
worksheet.Import(list, 0, 0, New DataSourceImportOptions() With {.Converter = New TestDataValueConverter()})
class TestDataValueConverter : DevExpress.Spreadsheet.IDataValueConverter
{
public bool TryConvert(object value, int columnIndex, out DevExpress.Spreadsheet.CellValue result)
{
string strValue = value as string;
if (strValue != null)
{
int str2int;
bool success = Int32.TryParse(strValue, out str2int);
result = success ? str2int : 0;
return true;
}
Type valueType = value.GetType();
if (valueType == typeof(int))
result = (int)value;
else
result = null;
return true;
}
}
Friend Class TestDataValueConverter
Implements DevExpress.Spreadsheet.IDataValueConverter
Public Function TryConvert(ByVal value As Object, ByVal columnIndex As Integer, ByRef result As DevExpress.Spreadsheet.CellValue) As Boolean Implements DevExpress.Spreadsheet.IDataValueConverter.TryConvert
Dim strValue As String = TryCast(value, String)
If strValue IsNot Nothing Then
Dim str2int As Integer = Nothing
Dim success As Boolean = Int32.TryParse(strValue, str2int)
result = If(success, str2int, 0)
Return True
End If
Dim valueType As Type = value.GetType()
If valueType Is GetType(Integer) Then
result = DirectCast(value, Integer)
Else
result = Nothing
End If
Return True
End Function
End Class
class TestObject
{
public TestObject(int intValue, string value, bool boolValue)
{
this.intValue = intValue;
this.Value = value;
this.BoolValue = boolValue;
}
public int intValue;
private int privateValue { get { return 123; } }
public int IntValue { get { return intValue + privateValue - 123; } }
public string Value { get; set; }
public bool BoolValue { get; set; }
public int this[int index] { get { return index; } }
}
Friend Class TestObject
Public Sub New(ByVal intValue As Integer, ByVal value As String, ByVal boolValue As Boolean)
Me.intValue_Renamed = intValue
Me.Value = value
Me.BoolValue = boolValue
End Sub
Public intValue_Renamed As Integer
Private ReadOnly Property privateValue() As Integer
Get
Return 123
End Get
End Property
Public ReadOnly Property IntValue() As Integer
Get
Return intValue_Renamed + privateValue - 123
End Get
End Property
Public Property Value() As String
Public Property BoolValue() As Boolean
Default Public ReadOnly Property Item(ByVal index As Integer) As Integer
Get
Return index
End Get
End Property
End Class
See Also
How to: Import Data to a Worksheet in SpreadsheetControl for WinForms