-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guid as Property value #19
Comments
Yes, this should be possible. I didn't test it though it might be related to the default converter not being able to convert from string to Guid. Are you sure that the cell value can be parsed to a Guid? For now, you can provide a custom function to the var data = package.Workbook.Worksheets["MyWorksheet"]
.Extract<MyPoco>()
.WithProperty(p => p.MyGuidProperty, "A",
(value) =>
{
Guid convertedValue;
if (!Guid.TryParse(value.ToString(), out convertedValue))
throw new InvalidCastException(string.Format("Cannot convert type {0} to Guid. Value {1}",
value.GetType(), value.ToString()));
return convertedValue;
})
.GetData(1, 100)
.ToList(); |
Thanks for your quick reply. Yes i can, I use
the setPropertyCastedValueCallback on the string column and try parse the
cell value and it passes as a Guid.
But i am going to use the snippet you send me because that looks like a
better solution so my POCO can have the Guid property.
…On Wed, Apr 17, 2019 at 3:12 PM Israel Valverde ***@***.***> wrote:
Yes, this should be possible. I didn't test it thought it might be related
to the default converter not being able to convert from string to Guid. Are
you sure that the cell value can be parsed to a Guid?
For now, you can provide a custom function to the
<https://github.com/ipvalverde/EPPlus.DataExtractor/blob/f652aa54a94b0bc5ed7e284dc6fbc4bd4e09909c/src/EPPlus.DataExtractor/DataExtractor.cs#L74>
WithProperty where you can perform the parsing yourself. The parameter
will be an "object" with the content of the cell and the return will be
mapped to the property that you indicated, something like this:
var data = package.Workbook.Worksheets["MyWorksheet"]
.Extract<MyPoco>()
.WithProperty(p => p.MyGuidProperty, "A",
(value) =>
{
Guid convertedValue;
if (!Guid.TryParse(value.ToString(), out convertedValue))
throw new InvalidCastException(string.Format("Cannot convert type {0} to Guid. Value {1}",
value.GetType(), value.ToString()));
return convertedValue;
})
.GetData(1, 100)
.ToList();
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#19 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AL3NCG6KZG75XGOHBTGI2D3PQ6OFJANCNFSM4HGWQO6A>
.
|
@ipvalverde |
Hi @paradisehuman , If your value can be null your model's |
Can the POCO have a Guid or as data type?
I am getting an error because a Guid can be parse but not casted.
System.InvalidCastException : Invalid cast from 'System.String' to 'System.Guid'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at OfficeOpenXml.Utils.ConvertUtil.GetTypedCellValue[T](Object value)
at EPPlus.DataExtractor.PropertyValueSetter
2.SetPropertyValue(TModel dataInstance, ExcelRangeBase cell) at EPPlus.DataExtractor.DataExtractor
1.d__8.MoveNext()The text was updated successfully, but these errors were encountered: