///
/// 导出到Excel
///
///
///
/// 导出Excel
///
///
private void ExportToExcelCommandAction(object obj)
{
string CurrentDateSting = _SelectDate.ToString("yyyyMMdd");
if (RaceCollect.Count == 0)
{
MessageBox.Show("没有可导出的数据!", "警告提示.", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// CommandParameter="{Binding ElementName=RaceGrid}"
DataGrid grd = obj as DataGrid;
ExcelPackage ep = new ExcelPackage();
OfficeOpenXml.ExcelWorkbook workbook = ep.Workbook;
OfficeOpenXml.ExcelWorksheet sheet = workbook.Worksheets.Add("数据表");
#region 配置文件属性
workbook.Properties.Category = "类别";
workbook.Properties.Author = "作者";
workbook.Properties.Comments = "备注";
workbook.Properties.Company = "公司";
workbook.Properties.Keywords = "关键字";
workbook.Properties.Manager = "管理者";
workbook.Properties.Status = "内容状态";
workbook.Properties.Subject = "主题";
workbook.Properties.Title = "标题";
workbook.Properties.LastModifiedBy = "最后一次保存者";
#endregion
#region 表头
int count = 0;
foreach (var c in grd.Columns)
{
count++;
sheet.Cells[1, count].Value = c.Header;
sheet.Column(count).Width = c.Width.Value / 5;
sheet.Cells[1, count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//垂直居中
sheet.Cells[1, count].Style.VerticalAlignment = ExcelVerticalAlignment.Center; //上下居中
}
#endregion
//// Example how to Format Column 1 as numeric
//using (ExcelRange col = sheet.Cells[2, 6, 1 + _RaceCollect.Count, grd.Columns.Count])
//{
// col.Style.Numberformat.Format = "##0.00"; //"#,##0.00";
// //col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
//}
//写数据,即写数据集合
int rowIndex = 1;
foreach (Race race in _RaceCollect)
{
if (race.QH.ToString().StartsWith(CurrentDateSting))//如果期号以今天开头。则输出Excel
{
rowIndex++;
sheet.Cells[rowIndex, 1].Value = race.QH;
sheet.Cells[rowIndex, 2].Value = race.D1; SetCellColor(sheet, rowIndex, race, "C1");
sheet.Cells[rowIndex, 3].Value = race.D2; SetCellColor(sheet, rowIndex, race, "C2");
sheet.Cells[rowIndex, 4].Value = race.D3; SetCellColor(sheet, rowIndex, race, "C3");
sheet.Cells[rowIndex, 5].Value = race.D4; SetCellColor(sheet, rowIndex, race, "C4");
sheet.Cells[rowIndex, 6].Value = race.D5; SetCellColor(sheet, rowIndex, race, "C5");
sheet.Cells[rowIndex, 7].Value = race.D6; SetCellColor(sheet, rowIndex, race, "C6");
sheet.Cells[rowIndex, 8].Value = race.D7; SetCellColor(sheet, rowIndex, race, "C7");
sheet.Cells[rowIndex, 9].Value = race.D8; SetCellColor(sheet, rowIndex, race, "C8");
sheet.Cells[rowIndex, 10].Value = race.D9; SetCellColor(sheet, rowIndex, race, "C9");
sheet.Cells[rowIndex, 11].Value = race.D10; SetCellColor(sheet, rowIndex, race, "C10");
sheet.Cells[rowIndex, 12].Value = race.DCount;
sheet.Cells[rowIndex, 13].Value = race.Colordiff;
sheet.Cells[rowIndex, 14].Value = race.ColordiffTotal;
sheet.Cells[rowIndex, 15].Value = race.QH;
sheet.Cells[rowIndex, 16].Value = race.P1;
sheet.Cells[rowIndex, 17].Value = race.P2;
sheet.Cells[rowIndex, 18].Value = race.P3;
sheet.Cells[rowIndex, 19].Value = race.P4;
sheet.Cells[rowIndex, 20].Value = race.P5;
sheet.Cells[rowIndex, 21].Value = race.P6;
sheet.Cells[rowIndex, 22].Value = race.P7;
sheet.Cells[rowIndex, 23].Value = race.P8;
sheet.Cells[rowIndex, 24].Value = race.P9;
sheet.Cells[rowIndex, 25].Value = race.P10;
}
sheet.Cells[rowIndex + 1, 1].Value = "颜色小计";
//利用反射读取类属性的值
var typeRaceTj = typeof(RaceTj);
for (int i = 1; i <= 10; i++)
{
var n = (int)typeRaceTj.GetProperty($"T{i}").GetValue(_Tj);
sheet.Cells[rowIndex + 1, i+1].Value = n;
}
sheet.Row(rowIndex + 1).Height = 30;
sheet.Row(1).Height = 30;
}
var border = sheet.Cells[1, 1, rowIndex+1, 25].Style.Border;
border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;
sheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
sheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
sheet.Cells.Style.WrapText = true;
if (rowIndex == 1)
{
MessageBox.Show("没有选中日期的数据可导出Excel(可能只有前1日数据存在)!", "警告提示.", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = "Excel(*.xlsx)|*.xlsx";
sfd.Title = "保存文件路径及名称";
if (sfd.ShowDialog() == true)
{
FileInfo file = new FileInfo(sfd.FileName);
ep.File = file;
ep.Save();
MessageBox.Show("选中日期数据已经成功保存至Excel!", "保存Excel提示.", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("保存操作被取消");
}
}
///
/// 设置单元格背景色为红色
///
///
///
///
///
private static void SetCellColor(ExcelWorksheet sheet, int rowIndex, Race race, string colorFieldName)
{
//反射类属性值
PropertyInfo p = typeof(Race).GetProperty(colorFieldName);
int col = 0;
if ((int)p.GetValue(race) == 1)
{
col = int.Parse(colorFieldName.Substring(1, colorFieldName.Length - 1)) + 1;
sheet.Cells[rowIndex, col].Style.Fill.PatternType = ExcelFillStyle.Solid;//将背景图案设置为实体
sheet.Cells[rowIndex, col].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.IndianRed);
}
}