C# 按货币代码设置列货币格式
将问题重新表述为按货币代码设置列格式。C# 按货币代码设置列货币格式,c#,epplus,C#,Epplus,将问题重新表述为按货币代码设置列格式。 使用EPPLUS导出数据表。 每行需要两种不同的货币。 按货币代码设置列货币格式的语法是什么? 将数据表传递到函数中并在列中循环。 按区域性名称设置“DateTimeFormat”的格式->正常工作。 需要按货币代码列出货币格式的语法。 谢谢 是否有一个参考,我可以在那里查找我们支持的其他27种货币代码的字符串格式 private void Export_Excel_EpPlus( string strWorksheetName, DataTable dt
使用EPPLUS导出数据表。
每行需要两种不同的货币。
按货币代码设置列货币格式的语法是什么?
将数据表传递到函数中并在列中循环。
按区域性名称设置“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();
}
在浏览完评论后,我正在编辑我的答案。 如果你需要欧元和英镑,那么你可以这样定义
//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;
}
我已经检查了上面的代码,它可以正常工作。如果您可以通过某种方式从区域性信息获取货币模式,则无需明确定义模式看起来必须使用货币代码而不是区域性名称。示例:格式货币代码“USD”或“GBP”或“EUR”。Sanjay,我正在按货币代码查找格式货币。您的答复是日期时间格式。谢谢。哦……很抱歉。如果您要查找的是货币,那么我认为这可能适用于工作表.Column(iColNumber).Style.Numberformat.Format=gb.Numberformat.ToString()。使用您的建议,Microsoft Excel错误消息中会出现以下结果:“Excel发现无法读取的内容…”工作表.Column(iColNumber).Style.Numberformat.Format=oCulture.Numberformat.ToString();这是两个不同的对象:OfficeOpenXml.Style.ExcelNumberFormat和System.Globalization.NumberFormatInfo,这可能导致Excel格式错误。@G.Highsmith我已编辑了上面的答案。它工作正常。看看这个。