zt_userManager zt_UserDal = new zt_userManager();
var list = zt_UserDal.GetUserToTaskList();
var check = GetCheckedValues(TaskChecked);
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 cWidth = 100;
int rHeight = 25;
//设置单元的宽度
sheet.SetColumnWidth(0, 25 * 256);
sheet.SetColumnWidth(1, 35 * 256);
sheet.SetColumnWidth(2, 15 * 256);
sheet.SetColumnWidth(3, 15 * 256);
sheet.SetColumnWidth(5, 25 * 256);
AppSetting.SetHeader(ref sheet, ref workbook, zt_product_lookup.Text, new List<string> { "模块", "任务", "编辑人", "状态", "内容", "时间" });
var listTask = list.Where(c => c.projectName == zt_product_lookup.Text && check.Contains(c.taskStatus) && (c.date > startTime && c.date < endTime)).OrderBy(c => c.moduleName).ToList();
//moduleName
var mlist = listTask.Select(c => c.moduleName).Distinct().ToList();
var znlist = listTask.Select(c => c.realname).Distinct().ToList();
///计算合并的单元格
List<CellRange> celllist = new List<CellRange>();
int endtemp = 0;
int tendtemp = 0;
int rendtemp = 0;
int sendtemp = 0;
for (int i = 0; i < mlist.Count; i++)
{
var c = listTask.Count(t => t.moduleName == mlist[i]);
if (c == 0)
{
continue;
}
if (c == 1)
{
celllist.Add(new CellRange() { colstart = 0, colend = 0, rowstart = 2 + endtemp, rowend = 1 + c + endtemp, isMerge = false, name = mlist[i] });
}
else
{
celllist.Add(new CellRange() { colstart = 0, colend = 0, rowstart = 2 + endtemp, rowend = 1 + c + endtemp, isMerge = true, name = mlist[i] });
}
endtemp += c;
var tlist = listTask.Where(a => a.moduleName == mlist[i]).Select(ab => ab.taskName).Distinct().ToList();
foreach (var titem in tlist)
{
var tc = listTask.Where(z => z.moduleName == mlist[i]).Count(za => za.taskName == titem);
if (tc == 0)
{
continue;
}
if (tc == 1)
{
celllist.Add(new CellRange() { colstart = 1, colend = 1, rowstart = 2 + tendtemp, rowend = 1 + tc + tendtemp, isMerge = false, name = $"{mlist[i]}_{titem}" });
}
else
{
celllist.Add(new CellRange() { colstart = 1, colend = 1, rowstart = 2 + tendtemp, rowend = 1 + tc + tendtemp, isMerge = true, name = $"{mlist[i]}_{titem}" });
}
tendtemp += tc;
var rlist = listTask.Where(a => a.moduleName == mlist[i] && a.taskName == titem).Select(ab => ab.realname).Distinct().ToList();
var slist = listTask.Where(a => a.moduleName == mlist[i] && a.taskName == titem).Select(ab => ab.taskStatus).Distinct().ToList();
foreach (var ritem in rlist)
{
var rc = listTask.Where(r => r.moduleName == mlist[i] && r.taskName == titem).Count(rb => rb.realname == ritem);
if (rc == 0)
{
continue;
}
if (rc == 1)
{
celllist.Add(new CellRange() { colstart = 2, colend = 2, rowstart = 2 + rendtemp, rowend = 1 + rc + rendtemp, isMerge = false, name = $"{mlist[i]}_{titem}_{ritem}" });
}
else
{
celllist.Add(new CellRange() { colstart = 2, colend = 2, rowstart = 2 + rendtemp, rowend = 1 + rc + rendtemp, isMerge = true, name = $"{mlist[i]}_{titem}_{ritem}" });
}
rendtemp += rc;
}
foreach (var sitem in slist)
{
var sc = listTask.Where(r => r.moduleName == mlist[i] && r.taskName == titem).Count(rb => rb.taskStatus == sitem);
if (sc == 0)
{
continue;
}
if (sc == 1)
{
celllist.Add(new CellRange() { colstart = 3, colend = 3, rowstart = 2 + sendtemp, rowend = 1 + sc + sendtemp, isMerge = false, name = $"{mlist[i]}_{titem}_{sitem}" });
}
else
{
celllist.Add(new CellRange() { colstart = 3, colend = 3, rowstart = 2 + sendtemp, rowend = 1 + sc + sendtemp, isMerge = true, name = $"{mlist[i]}_{titem}_{sitem}" });
}
sendtemp += sc;
}
}
}
DataTable dtTask = AppSetting.ListToDataTable(listTask);
for (int i = 0; i < dtTask.Rows.Count; ++i)
{
IRow row = sheet.CreateRow(2 + i);
for (int j = 0; j < dtTask.Columns.Count; ++j)
{
if (dtTask.Columns[j].ColumnName == "moduleName")
{
ICell newCell = row.CreateCell(0);
newCell.SetCellValue(dtTask.Rows[i][j].ToString());
newCell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.White.Index, 10);
}
else if (dtTask.Columns[j].ColumnName == "taskName")
{
ICell newCell = row.CreateCell(1);
newCell.SetCellValue(dtTask.Rows[i][j].ToString());
newCell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(dtTask.Rows[i][j].ToString()), 10);
}
else if (dtTask.Columns[j].ColumnName == "realname")
{
ICell newCell = row.CreateCell(2);
newCell.SetCellValue(dtTask.Rows[i][j].ToString());
newCell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.White.Index, 10);
}
else if (dtTask.Columns[j].ColumnName == "taskStatus")
{
ICell newCell = row.CreateCell(3);
newCell.SetCellValue(AppSetting.GetStatusToString(dtTask.Rows[i][j].ToString()));
newCell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, AppSetting.GetExcelColor(dtTask.Rows[i][j].ToString()), 10);
}
else if (dtTask.Columns[j].ColumnName == "_new")
{
ICell newCell = row.CreateCell(4);
//处理网页代码
var str = dtTask.Rows[i][j].ToString();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(str);
HtmlNodeCollection p = doc.DocumentNode.SelectNodes("//p");
if (null != p)
{
StringBuilder sb = new StringBuilder();
foreach (var item in p)
{
if (!string.IsNullOrEmpty(item.InnerText))
{
sb.Append($"{item.InnerText}\r\n");
}
}
var size = AppSetting.GetFontSize(sb.ToString(), new Font("微软雅黑", 10), this.CreateGraphics());
if (size.Height > 25)
{
rHeight = (int)Math.Round(size.Height) + 15;
}
else if (sb.Length < 50)
{
rHeight = 20;
}
else
{
rHeight = 30;
}
newCell.SetCellValue(sb.ToString());
}
else {
rHeight = 20;
newCell.SetCellValue(AppSetting.GetStatusToString(str));
}
newCell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Top, NPOI.HSSF.Util.HSSFColor.White.Index, 10);
}
else if (dtTask.Columns[j].ColumnName == "date")
{
ICell newCell = row.CreateCell(5);
newCell.SetCellValue(dtTask.Rows[i][j].ToString());
newCell.CellStyle = AppSetting.GetExcelStyle(workbook, NPOI.SS.UserModel.HorizontalAlignment.Left, VerticalAlignment.Center, NPOI.HSSF.Util.HSSFColor.White.Index, 10);
}
}
//var t = dtTask.Rows[mCount+1]["moduleName"].ToString();
row.HeightInPoints = rHeight;
}
sheet.SetColumnWidth(4, cWidth * 256);
//合并单元格
foreach (var item in celllist)
{
if (item.isMerge)
{
AppSetting.SetCellRangeAddress(ref sheet, item.rowstart, item.rowend, item.colstart, item.colend);
}
}
sheet.CreateFreezePane(0, 2); //冻结
FileStream fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
workbook.Write(fs);
fs.Close();
DevExpress.XtraEditors.XtraMessageBox.Show($"Excel文件导出 \r\n 路劲:{sfd.FileName}", "文件导出", MessageBoxButtons.OK, MessageBoxIcon.Information);
网友评论