2016-10-31 28 views
2

我使用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希望印尼數字格式但它仍然顯示默認值。

+0

我想在Excel中試試這個。您創建的數字格式不正確。逗號*總是*千位分隔符,而點位*總是*小數點分隔符。顯示內容取決於您的系統區域設置或[在Excel中重寫的內容](http://www.howtogeek.com/245510/how-to-change-excels-decimal-separators-from-periods-to-commas/ )。 –

+0

[EPPlus數字格式]可能的副本(http://stackoverflow.com/questions/40209636/epplus-number-format) – VDWWD

+0

但是在印度尼西亞,NumberFormat Dot總是千位分隔符,而逗號總是小數點分隔符。 @Charles Mager –

回答

1

我之前使用過EP Plus,但是我不必更改小數格式。不過,試試這個,我認爲它會工作

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("id-ID"); 
+0

仍然無法使用@ Ahmed Ragheb –

0
private static void AddDataRows(Excel.Worksheet sheet, DataTable table, object[,] tempArray) 
    { 

      var range = sheet.Range(sheet.Cells[2, 1], sheet.Cells[(table.Rows.Count), (table.Columns.Count)]); 
      range.Value = tempArray; 
      sheet.Name = table.TableName; 
      for (int i = 0; i < table.Rows.Count; i++) 
      { 
       for (int j = 0; j < table.Columns.Count; j++) 
       { 
        if (table.Columns[j].DataType == typeof(decimal)) 
        { 
         sheet.Cells[i+2, j+1].Value = ConvertNumberFormat.ConvertToDecimal(Convert.ToDecimal(sheet.Cells[i+2 , j+1 ].Value)); 
        } 
       } 
      } 

    } 

創建轉換數字格式一個靜態方法。