本文介绍了使用EPPlus设置网格线颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 epplus ?

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.

推荐答案

我在EPPlus中看不到该选项。可以使用xml手动设置属性:

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

这篇关于使用EPPlus设置网格线颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!