简体   繁体   English

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

[英]Set DataType in Excel Cell using OfficeOpenXml in C#

I am using the OfficeOpenXml library to create Excel files in C#. 我正在使用OfficeOpenXml库在C#中创建Excel文件。 Specifically, I need to set the datatype of a specific cell to EUR programmatically. 具体来说,我需要以编程方式将特定单元格的数据类型设置为EUR。 For example 1234.5 must be turned into 1234.5 €. 例如,1234.5必须变成1234.5€。

With the UI, this operation is fairly easy RightClick -->Format Cells --> Number --> Currency --> Symbol (see attached image). 使用用户界面,此操作相当容易。右键单击->设置单元格格式->数字->货币->符号(请参见下图)。 在此处输入图片说明 .

Below is my code. 下面是我的代码。 Any clue on how to do that? 关于如何做到这一点的任何线索?

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

I just tested the following code, and it successfully creates a custom format with the using Excel = Microsoft.Office.Interop.Excel; 我刚刚测试了以下代码,并且using Excel = Microsoft.Office.Interop.Excel;成功创建了一种自定义格式using Excel = Microsoft.Office.Interop.Excel; library. 图书馆。

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

I believe it can also be used on columns, rows, or groups of cells. 我相信它也可以用于列,行或单元格组。

I'm not sure how similar Microsoft.Office.Interop.Excel is to OpenOfficeXML , but I hope that can at least point you in the right direction. 我不确定Microsoft.Office.Interop.ExcelOpenOfficeXML有何相似之处,但我希望至少可以为您指明正确的方向。

您正在寻找的是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM