加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用EPPLUS Excel Library格式化列

发布时间:2020-12-15 06:36:37 所属栏目:百科 来源:网络整理
导读:我写了一个C#程序来创建一个excel电子表格.工作表有多个列.我想格式化一列. aFile = new FileInfo(excelDocName); // excelDocName is a stringExcelPackage pck = new ExcelPackage(aFile);var ws = pck.Workbook.Worksheets.Add("Content");ws.View.ShowGr
我写了一个C#程序来创建一个excel电子表格.工作表有多个列.我想格式化一列.
aFile = new FileInfo(excelDocName); // excelDocName is a string
ExcelPackage pck = new ExcelPackage(aFile);
var ws = pck.Workbook.Worksheets.Add("Content");
ws.View.ShowGridLines = true;
ws.Cells["B:B"].Style.Numberformat.Format = "0.00";
ws.Cells[1,1].Value = "AA";
ws.Cells[1,2].Value = "BB";
ws.Cells[1,3].Value = "CC";
ws.Cells[1,4].Value = "DD";
for (int row = 2; row <= 10; ++row)
  for (int col = 1; col <= 4; ++col)
  {
  ws.Cells[row,col].Value = row * col;
  }
ws.Row(1).Style.Font.Bold = true;
pck.Save();

问题是,当格式化列正确时,它也格式化格式的其他列,而不仅仅是我指定的列.
我也试过:

ws.Column(1).Style.Numberformat.Format = "0.00";

这是一个bug还是我错过了什么?

解决方法

你打开现有文件吗?在打开之前,它可能已经应用于其他列的格式.或者像阿斯坦这样的模板说.

清除所有的格式,以防万一如下:

ws.Cells["A:D"].Style.Numberformat.Format = null;
ws.Cells["B:B"].Style.Numberformat.Format = "0.00";

EPPlus 4.0.3中的全面测试:

[TestMethod]
public void Format_Single_Column_Test()
{
    //https://stackoverflow.com/questions/28698226/formatting-a-column-with-epplus-excel-library
    var excelDocName = @"c:temptemp.xlsx";
    var aFile = new FileInfo(excelDocName); // excelDocName is a string

    if (aFile.Exists)
        aFile.Delete();

    ExcelPackage pck = new ExcelPackage(aFile);
    var ws = pck.Workbook.Worksheets.Add("Content");
    ws.View.ShowGridLines = true;
    ws.Cells["A:D"].Style.Numberformat.Format = null;
    ws.Cells["B:B"].Style.Numberformat.Format = "0.00";
    ws.Cells[1,1].Value = "AA";
    ws.Cells[1,2].Value = "BB";
    ws.Cells[1,3].Value = "CC";
    ws.Cells[1,4].Value = "DD";
    for (int row = 2; row <= 10; ++row)
        for (int col = 1; col <= 4; ++col)
        {
            ws.Cells[row,col].Value = row*col;
        }
    ws.Row(1).Style.Font.Bold = true;
    pck.Save();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读