按货币代码设置列货币格式

问题描述 投票:0回答:1

用货币CODE格式化列的问题。 使用EPPLUS进行数据表导出。 每行需要两种不同的货币。 通过货币CODE设置列CURRENCY格式的语法是什么? 将dataTable传递给函数并循环遍历列。 按文化名称格式“DateTimeFormat” - >工作正常。 需要货币格式的货币格式语法。谢谢! 是否有参考我可以查找我们支持的其他27种货币代码的字符串格式?

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();
    }
c# epplus
1个回答
0
投票

我在评论后编辑我的答案。如果欧元和英镑是您的要求,那么您可以像这样定义它

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

我检查了上面的代码,它工作正常。如果你能以某种方式获得文化信息的货币模式,那么就不需要明确定义模式

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.