/// <summary>
/// 详细报表生成excel列名
/// </summary>
private static string _detailsCloumns = @"Actual Claim Amount,Decl Date";
/// <summary>
/// 详细报表生成excel
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static byte[] DetailDownloadAsync(ImportDetailResponse request)
{
var workbook = new XSSFWorkbook();
var cellStyle = workbook.CreateCellStyle();
// 垂直对齐方式 ,设定垂直居中
cellStyle.VerticalAlignment = VerticalAlignment.Center;
// 水平对齐方式 ,设定水平居中
cellStyle.Alignment = HorizontalAlignment.Center;
var sheet = workbook.CreateSheet("import detail compare data");
var headRow = sheet.CreateRow(0);
string[] titles = _detailsCloumns.Split(",");
for (int i = 0; i < titles.Length; i++)
{
string title = titles[i].Trim();
headRow.CreateCell(i).SetCellValue(title);
}
var datas = request.List;
if (datas != null)
{
//根据SpecialList生成标题
if (datas.Count(t => t.SpecialList != null) > 0)
{
int max = datas.Where(t => t.SpecialList != null).Max(x => x.SpecialList.Count);
if (max > 0)
{
int index = 1;
int specialListCount = titles.Length + max;
for (int i = titles.Length; i < specialListCount; i++)
{
headRow.CreateCell(i).SetCellValue("Specialinstruction" + index);
index++;
}
}
}
for (int i = 0; i < datas.Count; i++)
{
var row = sheet.CreateRow(i + 1);
var fields = GetDetailFields(datas[i]);
for (int j = 0; j < titles.Length; j++)
{
string value = fields[j];
if (!string.IsNullOrWhiteSpace(value))
{
value = value.Trim();
}
else
{
value = "";
}
row.CreateCell(j).SetCellValue(value);
}
if (datas[i].SpecialList != null)
{
for (int j = 0; j < datas[i].SpecialList.Count; j++)
{
row.CreateCell(71 + j).SetCellValue(datas[i].SpecialList[j]);
}
}
}
}
byte[]? result = null;
using (var stream = new MemoryStream())
{
workbook.Write(stream);
result = stream.ToArray();
}
return result;
}
/// <summary>
/// 获取ImportDetail中的字段值
/// </summary>
/// <returns>所有字段名称</returns>
public static List<string> GetDetailFields(ImportDetail detail)
{
var list = new List<string>();
if (detail == null)
{
return list;
}
var pro = detail.GetType().GetProperties();
foreach (var item in pro)
{
object value = item.GetValue(detail);
if (value == null)
{
list.Add("");
}
else
{
if (item.Name == "ReleaseDate" || item.Name == "DeclnoDate")
{
if (item.Name == "CreatedOn")
{
string createdOn = (string)value;
if (!string.IsNullOrWhiteSpace(createdOn))
{
list.Add(DateTime.Parse(createdOn).ToString("yyyy/MM/dd"));
}
}
else
{
var date = (DateTime)value;
list.Add(date.ToString("yyyy/MM/dd"));
}
}
else
{
list.Add(value.ToString());
}
}
}
return list;
}