2014-10-20 75 views
2

爲什麼我的價值觀,當我在我的DataGrid導出到Excel出口WPF的DataGrid到Excel

改變

串351732051316944成爲3,51732E + 14

和我的日期時間: 2014年1月1日02:09: 29.942變爲41641,09035

以及如何使其停止?

我用ExportToExcel

public class ExportToExcel<T> 
{ 
    public List<T> dataToPrint; 
    // Excel object references. 
    private Microsoft.Office.Interop.Excel.Application excelApp = null; 
    private Workbooks books = null; 
    private Workbook book = null; 
    private Sheets sheets = null; 
    private Worksheet sheet = null; 
    private Range range = null; 
    private Font font = null; 
    // Optional argument variable 
    private object optionalValue = Missing.Value; 


    /// Generate report and sub functions 
    public void GenerateReport() 
    { 
     try 
     { 
      if (dataToPrint != null) 
      { 
       if (dataToPrint.Count != 0) 
       { 
        Mouse.SetCursor(Cursors.Wait); 
        CreateExcelRef(); 
        FillSheet(); 
        OpenReport(); 
        Mouse.SetCursor(Cursors.Arrow); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show("Error while generating Excel report"); 
     } 
     finally 
     { 
      ReleaseObject(sheet); 
      ReleaseObject(sheets); 
      ReleaseObject(book); 
      ReleaseObject(books); 
      ReleaseObject(excelApp); 
     } 
    } 
    /// Make Microsoft Excel application visible 
    private void OpenReport() 
    { 
     excelApp.Visible = true; 
    } 
    /// Populate the Excel sheet 
    private void FillSheet() 
    { 
     object[] header = CreateHeader(); 
     WriteData(header); 
    } 
    /// Write data into the Excel sheet 
    private void WriteData(object[] header) 
    { 
     object[,] objData = new object[dataToPrint.Count, header.Length]; 

     for (int j = 0; j < dataToPrint.Count; j++) 
     { 
      var item = dataToPrint[j]; 
      for (int i = 0; i < header.Length; i++) 
      { 
       var y = typeof(T).InvokeMember 
     (header[i].ToString(), BindingFlags.GetProperty, null, item, null); 
       objData[j, i] = (y == null) ? "" : y.ToString(); 
      } 
     } 
     AddExcelRows("A2", dataToPrint.Count, header.Length, objData); 
     AutoFitColumns("A1", dataToPrint.Count + 1, header.Length); 
    } 
    /// Method to make columns auto fit according to data 
    private void AutoFitColumns(string startRange, int rowCount, int colCount) 
    { 
     range = sheet.get_Range(startRange, optionalValue); 
     range = range.get_Resize(rowCount, colCount); 
     range.Columns.AutoFit(); 
    } 
    /// Create header from the properties 
    private object[] CreateHeader() 
    { 
     PropertyInfo[] headerInfo = typeof(T).GetProperties(); 

     // Create an array for the headers and add it to the 
     // worksheet starting at cell A1. 
     List<object> objHeaders = new List<object>(); 
     for (int n = 0; n < headerInfo.Length; n++) 
     { 
      objHeaders.Add(headerInfo[n].Name); 
     } 

     var headerToAdd = objHeaders.ToArray(); 
     AddExcelRows("A1", 1, headerToAdd.Length, headerToAdd); 
     SetHeaderStyle(); 

     return headerToAdd; 
    } 
    /// Set Header style as bold 
    private void SetHeaderStyle() 
    { 
     font = range.Font; 
     font.Bold = true; 
    } 
    /// Method to add an excel rows 
    private void AddExcelRows(string startRange, int rowCount, int colCount, object values) 
    { 
     range = sheet.get_Range(startRange, optionalValue); 
     range = range.get_Resize(rowCount, colCount); 
     range.set_Value(optionalValue, values); 
    } 

    /// Create Excel application parameters instances 
    private void CreateExcelRef() 
    { 
     excelApp = new Microsoft.Office.Interop.Excel.Application(); 
     books = (Workbooks)excelApp.Workbooks; 
     book = (Workbook)(books.Add(optionalValue)); 
     sheets = (Sheets)book.Worksheets; 
     sheet = (Worksheet)(sheets.get_Item(1)); 
    } 

    /// Release unused COM objects 
    private void ReleaseObject(object obj) 
    { 
     try 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
      obj = null; 
     } 
     catch (Exception ex) 
     { 
      obj = null; 
      MessageBox.Show(ex.Message.ToString()); 
     } 
     finally 
     { 
      GC.Collect(); 
     } 
    } 
} 

類,我不知道從哪裏開始解決這個問題,但任何幫助表示讚賞

+0

不要使用Excel自動化,使用庫,而不是,有很多上的NuGet。 – DavidG 2014-10-20 12:27:55

+0

你有這樣的首選圖書館嗎? – Simon 2014-10-20 12:58:38

+0

我使用EPPlus http://epplus.codeplex.com/ – blindmeis 2014-10-20 13:24:40

回答

2

如果你有使用Excel自動化這樣,那麼你需要確保正確的格式應用於單元格。因此,在設置值之後,還要設置格式。例如,日期可能是這樣的:

range.set_Value(optionalValue, values); 
range.NumberFormat = "dd-mmm-yyyy"; 

此外,要防止號碼使用你所提到的格式顯示:

range.set_Value(optionalValue, values); 
range.NumberFormat = "0";