【问题标题】:Set column currency format by currency code按货币代码设置列货币格式
【发布时间】:2018-08-30 04:49:47
【问题描述】:

改写问题以按货币代码格式化列。
使用 EPPLUS 进行数据表导出。
每行需要两种不同的货币。
按货币代码设置列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();
    }

【问题讨论】:

  • 看起来必须使用货币代码而不是文化名称。示例:格式化货币代码“USD”或“GBP”或“EUR”。

标签: c# epplus


【解决方案1】:

通过 cmets 后,我正在编辑我的答案。 如果欧元和英镑是您的要求,那么您可以这样定义

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

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

【讨论】:

  • Sanjay,我正在按货币代码查找格式货币。您的回复是针对日期时间格式的。谢谢。
  • 哦...对不起。如果您正在寻找货币,那么我认为这可能有效 oWorkSheet.Column(iColNumber).Style.Numberformat.Format=gb.NumberFormat.ToString()
  • 使用您的建议,Microsoft Excel 错误消息中会出现以下结果:“Excel 发现无法读取的内容...” oWorkSheet.Column(iColNumber).Style.Numberformat.Format = oCulture.NumberFormat.ToString();
  • 这是两个不同的对象:OfficeOpenXml.Style.ExcelNumberFormat 和 System.Globalization.NumberFormatInfo,它们可能会导致 Excel 格式错误。
  • @G.Highsmith 我在上面编辑了我的答案。它工作正常。检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 2017-01-20
  • 2014-01-17
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
相关资源
最近更新 更多