简体   繁体   中英

Set column currency format by currency code

Rephrasing question to format column by currency CODE.
Using EPPLUS for data table export.
Need two different currencies per row.
What is syntax to set column CURRENCY format by currency CODE?
Passing dataTable into function and looping through columns.
Format for "DateTimeFormat" by culture name -> works ok.
Need syntax for currency format by currency code. Thanks!
Is there a reference where I can look up the string formats for the other 27 currency codes we are supporting?

private void Export_Excel_EpPlus(
string strWorksheetName, DataTable dtReportData)
{
    CultureInfo oCulture;
    oCulture = new CultureInfo(sMyCultureName);
    using (ExcelPackage oEPkg = new ExcelPackage())
    {
        int iColNumber = 0;

        ExcelWorksheet oWorkSheet = oEPkg.Workbook.Worksheets.Add(strWorksheetName);

        //Load the datatable into the sheet, starting from cell A1
       oWorkSheet.Cells["A1"].LoadFromDataTable(dtReportData, true);

        //auto fix the columns
       oWorkSheet.Cells[oWorkSheet.Dimension.Address].AutoFitColumns();

        foreach (DataColumn oCol in dtReportData.Columns)
        {
            iColNumber++;

            if (oCol.DataType == typeof(DateTime))
            {
                // This works ok -> Use Culture - DateTimeFormat - ShortDatePattern
                oWorkSheet.Column(iColNumber).Style.Numberformat.Format = oCulture.DateTimeFormat.ShortDatePattern;
            }
            if (oCol.DataType == typeof(Decimal))
            {
                if (oCol.ColumnName == "TotalAmt_Currency1")
                {
                    // ??? NEED syntax to format by currency CODE (i.e. GBP)
                }
                if (oCol.ColumnName == "TotalAmt_Currency2")
                {
                     // ??? NEED syntax to format by currency CODE (i.e. EUR)
                }
            }
        }

        // Prepare the response
        HttpResponse ohttpResponse = Response;
        ohttpResponse.Clear();
        ohttpResponse.ClearHeaders();
        ohttpResponse.Buffer = true;
        ohttpResponse.Charset = "";
        ohttpResponse.ContentEncoding = System.Text.Encoding.UTF8;
        ohttpResponse.Cache.SetCacheability(HttpCacheability.NoCache);
        ohttpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        //Provide your file name here
        ohttpResponse.AddHeader("content-disposition", "attachment;filename=\"ExportFile.xlsx\"");
        //ohttpResponse.ContentEncoding 

        // Write the workbook to the Response.OutputStream
        using (MemoryStream oMemoryStream = new MemoryStream())
        {
            oEPkg.SaveAs(oMemoryStream);

oMemoryStream.WriteTo(ohttpResponse.OutputStream);
            oMemoryStream.Position = 0;
            oMemoryStream.Close();
        }

        ohttpResponse.Flush();
        ohttpResponse.End();
    }

I am editing my answer after going through comments. If euro and pound are your requirements then you can define it like this

//define this somewhere
 string sFormatEU = @"_([$€-2] * #,##0.00_);_([$€-2] * (#,##0.00);_([$€-2] * ""-""??_);_(@_)";
 string sFormatUK = @"_-[$£-809]* #,##0.00_-;-[$£-809]* #,##0.00_-;_-[$£-809]* ""-""??_-;_-@_-";

if (oCol.ColumnName == "TotalAmt_Currency1")
 {
     oWorkSheet.Column(iColNumber).Style.Numberformat.Format=sFormatUk;
 }
 if (oCol.ColumnName == "TotalAmt_Currency2")
  {
     oWorkSheet.Column(iColNumber).Style.Numberformat.Format=sFormatEU;
  }

I have checked the above code and it works fine.If you can somehow get currency pattern from Culture Info then no need for explicit definition of patterns

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