2017-10-19 46 views
0

我將DataTable導出到Excel,其中一列是計算出的百分比。將DataTable導出爲ex​​cel NumberFormat百分比如何刪除多餘的小數點符號c#問題

我在DataGridView中顯示錶單中的數據,並在代碼中格式化列,以所需的格式顯示百分比值,即.99450294將爲99.45%。

關於導出我可以導出到Excel,並且同一列顯示相同的值,但是如果結果值爲100%,它會將其顯示爲100.%在百分號前添加小數位。

如何刪除符合導出100%值的項目的小數點符號?

這是我的代碼;

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Globalization; 
using System.IO; 
using System.Reflection; 
using System.Threading; 
using System.Windows.Forms; 
using Excel = Microsoft.Office.Interop.Excel; 

namespace CLStats 
{ 
public partial class FrmMain : Form 
{ 
    DataTable dt = new DataTable(); 

    public FrmMain() 
    { 
     InitializeComponent(); 
    } 

    private void BtnRunReport_Click(object sender, EventArgs e) 
    { 
     Cursor.Current = Cursors.WaitCursor; 

     int year = (int)NmUDYear.Value ; 
     int month = CbMonth.SelectedIndex+1; 
     SqlConnection conn = new SqlConnection(Globals.ConStatic); 
     SqlDataAdapter dataAdapter = new SqlDataAdapter($"SELECT T1.CODELINE AS Codeline, T1.CODELINELIMITS AS Limits, T1.TYPE AS Type, sum(t1.CONTROLFAILCOUNT) as ControlFailCount, sum(t1.CONTROLSENTCOUNT) as CONTROLSENTCOUNT, sum(t1.INDICATIONCOUNT) as INDICATIONCOUNT, 1 - (sum(t1.CONTROLFAILCOUNT)/(sum(T1.CONTROLSENTCOUNT) * 1.0000)) as SUCCESSRATIO FROM (SELECT tmdsDatabaseStatistics.dbo.tblStatisticsCodeStationMonth.CODELINE, tmdsDatabaseStatic.dbo.tblCodelines.Legacytype as Type, tmdsDatabaseStatic.dbo.tblCodelines.CodeLineLimits, tmdsDatabaseStatistics.dbo.tblStatisticsCodeStationMonth.CONTROLFAILCOUNT, tmdsDatabaseStatistics.dbo.tblStatisticsCodeStationMonth.CONTROLSENTCOUNT, tmdsDatabaseStatistics.dbo.tblStatisticsCodeStationMonth.INDICATIONCOUNT FROM tmdsDatabaseStatistics.dbo.tblStatisticsCodeStationMonth INNER JOIN tmdsDatabaseStatic.dbo.tblCodeLines ON tmdsDatabaseStatistics.dbo.tblStatisticsCodeStationMonth.codeline = tmdsDatabaseStatic.dbo.tblCodeLines.CodelineNumber WHERE month = {month} AND codeline <> 415 AND year ={year} AND CONTROLSENTCOUNT <> 0) AS T1 GROUP BY T1.CODELINE , T1.TYPE, T1.CODELINELIMITS ORDER BY TYPE, CONTROLFAILCOUNT DESC, CODELINE DESC", Globals.ConStatic); 
     conn.Open(); 
     dataAdapter.Fill(dt); 
     conn.Close(); 
     Dg.DataSource = dt; 
     Dg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; 
     Dg.CellFormatting += new DataGridViewCellFormattingEventHandler(Dg_CellFormatting); 

     Cursor.Current = Cursors.Default; 
    } 

    void Dg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (e.ColumnIndex == 6 && e.RowIndex != Dg.NewRowIndex) 
     { 
      e.CellStyle.Format = "P2"; 
     } 
    } 

    private void BtnSaveReport_Click(object sender, EventArgs e) 
    { 
     Cursor.Current = Cursors.WaitCursor; 
     Excel.Application xlApp; 
     CultureInfo CurrentCI = Thread.CurrentThread.CurrentCulture; 
     Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
     Excel.Workbook xlWorkBook; 
     Excel.Worksheet xlWorkSheet; 
     Excel.Range range; 
     object misValue = Missing.Value; 
     xlApp = new Excel.Application 
     { 
      DisplayAlerts = false 
     }; 

     xlWorkBook = xlApp.Workbooks.Add(misValue); 
     xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
     xlWorkSheet.Name = "Control Failures Report"; 

     for (var b = 0; b < dt.Columns.Count; b++) 
     { 
      xlWorkSheet.Cells[1, b + 1] = dt.Columns[b].ColumnName; 
      range = xlWorkSheet.Cells[1, b + 1]; 
      range.Interior.ColorIndex = 15; 
      range.Font.Bold = true; 

      range[2, 7].EntireColumn.NumberFormat = "#.####%"; 

     } 

     for (var r = 0; r < dt.Rows.Count; r++) 
     { 
      for (var a = 0; a < dt.Columns.Count; a++) 
      { 
       xlWorkSheet.Cells[r + 2, a + 1] = dt.Rows[r][a].ToString(); 
      } 

     } 

     Excel.Range columns = xlWorkSheet.UsedRange.Columns; 
     columns.AutoFit(); 
     xlWorkSheet.Rows[1].Insert(); 
     Excel.Range newRow = xlWorkSheet.Rows[1]; 
     Excel.Range newCell = newRow.Cells[1]; 
     newCell.Value = DateTime.Now.ToString("dd/MM/yyyy"); 

     xlWorkBook.SaveAs("ControlFailuresAuto.xlsx", 
      Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, false, false, 
      Excel.XlSaveAsAccessMode.xlNoChange, 
      Excel.XlSaveConflictResolution.xlUserResolution, true 
      , misValue, misValue, misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 

     ReleaseObject(xlWorkSheet); 
     ReleaseObject(xlWorkBook); 
     ReleaseObject(xlApp); 

     MessageBox.Show("Report created, you can find the file Documents\\ControlFailuresAuto.xlsx"); 
     Cursor.Current = Cursors.Default; 
    } 

    private void ReleaseObject(object obj) 
    { 
     try 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
      obj = null; 
     } 
     catch (Exception ex) 
     { 
      obj = null; 
      MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); 
     } 
     finally 
     { 
      GC.Collect(); 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     CbMonth.SelectedIndex = 0; 
    } 
} 

感謝

回答

1

嘗試改變號碼的格式#.00%,如果你想100%,無小數有一個條件,並有編號格式爲#%

+0

這沒有奏效。它將100%更改爲100.00%。對於任何等於100的閱讀100%的條目,我都需要100%。我確實需要在100之後的兩位小數。 –

+0

然後你需要條件如果100格式應該是#%,看到更新的部分。 – user2107843

+0

這就是我正在尋找的。我要添加/更改以查找條件? –