从Ilist中取出数据导入到Excel中
/// <summary>
/// 创建Excel
/// </summary>
/// <returns></returns>
public object CreateExcel(IList<DocumentRegisterInfo> list)
{
Application myExcel = new Application();
myExcel.Application.Workbooks.Add(true);
myExcel.Visible = true;
Type type = list[0].GetType();
PropertyInfo[] properties = type.GetProperties();//获得list中的属性
for (int i = 0; i < properties.Count(); i++)
{
string Names = properties[i].Name;//对应数据库中字段名
myExcel.Rows.Cells[1, i+1] = Names;//设置列名
}
//每个单元格中的数据
for (int i = 0; i < list.Count; i++)
{
for (int j = 0 , k=1; j < properties.Count(); j++,k=1)
{
myExcel.Rows.Cells[i + 2, k++] = list[i].ID;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentID;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentName;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentTitle;
myExcel.Rows.Cells[i + 2, k++] = list[i].Author;
myExcel.Rows.Cells[i + 2, k++] = list[i].ResponsiblePerson;
myExcel.Rows.Cells[i + 2, k++] = list[i].StartTime;
myExcel.Rows.Cells[i + 2, k++] = list[i].EndTime;
myExcel.Rows.Cells[i + 2, k++] = list[i].Department;
myExcel.Rows.Cells[i + 2, k++] = list[i].ConfidentialRange;
myExcel.Rows.Cells[i + 2, k++] = list[i].DecryptDate;
myExcel.Rows.Cells[i + 2, k++] = list[i].Remark;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentTypeOne;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentTypeTwo;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentTypeThree;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentStatus;
myExcel.Rows.Cells[i + 2, k++] = list[i].DocumentRequ;
myExcel.Rows.Cells[i + 2, k++] = list[i].PlanStartDate;
myExcel.Rows.Cells[i + 2, k++] = list[i].PlanEndDate;
myExcel.Rows.Cells[i + 2, k++] = list[i].PlanStatus;
myExcel.Rows.Cells[i + 2, k++] = list[i].VersionNum;
myExcel.Rows.Cells[i + 2, k++] = list[i].ModifyType;
myExcel.Rows.Cells[i + 2, k++] = list[i].ModifyContent;
myExcel.Rows.Cells[i + 2, k++] = list[i].Redactor;
myExcel.Rows.Cells[i + 2, k++] = list[i].RevisionDate;
myExcel.Rows.Cells[i + 2, k++] = list[i].Reviewer;
myExcel.Rows.Cells[i + 2, k++] = list[i].AuditDate;
myExcel.Rows.Cells[i + 2, k++] = list[i].Approval;
myExcel.Rows.Cells[i + 2, k++] = list[i].ApprovalDate;
myExcel.Rows.Cells[i + 2, k++] = list[i].EffectStartTime;
myExcel.Rows.Cells[i + 2, k++] = list[i].EffectEndTime;
myExcel.Rows.Cells[i + 2, k++] = list[i].FailureStartTime;
myExcel.Rows.Cells[i + 2, k++] = list[i].FailureEndTime;
myExcel.Rows.Cells[i + 2, k++] = list[i].InsertTime;
myExcel.Rows.Cells[i + 2, k++] = list[i].LastChanged;
myExcel.Rows.Cells[i + 2, k++] = list[i].Status;
}
}
myExcel.ActiveWorkbook.SaveAs("D:\\1.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);//保存Excel到指定路径
return Json(new
{
});
}

浙公网安备 33010602011771号