与格式的Excel EPPLUS库列 [英] Formatting a column with EPPLUS Excel Library
问题描述
我写了一个C#程序创建一个Excel电子表格。所述片材具有多个列。我想格式化的一列
I wrote a C# program to create an excel spreadsheet. The sheet has multiple columns. I want to format ONE of the columns.
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();
的问题是,当它的格式化列正确的,它也格式化其它列的格式和不就在专栏中,我指定。
我也试过:
The problem is, while it's formatting the column correct, it's also formatting other columns with the format and not just the column I specified. I also tried:
ws.Column(1).Style.Numberformat.Format = "0.00";
这是一个错误还是我失去了一些东西?
Is this a bug or am I missing something?
推荐答案
您打开现有的文件?它可以具有格式已经给你打开它应用到其它列之前。 。还是喜欢astian模板说
Are you opening an existing file? It may have a format already applied to the other columns prior to you opening it. Or a template like astian said.
清除所有格式,以防万一是这样的:
Clear all the formatting just in case like this:
ws.Cells["A:D"].Style.Numberformat.Format = null;
ws.Cells["B:B"].Style.Numberformat.Format = "0.00";
在EPPlus 4.0.3
全部单元测试:
Full unit test in EPPlus 4.0.3:
[TestMethod]
public void Format_Single_Column_Test()
{
//http://stackoverflow.com/questions/28698226/formatting-a-column-with-epplus-excel-library
var excelDocName = @"c:\temp\temp.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();
}
这篇关于与格式的Excel EPPLUS库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!