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;
}

你可能感兴趣的:(c#,echarts,开发语言)