1.导出甘特图代码
zt_userManager zt_UserDal = new zt_userManager();
var list = zt_UserDal.GetUserToTaskList();
var check = GetCheckedValues(TaskChecked);
var listTask = list.Where(c =>c.field == "desc" && c.projectName == zt_product_lookup.Text && check.Contains(c.taskStatus) && (c.date > startTime && c.date < endTime)).OrderBy(c => c.moduleName).ToList();
List
List
SaveFileDialog sfd = new SaveFileDialog();
//设置默认文件类型显示顺序
sfd.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
//设置文件名
sfd.FileName = $"{zt_product_lookup.Text}项目甘特图_" + DateTime.Now.ToString("yyyyMMddHHmms");
sfd.Filter = "Excel文件(*.xls)|*.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet($"{zt_product_lookup.Text}");
int rowIndex = 0;
//创建表头
IRow row = sheet.CreateRow(rowIndex);
ICell cell = row.CreateCell(0);
cell.SetCellValue("模块"); cell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Center, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.LightBlue.Index, 12);
cell = row.CreateCell(1);
cell.SetCellValue("任务"); cell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Center, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.LightBlue.Index, 12);
cell = row.CreateCell(2);
cell.SetCellValue("状态"); cell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Center, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.LightBlue.Index, 12);
var wDate = DateTime.MinValue;
sheet.SetColumnWidth(0, 25 * 256);//模块
sheet.SetColumnWidth(1, 35 * 256);//任务
sheet.SetColumnWidth(2, 15 * 256);//任务状态
var setNo = 0;
while (startTime < endTime)
{
wDate = startTime.AddDays(6);
if (DateTime.Compare(startTime, endTime) < 0)
{
sheet.SetColumnWidth(3 + setNo, 30 * 256);//时间框
cell = row.CreateCell(3+setNo);
cell.SetCellValue(startTime.ToString("yyyy_MM_dd ")); cell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Center, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.LightBlue.Index, 12);
timesList.Add(startTime);
setNo++;
}
startTime = startTime.AddDays(7);
}
row.HeightInPoints = 25;
rowIndex++;
//赋值开始日期
startDate_EditValueChanged(null, null);
int mCount = 1;//模块总数
int tCount = 1;//任务总数
var mlist = listTask.Select(c => c.moduleName).Distinct().ToList();
for (int i = 0; i < mlist.Count; i++) {
int mlistIndex = 0;//模块数量
var tlist = listTask.Where(c => c.moduleName == mlist[i]).Select(c => c.taskName).Distinct().ToList();
for (int t = 0; t < tlist.Count; t++) {
int tlistIndex = 0;
var talist = listTask.Where(c => c.moduleName == mlist[i] && c.taskName == tlist[t]).ToList();
var taNameList = talist.Select(c => c.realname).Distinct().ToList();
for (int ta = 0; ta < taNameList.Count; ta++) {
var row1 = sheet.CreateRow(rowIndex);
rowIndex++;
var row2 = sheet.CreateRow(rowIndex);
rowIndex++;
ICell cell1 = row1.CreateCell(0);
cell1.SetCellValue(mlist[i]); //模块
mlistIndex++;
cell1.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.White.Index, 10);
cell1 = row1.CreateCell(1);
cell1.SetCellValue(tlist[t]); //任务
tlistIndex++;
cell1.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(talist[0].taskStatus), 10);
cell1 = row1.CreateCell(2);
cell1.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(talist[0].taskStatus), 10);
cell1.SetCellValue(AppSetting.GetStatusToString(talist[0].taskStatus)); //状态
ICell cell2 = row2.CreateCell(0);
cell2.SetCellValue(mlist[i]); //模块
mlistIndex++;
cell2.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.White.Index, 10);
cell2 = row2.CreateCell(1);
cell2.SetCellValue(tlist[t]); //任务
tlistIndex++;
cell2.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(talist[0].taskStatus), 10);
cell2 = row2.CreateCell(2);
cell2.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(talist[0].taskStatus), 10);
cell2.SetCellValue(AppSetting.GetStatusToString(talist[0].taskStatus)); //状态
//内容模块处理
for (int z = 0; z < timesList.Count; z++)
{
var stime = timesList[z];//开始时间
var etime = timesList[z].AddDays(6); //结束时间
var nmodel = talist.Where(c => c.realname == taNameList[ta] && c.date>=stime&&c.date ICell cell3 = row1.CreateCell(3 + z); //存放内容 ICell cell4 = row2.CreateCell(3 + z); //存放姓名 StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for (int p = 0; p < nmodel.Count; p++) { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(nmodel[p]._new); HtmlNodeCollection p1 = doc.DocumentNode.SelectNodes("//p"); if (null != p1) { foreach (var items in p1) { if (!string.IsNullOrEmpty(items.InnerText) && items.InnerText.Contains("||")) { var str1 = items.InnerText.Replace("||", ""); if (!sb1.ToString().Contains(str1)) { sb1.Append($"{str1 }\r\n"); } } } } if (!sb2.ToString().Contains(nmodel[p].realname)) { sb2.Append($"{nmodel[p].realname} "); } } cell3.SetCellValue(sb1.ToString()); //内容 if (!string.IsNullOrEmpty(sb1.ToString())) cell3.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(talist[0].taskStatus), 10); cell4.SetCellValue(sb2.ToString()); if (!string.IsNullOrEmpty(sb2.ToString())) cell4.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(talist[0].taskStatus), 10); } } //合并任务 celllist.Add(new CellRange() { colstart = 1, colend = 1, rowstart = tCount, rowend = tCount + tlistIndex - 1, isMerge = true, name = tlist[t] }); //合并状态 celllist.Add(new CellRange() { colstart = 2, colend = 2, rowstart = tCount, rowend = tCount + tlistIndex - 1, isMerge = true, name = talist[0].taskStatus }); tCount += tlistIndex; } //合并模块 celllist.Add(new CellRange() { colstart = 0, colend = 0, rowstart = mCount, rowend = mCount + mlistIndex - 1, isMerge = true, name = mlist[i] }); mCount += mlistIndex; } //设置 foreach (var item in celllist) { if (item.isMerge) { AppSetting.SetCellRangeAddress(ref sheet, item.rowstart, item.rowend, item.colstart, item.colend); } } sheet.CreateFreezePane(3, 1); //冻结 MemoryStream stream = new MemoryStream(); workbook.Write(stream); var buf = stream.ToArray(); using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write)) { fs.Write(buf, 0, buf.Length); fs.Flush(); } DevExpress.XtraEditors.XtraMessageBox.Show($"Excel文件导出 \r\n 路劲:{sfd.FileName}", "文件导出", MessageBoxButtons.OK, MessageBoxIcon.Information);