EPPlus number format

后端 未结 4 1185
一整个雨季
一整个雨季 2020-11-28 04:38

I have an Excel sheet generated with Epplus, I am experiencing some pain points and I wish to be directed by someone who have solved a similar challenge.

I need to ap

相关标签:
4条回答
  • 2020-11-28 05:04

    And if you want to format a specific column like column "B" to number format you can do it this way-

    using (var package = new ExcelPackage())
    {
      var worksheet = package.Workbook.Worksheets.Add("SHEET1");
      worksheet.Cells["A1"].LoadFromDataTable(dataTable, PrintHeaders: true);
      for (var col = 1; col < dataTable.Columns.Count + 1; col++)
      {
        if (col == 2)//col number 2 is equivalent to column B
        {
          worksheet.Column(col).Style.Numberformat.Format = "#";//apply the number formatting you need
        }
        worksheet.Column(col).AutoFit();
      }
      return File(package.GetAsByteArray(), XlsxContentType, "report.xlsx");//downloads file
    }
    
    0 讨论(0)
  • 2020-11-28 05:15

    Here are some number format options for EPPlus:

    //integer (not really needed unless you need to round numbers, Excel will use default cell properties)
    ws.Cells["A1:A25"].Style.Numberformat.Format = "0";
    
    //integer without displaying the number 0 in the cell
    ws.Cells["A1:A25"].Style.Numberformat.Format = "#";
    
    //number with 1 decimal place
    ws.Cells["A1:A25"].Style.Numberformat.Format = "0.0";
    
    //number with 2 decimal places
    ws.Cells["A1:A25"].Style.Numberformat.Format = "0.00";
    
    //number with 2 decimal places and thousand separator
    ws.Cells["A1:A25"].Style.Numberformat.Format = "#,##0.00";
    
    //number with 2 decimal places and thousand separator and money symbol
    ws.Cells["A1:A25"].Style.Numberformat.Format = "€#,##0.00";
    
    //percentage (1 = 100%, 0.01 = 1%)
    ws.Cells["A1:A25"].Style.Numberformat.Format = "0%";
    
    //accounting number format
    ws.Cells["A1:A25"].Style.Numberformat.Format = "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* \"-\"??_-;_-@_-";
    

    Don't change the decimal and thousand separators to your own localization. Excel will do that for you.

    By request some DateTime formatting options.

    //default DateTime pattern
    worksheet.Cells["A1:A25"].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
    
    //custom DateTime pattern
    worksheet.Cells["A1:A25"].Style.Numberformat.Format = "dd-MM-yyyy HH:mm";
    
    0 讨论(0)
  • 2020-11-28 05:22

    Addition to Accepted Answer, because value Accept Object you must pass Number to Value For Example if your input is in string :

    var input = "5";    
    ws.Cells["A1:A25"].Value = double.Parse(input);
    
    0 讨论(0)
  • 2020-11-28 05:22

    Another addition to the accepted answer: you can use nullable values and the formatting all looks good BUT it ends up being a string in Excel and you can't SUM, AVG etc.

    So make sure you use the actual Value of the nullable.

    0 讨论(0)
提交回复
热议问题