使用EPPlus设置网格线颜色?

时间:2015-03-31 23:59:50

标签: c# excel epplus

是否可以使用设置工作表的网格线颜色?

"网格线颜色" 选项我尝试以编程方式设置,如下面的屏幕截图所示。

enter image description here

1 个答案:

答案 0 :(得分:2)

我在EPPlus中没有看到它的选项。可以使用xml手动设置属性:

[TestMethod]
public void Sheet_Gridline_Color_Test()
{
    //http://stackoverflow.com/questions/29380587/set-gridline-color-using-epplus

    //Throw in some data
    var dtMain = new DataTable("tblData");
    dtMain.Columns.Add(new DataColumn("Col1", typeof(int)));

    for (var i = 0; i < 20; i++)
    {
        var row = dtMain.NewRow();
        row["Col1"] = i;
        dtMain.Rows.Add(row);
    }

    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var pck = new ExcelPackage(existingFile))
    {
        var ws = pck.Workbook.Worksheets.Add("Content");
        ws.Cells["A1"].LoadFromDataTable(dtMain, true);

        //Can get xml elements quick and dirty using relative childs but should do a proper search in production
        var wsxd = ws.WorksheetXml;
        var wsxml = wsxd.LastChild; //gets 'worksheet'
        var sheetViewsXml = wsxml.FirstChild; //gets 'sheetViews'
        var sheetViewXml = sheetViewsXml.FirstChild; //gets 'sheetView'

        var att = wsxd.CreateAttribute("defaultGridColor");
        att.Value = "0";
        sheetViewXml.Attributes.Append(att);

        att = wsxd.CreateAttribute("colorId");
        att.Value = "10";
        sheetViewXml.Attributes.Append(att);

        pck.Save();
    }
}