2011-06-01 152 views
6

嘿,我可以將datagridview的數據導出爲ex​​cel。但datagridview的實際格式並未導出,即字體,顏色和空間。那麼,有沒有什麼最好的辦法來導出datagridview到excel,不僅是數據,還有外觀。datagridview導出爲ex​​cel

樣品外觀是: - enter image description here

+0

它看起來像你正在顯示某種報告。我建議你使用Crystal Reports或SSRS。他們已經建立了輸出爲其他格式的設施。如果您手動執行導出,則每次報告格式更改時都必須修改導出功能。 – Eranga 2011-06-01 05:50:28

回答

5

嘗試CSV出口

private void ToCsV(DataGridView dGV, string filename) 
    { 
     string separator = ","; 
     StringBuilder stOutput = new StringBuilder(); 
     // Export titles: 
     StringBuilder sHeaders = new StringBuilder(); 
     for (int j = 0; j < dGV.Columns.Count; j++) 
     { 
      sHeaders.Append(dGV.Columns[j].HeaderText); 
      sHeaders.Append(separator); 
     } 
     stOutput.AppendLine(sHeaders.ToString()); 
     // Export data. 
     for (int i = 0; i < dGV.RowCount - 1; i++) 
     { 
      StringBuilder stLine = new StringBuilder(); 
      for (int j = 0; j < dGV.ColumnCount; j++) 
      { 
       stLine.Append(Convert.ToString(dGV[j, i].Value)); 
       stLine.Append(separator); 
      } 
      stOutput.AppendLine(stLine.ToString()); 
     } 

     File.WriteAllText(filename, stOutput.ToString()); 
    } 
+0

我得到一個錯誤的'「System.Windows.Forms.DataGridViewRowCollection」不包含一個定義的「細胞」 \t'你能幫我 – Dotnet 2011-08-03 09:50:44

+0

這有這麼多的錯誤! dGV.Rows.Cells應該是for(int i = 0; i 2011-09-26 20:54:08

0

之前您在button_Click事件編寫代碼,您必須添加到的Microsoft.Office.Interop.Excel對象庫的引用。

右鍵單擊您的項目並選擇添加引用菜單。之後,轉到.NET選項卡並選擇並添加Microsoft.Office.Interop.Excel。

將下面的代碼寫入button_Click事件中。

  // button_Click event 

      private void button11_Click(object sender, EventArgs e) 
      { 
       // creating Excel Application 
       string fileName = String.Empty; 
       Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
       // creating new WorkBook within Excel application 
       Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
       // creating new Excelsheet in workbook 
       Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
       // see the excel sheet behind the program 
       app.Visible = true; 
       // get the reference of first sheet. By default its name is Sheet1. 
       // store its reference to worksheet 
       try 
       { 
        //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) 
        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; 
        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; 
        // changing the name of active sheet 
        worksheet.Name = "Exported from AMIT"; 
        // storing header part in Excel 
        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) 
        { 
          worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText; 
        } 
        // storing Each row and column value to excel sheet 
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
        { 
          for (int j = 0; j < dataGridView1.Columns.Count; j++) 
          { 
           worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); 
          } 
        } 


        // Save The Application 
        SaveFileDialog saveFileExcel = new SaveFileDialog(); 

        saveFileExcel.Filter = "Excel files |*.xls|All files (*.*)|*.*"; 
        saveFileExcel.FilterIndex = 2; 
        saveFileExcel.RestoreDirectory = true; 


        if (saveFileExcel.ShowDialog() == DialogResult.OK) 
        { 
          fileName = saveFileExcel.FileName; 
          //Fixed-old code :11 para->add 1:Type.Missing 
          workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

        } 
        else 
        { 
          return; 

          // Exit from the application 
          //app.Quit(); 
        } 
       } 
       catch (Exception) 
       { 
        //Statement; 
       } 
       finally 
       { 
        app.Quit(); 
        workbook = null; 
        app = null; 
       } 
      }