使用C#中的OfficeOpenXml在Excel单元格中设置DataType

时间:2017-02-02 16:03:46

标签: c# excel

我正在使用OfficeOpenXml库在C#中创建Excel文件。具体来说,我需要以编程方式将特定单元格的数据类型设置为EUR。例如,1234.5必须变成1234.5€。

使用UI,此操作相当简单RightClick - > Format Cells - >数字 - >货币 - >符号(见附图)。 enter image description here

以下是我的代码。有关如何做到这一点的任何线索?

string fullpath = @"\\SOME_PATH\test_file.xlsx";

// if file exists, overwrite
if (File.Exists(fullpath))
    File.Delete(fullpath);

var pck = new ExcelPackage(new FileInfo(fullpath));

var workSheet = pck.Workbook.Worksheets.Add("contract summary");

workSheet.Cell(1, 1).DataType = "Currency"; // this does not work.

workSheet.Cell(1, 1).Value = 1234.5.ToString();

2 个答案:

答案 0 :(得分:0)

我刚刚测试了以下代码,并成功使用using Excel = Microsoft.Office.Interop.Excel;库创建了自定义格式。

workSheet.Cells[1, 1].NumberFormat = "#,###,###.00 €";

我相信它也可用于列,行或单元组。

我不确定Microsoft.Office.Interop.ExcelOpenOfficeXML有多相似,但我希望至少可以指出正确的方向。

答案 1 :(得分:0)

您正在寻找的是

workSheet.Cells[1,1].Style.Numberformat.Format = "0.00 €";