ExportToExcel中的NumberFormat根据文化

时间:2016-10-31 13:07:11

标签: c# asp.net asp.net-mvc-4 epplus

我正在使用ExcelPackage(EP Plus)进行ExportTo Excel.Here是我的代码......

 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();
    }

我无法获得正确的十进制格式。我想要印度尼西亚数字格式但仍显示默认值。

3 个答案:

答案 0 :(得分:1)

之前我使用过EP Plus,但我没有必要更改小数格式。但试试这个我觉得它会起作用

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

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

    }

创建一个静态方法来转换数字格式。

答案 2 :(得分:0)

CultureInfo c;
c = new CultureInfo("en-EN");
var Test= worksheet.Cells[row, 1].Value != null ? 
Convert.ToDouble(worksheet.Cells[row, 1].Value, c) : 0,