简体   繁体   中英

Set DataType in Excel Cell using OfficeOpenXml in C#

I am using the OfficeOpenXml library to create Excel files in C#. Specifically, I need to set the datatype of a specific cell to EUR programmatically. For example 1234.5 must be turned into 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; 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.

您正在寻找的是

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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