我使用ExcelPackage(EP加)爲ExportTo Excel.Here是我的代碼的NumberFormat在ExportToExcel ...根據文化
public static void ExportDataSetToExcel(DataSet ds, string FileNameWithExtension)
{
//Using EPPLUS to export Spreadsheets
ExcelPackage pck = new ExcelPackage();
foreach (System.Data.DataTable table in ds.Tables)
{
var ws = pck.Workbook.Worksheets.Add(table.TableName);
ws.Cells["A1"].LoadFromDataTable(table, true);
ws.Cells["A1:CC1"].Style.Font.Bold = true;
for (int iCount = 0; iCount < table.Columns.Count; iCount++)
{
if (table.Columns[iCount].DataType == typeof(decimal))
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
string Pattern1 = string.Format("0{0}00", cultureInfo.NumberFormat.CurrencyDecimalSeparator);
string Pattern2 = string.Format("#{1}##0{0}00", cultureInfo.NumberFormat.NumberDecimalSeparator, cultureInfo.NumberFormat.NumberGroupSeparator);
ws.Column(iCount + 1).Style.Numberformat.Format = Pattern2;
}
if (table.Columns[iCount].DataType == typeof(int))
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
string Pattern1 = string.Format("0{0}00", cultureInfo.NumberFormat.CurrencyDecimalSeparator);
string Pattern2 = string.Format("#{1}##0{0}00", cultureInfo.NumberFormat.CurrencyDecimalSeparator, cultureInfo.NumberFormat.CurrencyGroupSeparator);
ws.Column(iCount + 1).Style.Numberformat.Format = "0";
}
if (table.Columns[iCount].DataType == typeof(DateTime))
{
ws.Column(iCount + 1).Style.Numberformat.Format = "dd-mm-yyyy";
}
}
}
pck.SaveAs(System.Web.HttpContext.Current.Response.OutputStream);
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + FileNameWithExtension);
System.Web.HttpContext.Current.Response.End();
}
我無法得到正確的十進制format.I希望印尼數字格式但它仍然顯示默認值。
我想在Excel中試試這個。您創建的數字格式不正確。逗號*總是*千位分隔符,而點位*總是*小數點分隔符。顯示內容取決於您的系統區域設置或[在Excel中重寫的內容](http://www.howtogeek.com/245510/how-to-change-excels-decimal-separators-from-periods-to-commas/ )。 –
[EPPlus數字格式]可能的副本(http://stackoverflow.com/questions/40209636/epplus-number-format) – VDWWD
但是在印度尼西亞,NumberFormat Dot總是千位分隔符,而逗號總是小數點分隔符。 @Charles Mager –