获取字段类型值
金蝶云社区-assassinl10
assassinl10
0人赞赏了该文章 886次浏览 未经作者许可,禁止转载编辑于2017年09月26日 13:35:15

[password]lzh123[/password]
#region 根据字段类型进行赋值

if (field is BaseDataField)
{
string baseName = (item[field.Key] as DynamicObject) == null
? ""
: (item[field.Key] as DynamicObject)[
(field as BaseDataField).NameProperty.PropertyName].ToString().Trim();
cell.SetCellValue(baseName);
}
else if (field is BasePropertyField)
{
string displayfield =
(view.LayoutInfo.GetFieldAppearance(field.Key) as BasePropertyFieldAppearance)
.SourceFieldAppearance.Field.FieldName;
string value = (item[field.ControlField.Key] as DynamicObject) == null
? ""
: (item[field.ControlField.Key] as DynamicObject)[displayfield].ToString().Trim();
cell.SetCellValue(value);
}
else if (field is PictureField)
{
if (
!String.IsNullOrEmpty(item[field.Key] == null
? ""
: item[field.Key].ToString().Trim()))
{
byte[] bytes = GetImageBytes(item[field.Key], view);
if (bytes != null)
{
row.Height = 80*20;
sheet.SetColumnWidth(j, 256*20);
columnStyle.WrapText = false;
cell.CellStyle = columnStyle;
int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
IDrawing patriarch = sheet.CreateDrawingPatriarch();

if (fileType.EqualsIgnoreCase("xls"))
{
HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 0, 0, j, rowline,
j + 1,
rowline + 1);
HSSFPicture pict = (HSSFPicture) patriarch.CreatePicture(anchor, pictureIdx);
}
else if (fileType.EqualsIgnoreCase("xlsx"))
{
XSSFClientAnchor anchor = new XSSFClientAnchor(1*10000, 1*10000, 0, 0, j,
rowline, j + 1,
rowline + 1);
XSSFPicture pict = (XSSFPicture) patriarch.CreatePicture(anchor, pictureIdx);
}

}
}
}
else if (field is ComboField)
{
if (
!String.IsNullOrEmpty(item[field.Key] == null
? ""
: item[field.Key].ToString().Trim()))
{
ComboField comboField = field as ComboField;
var enumObj = (EnumObject) comboField.EnumObject;
//根据枚举值获取枚举项,然后拿枚举项的枚举名称
var enumItemName =
enumObj.Items.FirstOrDefault(
p =>
p.Value.Equals(item[field.Key] == null ? "" : item[field.Key].ToString()))
.Caption.ToString();
cell.SetCellValue(enumItemName);
}
}
else if (field is DateTimeField || field is TimeField)
{
if (
!String.IsNullOrEmpty(item[field.Key] == null
? ""
: item[field.Key].ToString().Trim()))
{
DateTime dt = Convert.ToDateTime(item[field.Key].ToString());
string formatStr =
view.LayoutInfo.GetFieldAppearance(field.Key).DisplayFormatString;
cell.SetCellValue(dt.ToString(formatStr));
}
}
else if (field is DecimalField)
{
Double dc = Convert.ToDouble(item[field.Key] == null ? "" : item[field.Key].ToString());
if (!dc.Equals(0))
{
cell.SetCellValue(dc);
}
}
else
{
if (
!String.IsNullOrEmpty(item[field.Key] == null
? ""
: item[field.Key].ToString().Trim()))
{
string value = item[field.Key].ToString().Trim(); //文本
cell.SetCellValue(value);
}
}

#endregion