Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert formatted values as currency type while using EPPlus

I am using format:

type ="$###,###,##0.00"

for currency and assigning the format type to the worksheet cells

eg. wrkSheet.Cells[0].Style.Numberformat.Format = formatType;

But this is inserted as text type in excel. I want this to be inserted as Currency or Number in order to continue to do analysis on the values inserted (sort, sum etc).

Currently as it is text type validations do not hold correct. Is there any way to force the type in which the formatted values can be inserted?

like image 843
user3913190 Avatar asked Dec 01 '22 01:12

user3913190


2 Answers

Your formatting is correct. You needs to covert values to its native types

Use this code, it should work:

using (var package = new ExcelPackage())
{
    var worksheet = package.Workbook.Worksheets.Add("Sales list - ");
    worksheet.Cells[1, 1].Style.Numberformat.Format = "$###,###,##0.00";
    worksheet.Cells[1, 1].Value =Convert.ToDecimal(24558.4780);

    package.SaveAs(new FileInfo(path));
}
like image 73
Mohd Iliyas Avatar answered Dec 04 '22 13:12

Mohd Iliyas


Indices start from 1 in Excel.

This code

using (var package = new ExcelPackage())
{
    var worksheet = package.Workbook.Worksheets.Add("Sales list - ");
    worksheet.Cells[1, 1].Style.Numberformat.Format = "$###,###,##0.00";
    worksheet.Cells[1, 1].Value = 24558.4780;

    package.SaveAs(new FileInfo(path));
}

produces $24 558,48 for me

like image 36
Artiom Avatar answered Dec 04 '22 12:12

Artiom