C# OfficeOpenXML 画图,单元格染色

public void ExportDataTableToExcel(Page SourcePage, DataTable dt, string file_name, string[] Grid_column_name, string[] Grid_column_text, int[] Grid_column_width)
{
dt = ExportExcel.ChangeDataTable_ColumnName_OnlyDefineColumn(dt, Grid_column_name, Grid_column_text);
ExcelPackage excel = new ExcelPackage();
var workSheet = excel.Workbook.Worksheets.Add(“Sheet1”);
workSheet.Cells[1, 1].LoadFromDataTable(dt, true);
ExcelChartsheet chartSheet = null;
if(this.gg.Checked)
{
var chart = workSheet.Drawings.AddChart(“ColumnScatterChart”, OfficeOpenXml.Drawing.Chart.eChartType.XYScatter);
chart.Legend.Position = OfficeOpenXml.Drawing.Chart.eLegendPosition.Bottom;
chart.Legend.Add();
chart.Title.Text = “X-Y Scatter Chart”;
chart.SetSize(800, 400);
chart.ShowHiddenData = true;
chart.XAxis.MinorUnit = 1;
chart.YAxis.MinorUnit = 10;
for (int row = 1; row <= dt.Columns.Count; row++)
{
chart.Series.Add(workSheet.Cells[row+1,9],workSheet.Cells[row + 1, 8]);
}
}
//chart.XAxis.SourceLinked=

    //for (int rowindex = 2; rowindex <= dt.Rows.Count; rowindex++)
    //{
    //    if (workSheet.Cells[rowindex, 17].Text.ToString().Trim() != workSheet.Cells[rowindex, 35].Text.ToString().Trim())
    //    {
    //        workSheet.Cells[rowindex, 17].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 17].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //        workSheet.Cells[rowindex, 35].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 35].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    }
    //    if (workSheet.Cells[rowindex, 6].Text.ToString().Trim() != workSheet.Cells[rowindex, 24].Text.ToString().Trim())
    //    {
    //        workSheet.Cells[rowindex, 6].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 6].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //        workSheet.Cells[rowindex, 24].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 24].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    }
    //    if (workSheet.Cells[rowindex, 7].Text.ToString().Trim() != workSheet.Cells[rowindex, 25].Text.ToString().Trim())
    //    {
    //        workSheet.Cells[rowindex, 7].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 7].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //        workSheet.Cells[rowindex, 25].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 25].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    }
    //    if (workSheet.Cells[rowindex, 8].Text.ToString().Trim() != workSheet.Cells[rowindex, 26].Text.ToString().Trim())
    //    {
    //        workSheet.Cells[rowindex, 8].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 8].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //        workSheet.Cells[rowindex, 26].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 26].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    }
    //    //if (workSheet.Cells[rowindex, 8].Text.ToString().Trim() != workSheet.Cells[rowindex, 25].Text.ToString().Trim())
    //    //{
    //    //    workSheet.Cells[rowindex, 8].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //    //    workSheet.Cells[rowindex, 8].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    //    workSheet.Cells[rowindex, 25].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //    //    workSheet.Cells[rowindex, 25].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    //}
    //    if (workSheet.Cells[rowindex, 10].Text.ToString().Trim() != workSheet.Cells[rowindex, 28].Text.ToString().Trim())
    //    {
    //        workSheet.Cells[rowindex, 10].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 10].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //        workSheet.Cells[rowindex, 28].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 28].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    }

    //    //if (workSheet.Cells[rowindex, 9].Text.ToString().Trim() != workSheet.Cells[rowindex, 26].Text.ToString().Trim())
    //    //{
    //    //    workSheet.Cells[rowindex, 9].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //    //    workSheet.Cells[rowindex, 9].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    //    workSheet.Cells[rowindex, 26].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //    //    workSheet.Cells[rowindex, 26].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
    //    //}
    //    if (workSheet.Cells[rowindex, 15].Text.ToString().Trim().IndexOf("Over") > -1)
    //    {
    //        workSheet.Cells[rowindex, 11].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 11].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //        workSheet.Cells[rowindex, 15].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 15].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //    }
    //    if (workSheet.Cells[rowindex, 33].Text.ToString().Trim().IndexOf("Over") > -1)
    //    {
    //        workSheet.Cells[rowindex, 27].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 27].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //        workSheet.Cells[rowindex, 33].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 33].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //    }
    //    if (workSheet.Cells[rowindex, 16].Text.ToString().Trim().IndexOf("OOC") > -1 || workSheet.Cells[rowindex, 16].Text.ToString().Trim().IndexOf("OOS") > -1)
    //    {
    //        workSheet.Cells[rowindex, 16].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 16].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //        //workSheet.Cells[rowindex, 15].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        //workSheet.Cells[rowindex, 15].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //    }
    //    if (workSheet.Cells[rowindex, 34].Text.ToString().Trim().IndexOf("OOC") > -1 || workSheet.Cells[rowindex, 34].Text.ToString().Trim().IndexOf("OOS") > -1)
    //    {
    //        workSheet.Cells[rowindex, 34].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        workSheet.Cells[rowindex, 34].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //        //workSheet.Cells[rowindex, 15].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //        //workSheet.Cells[rowindex, 15].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
    //    }
    //}

    //range.Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin,Color.Red);
    //range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    //range.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);

    using (var memoryStream = new MemoryStream())
    {
        SourcePage.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        SourcePage.Response.AddHeader("content-disposition", "attachment;  filename=" + file_name + ".xlsx");
        excel.SaveAs(memoryStream);
        memoryStream.WriteTo(SourcePage.Response.OutputStream);
        SourcePage.Response.Flush();
        SourcePage.Response.End();
    }
    //Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application();
}

public DataTable ChangeDataTable_ColumnName_OnlyDefineColumn(DataTable dt, string[] Grid_column_name, string[] Grid_column_text)
{
DataTable dt2 = new DataTable();
for (int i = 0; i <= Grid_column_name.Length - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
if (dt.Columns[j].ColumnName.ToString() == Grid_column_name[i])
{
dt.Columns[j].ColumnName = Grid_column_text[i].Replace("
", " “).Replace(” ", " ");
dt2.Columns.Add(dt.Columns[j].ColumnName, dt.Columns[j].DataType);
}
}
}

    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        dt2.Rows.Add();
        for (int j = 0; j <= dt2.Columns.Count - 1; j++)
        {
            dt2.Rows[i][j] = dt.Rows[i][dt2.Columns[j].ColumnName];
        }
    }
    return dt2;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tony-甲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值