#region 导出任务信息
public FileResult ExportActivityTask(string taskno, string taskname, string startTime, string endTime, string status, string taskliststatus)
{
//获得查询条件
Dictionary<string, string> dicois = new Dictionary<string, string>();
List<ViewParas> vplist = GetFilter(taskno, taskname, startTime, endTime, status, taskliststatus);
//根据查询条件获得数据
List<ViewActivityTask> listtask = ActivityTaskProvider.GetActivityTaskByFilter(vplist);
//获得当前登录人ID
string userid = CurrentUserIdentity.UserID.ToString();
string templatePath = Server.MapPath(@"/Template/任务列表模板.xlsx");
Workbook workbook = new Workbook();
workbook.Open(templatePath);
Cells cells = workbook.Worksheets[0].Cells;
if (listtask.Count > 0)
{
List<int> listtaskid = new List<int>();
//循环获得所有的任务ID
listtask.ForEach(p =>
{
listtaskid.Add(p.Id);
});
//根据任务ID循环获得申请信息
List<MaterielApplyRecord> list = ActivityTaskProvider.GetMaterielApplyRecordByTaskids(listtaskid);
if (list.Count > 0)
{
listtask.ForEach(p =>
{
list.ForEach(m =>
{
ViewActivityTaskApply applymodel = JSONOperate.Deserialize<ViewActivityTaskApply>(m.MaterielInfo);
if (string.IsNullOrEmpty(taskliststatus))
{
//任务发起人角度
if (p.Id == applymodel.TaskId)
{
p.Money += applymodel.ApplyProducts.Sum(a => p.Money);
p.PostFee += applymodel.ApplyProducts.Sum(a => p.PostFee);
}
}
else
{
//申请人角度
if (p.Id == applymodel.TaskId && m.ApplyUser.ToString() == userid)
{
p.Money += applymodel.ApplyProducts.Sum(a => p.Money);
p.PostFee += applymodel.ApplyProducts.Sum(a => p.PostFee);
}
}
});
//判断是否已申请过
if (list.FindAll(h => JSONOperate.Deserialize<ViewActivityTaskApply>(h.MaterielInfo).TaskId == p.Id && h.ApplyUser.ToString() == userid).Count > 0)
{
p.ApplyStatus = 1;
p.applyid = list.FindAll(h => JSONOperate.Deserialize<ViewActivityTaskApply>(h.MaterielInfo).TaskId == p.Id && h.ApplyUser.ToString() == userid)[0].ID;
}
else
{
p.ApplyStatus = 0;
}
if (!string.IsNullOrEmpty(taskliststatus))
{
if (p.Status > 2)
{
p.StatusString = "已结束";
}
}
p.TotalAmount = p.Money + p.PostFee;
});
}
int row = 1;
int column = 0;
foreach (var item in listtask)
{
List<ViewActivityTaskApply> listapplyinfo = new List<ViewActivityTaskApply>();
list.ForEach(p =>
{
ViewActivityTaskApply applymodel = JSONOperate.Deserialize<ViewActivityTaskApply>(p.MaterielInfo);
if (applymodel.TaskId == item.Id)
{
applymodel.ApplyTimeStr = p.ApplyTimeStr;//申请时间
applymodel.ID = p.ID;//申请编号
listapplyinfo.Add(applymodel);//申请详细信息
}
});
//子列表有数据合并单元格
if (listapplyinfo != null && listapplyinfo.Count > 0)
{
//合并单元格cells.Merge(1, 0, 3, 1) 参数1代表当前行,参数0代表当前行当前列即第一行第一列,参数3合并的行数,参数4合并的列数
cells.Merge(row, column, listapplyinfo.Count, 1);
cells[row, column].PutValue(item.TaskNo);
cells.Merge(row, column + 1, listapplyinfo.Count, 1);
cells[row, column + 1].PutValue(item.TaskName);
cells.Merge(row, column + 2, listapplyinfo.Count, 1);
cells[row, column + 2].PutValue(item.StartDateString);
cells.Merge(row, column + 3, listapplyinfo.Count, 1);
cells[row, column + 3].PutValue(item.EndDateString);
//任务编号详细列表信息
foreach (ViewActivityTaskApply ite in listapplyinfo)
{
cells[row, column + 4].PutValue(ite.ID);
cells[row, column + 5].PutValue(ite.Name);
cells[row, column + 6].PutValue(ite.Mobile);
cells[row, column + 7].PutValue(ite.ApplyTimeStr);
cells[row, column + 8].PutValue(ite.ApplyProducts.Sum(p => p.FinalApplyInfo != null ? p.FinalApplyInfo.Money : p.OriginalApplyInfo.Money));
cells[row, column + 9].PutValue(ite.ApplyProducts.Sum(p => p.FinalApplyInfo != null ? p.FinalApplyInfo.PostFee : p.OriginalApplyInfo.PostFee));
cells[row, column + 10].PutValue(ite.Address.ReceviceName);
cells[row, column + 11].PutValue(ite.Address.Mobile);
cells[row, column + 12].PutValue(ite.Address.Address);
row++;
}
}
else
{
cells[row, column].PutValue(item.TaskNo);
cells[row, column + 1].PutValue(item.TaskName);
cells[row, column + 2].PutValue(item.StartDateString);
cells[row, column + 3].PutValue(item.EndDateString);
row++;
}
}
}
string filename = string.Empty;
if (string.IsNullOrEmpty(taskliststatus))
{
filename = "任务列表" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
}
else
{
filename = "申请列表" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
}
byte[] byti = workbook.SaveToStream().GetBuffer();
return File(byti, "application/octet-stream", filename);
}
网友评论