/// <summary>
/// 导出excel表
/// </summary>
/// <returns></returns>
public FileContentResult export_CheckTree(string CheckInfoId)
{
var CheckName = _repo.Find<LH_CheckInfo>(CheckInfoId).CheckTitle;
var CheckDetail = _repo.FindAll<LH_CheckDetail>(o => o.CheckInfoId == CheckInfoId).ToList();
var treeInfo = _repo.FindAll<LH_TreeInfo>(o => o.IsDel != 1).ToList();
var mode = (from a in CheckDetail join b in treeInfo on a.TreeInfoId equals b.Id orderby a.CreateDate descending select b).ToList();
// List<LH_TreeInfo> TreeList = new List<LH_TreeInfo>();
if (mode.Count() == 0)
{
mode.Add(new LH_TreeInfo());
}
DataTable dt = new ModelHandler<LH_TreeInfo>().FillDataTable(mode);
//dt.Columns.Add("CreateDate1");
//foreach (DataRow dr in dt.Rows)
//{
// if (dr["PlantTime"].ToString().Length != 0)
// {
// dr["PlantTime"] = Convert.ToDateTime(dr["PlantTime"]).ToString("yyyy年MM月dd日HH时mm分");
// }
//}
if (dt != null)
{
dt.TableName = "m";
string fileUrl = Server.MapPath("~/App_Data/苗木标注模板.xlsx");
return File(ExportForTemplate(dt, fileUrl), "application/ms-excel", Server.UrlEncode("苗木普查" +CheckName+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"));
}
else
{
return null;
}
}
public static byte[] ExportForTemplate(DataTable data, string fileUrl)
{
try
{
data.TableName = "m";//excel模板中对应的表名
//取所需的数据(自己写一个函数)
WorkbookDesigner designer = new WorkbookDesigner();
//给模板对象设置数据源
designer.SetDataSource(data);
//excel模板的地址
// designer.Open(fileUrl);
designer.Workbook = new Workbook(fileUrl);
designer.Process();//全自动赋值
//新生成的excel的保存地址// designer.Workbook.Save(file, FileFormatType.Excel97To2003);
return designer.Workbook.SaveToStream().ToArray();
// return file;
}
catch (Exception e)
{
// _Log.Error(e.Message);
return null;
}
}
//模板格式