如何使用EPPLUS导出excel时根据值动态设置列数据的前期颜色?

时间:2015-10-27 08:41:31

标签: c# asp.net export-to-excel epplus

我有一组代码可以使用EPPLUS将网格视图数据导出到Excel。我可以将数据导出到EPPLUS。但是如何将字体的颜色导出为ex​​cel呢?我可以设置字体颜色的动态编码,如果..... color = green,如果..... color = red?

因此我需要根据我设置的值对column4进行着色。

以下代码是我为gridview动态设置的方式:

function initMap() {
    var locations = [
        ['Bondi Beach', -33.890542, 151.274856, 4],
        ['Coogee Beach', -33.923036, 151.259052, 5],
        ['Cronulla Beach', -34.028249, 151.157507, 3],
        ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
        ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var maps = [];
    for (var i = 0; i < locations.length; i++) {
        console.log(locations[i][1]);
        $('.maps-wrapper').append('<div class="map" id="map' + i + '"></div>');

        var latlng = {lat: locations[i][1], lng: locations[i][2]};

        var mapOptions = {
           center: latlng,
           scrollwheel: false,
           zoom: 12,
           mapTypeId: google.maps.MapTypeId.ROADMAP,
           disableDefaultUI: true
       };

       var map = new google.maps.Map(document.getElementById('map' + i), mapOptions);
       maps.push(map);


       var marker = new google.maps.Marker({
          map: map,
          position: latlng,
          title: locations[i][0]
       });
    };
}

initMap();

我的出口代码:

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell cell = e.Row.Cells[16];
                int Num = int.Parse(cell.Text);
                if (Num >= 1&& Num <= 11)
                {
                    cell.ForeColor = Color.Green;
                }
                if (Num >= 12&& Num <= 39)
                {
                    cell.ForeColor = Color.Orange;
                }
..........

        }

那么我如何在导出代码中这样做才能在导出时动态导出或设置前色?

谢谢

1 个答案:

答案 0 :(得分:0)

此行之后

Grid.Cells["A1"].LoadFromDataTable(Gridview1, true);

循环以循环DataTable

的计数

示例

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();

        // Create Column
        for(int i = 0; i < 5; i++)
            dt.Columns.Add("column" + i, typeof(int));

        for (int i = 0; i < 10; i++)
            dt.Rows.Add(i, i+1, i+2, i+3, i+4);

        GenerateExcel(dt);
    }
}

private void GenerateExcel(DataTable dt)
{
    using (ExcelPackage pkg = new ExcelPackage())
    {
        ExcelWorksheet ws = pkg.Workbook.Worksheets.Add("Sheet1");
        int num = 0;
        string value = string.Empty;

        ws.Cells[1, 1].LoadFromDataTable(dt, true);

        // Get Your DataTable Count
        int count = dt.Rows.Count;
        int countCol = dt.Columns.Count;
        bool isHeader = true;


        for (int i = 0; i < count; i++ )
        {                
            // Set Border
            for (int j = 1; j <= countCol; j++)
            {
                // Set Border For Header. Run once only
                if (isHeader) ws.Cells[1, j].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin, System.Drawing.Color.Black);

                ws.Cells[2 + i, j].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin, System.Drawing.Color.Black);
            }

            // Set to false
            isHeader = false;

            // Cells 2 + i == Rows, Wondering start with 2 because your 1st rows is Title so 2 is the value
            // Cells 2 == Column make sure your column is fix
            value = ws.Cells[2 + i, 2].Value + ""; // You can use .ToString() if u sure is not null

            // Parse
            int.TryParse(value, out num);

            // Check
            if (num >= 1 && num <= 11)
                ws.Cells[2 + i, 2].Style.Font.Color.SetColor(System.Drawing.Color.Green);
            else if (num >= 12 && num <= 39)
                ws.Cells[2 + i, 2].Style.Font.Color.SetColor(System.Drawing.Color.Orange);
        }

        pkg.SaveAs(new FileInfo("C:\\Test.xlsx"));
    }
}