通过货币代码设置列货币格式

时间:2018-08-27 15:17:36

标签: c# epplus

重新说明问题以按货币CODE格式化列。
使用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();
    }

1 个答案:

答案 0 :(得分: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;
  }

我已经检查了上面的代码,它可以正常工作。如果您可以通过某种方式从文化信息中获取货币模式,则无需明确定义模式