Insert formatted values as currency type while using EPPlus

人盡茶涼 提交于 2019-12-23 01:21:12

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/25154383/insert-formatted-values-as-currency-type-while-using-epplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!