Set Gridline Color Using EPPlus?

让人想犯罪 __ 提交于 2019-12-24 13:41:44

问题


Is it possible to set a worksheet's gridline color using epplus?

The "Gridline color" option I'm trying to set programmatically is shown in the screenshot below.


回答1:


I dont see an option for it in EPPlus. Could manually set the attributes using 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();
    }
}


来源:https://stackoverflow.com/questions/29380587/set-gridline-color-using-epplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!