【问题标题】:How to set CSS style in EPPLus?如何在 EPPLus 中设置 CSS 样式?
【发布时间】:2016-03-16 01:48:26
【问题描述】:

我正在使用 EPPlus 库从 DataTable 生成 Excel。

我正在修改用于循环遍历数据表和创建电子表格的代码。

用于设置单元格样式的代码如下:

td.Attributes.Add("style", @"mso-number-format:\@");

我有以下逻辑:

for (int row = 6; row <= totalRow; row++)
{
    for (int col = 1; col <= 9; col++)
    {
        string colVol = (string)ws.Cells[row, col].Value;
        bool isNumeric = long.TryParse(colVol, out n);
        if (isNumeric && colVol.Length > 10)
        {
           ws.Cells[row, col] //need to assign css style here

        }
    }
}

我需要使用 EPPlus 做同样的事情。

这可能吗?

【问题讨论】:

    标签: c# css datatable epplus


    【解决方案1】:

    要设置单元格的背景颜色,只需使用以下代码:

    ws.Cells[row, col].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    ws.Cells[row, col].Style.Fill.BackgroundColor.SetColor(Color.Red);
    

    为了设置与 Fill 属性相关的任何内容,您必须指定将用于实际填充单元格的 PatternType

    EPPlus 允许更广泛的格式化选项,但这些超出了本问题的范围。

    【讨论】:

    • 我需要像我提供的示例一样设置样式和属性
    • @gene 你能提供更多信息吗?例如,什么是“dt”?我根据我能推断出你想要的东西来回答。如果您提供有关您要完成的工作的更多信息,我将很乐意编辑我的答案:)
    • td 是一个表格单元格。我需要为单元格的值设置 css 样式,如示例中所示
    • @gene 好的。表格单元格属性来自什么对象?
    • 感谢您的帮助。这是我之前提交的帖子的链接,带有完整代码:stackoverflow.com/questions/36017247/…
    猜你喜欢
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2016-05-17
    • 2011-04-27
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多