2014-02-17 74 views
0

我想導出2個datagridviews到Excel,但它缺少數據網格視圖的兩組數據的最後一行,我檢查了代碼和我不能看到我做錯了什麼?另外,它在導出時創建一個臨時文件,然後鎖定文件並且不會正常退出,只有重新啓動才能刪除文件?任何幫助將是偉大的代碼如下。例如,如果我保存爲test.xlsx我得到2個文件〜$ test.xlsx和test.xlsx,並且這兩個文件都被鎖定。導出到Excel保存文件打開,而不是導出第一個數據網格

private void exprtbtn_Click(object sender, EventArgs e) 
    { 

     Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
     Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
     try 
     { 

      // creating new Excelsheet in workbook 
      Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null; 
      Microsoft.Office.Interop.Excel._Worksheet worksheet2 = null; 

      // get the reference of first sheet. By default its name is Sheet1. 
      // store its reference to worksheet 
      worksheet1 = workbook.Sheets["Sheet1"]; 
      worksheet1 = workbook.ActiveSheet; 

      // changing the name of active sheet 
      worksheet1.Name = "Switch Totals"; 


      // storing header part in Excel 
      for (int i = 1; i < switchtotalgrd.Columns.Count + 1; i++) 
      { 
       worksheet1.Cells[1, i] = switchtotalgrd.Columns[i - 1].HeaderText; 
      } 

      // storing Each row and column value to excel sheet 
      for (int i = 0; i < switchtotalgrd.Rows.Count - 1; i++) 
      { 
       for (int j = 0; j < switchtotalgrd.Columns.Count; j++) 
       { 
        worksheet1.Cells[i + 2, j + 1] = switchtotalgrd.Rows[i].Cells[j].Value.ToString(); 
       } 
      } 
      // Adding second worksheet 
      int count = workbook.Worksheets.Count; 
      Excel.Worksheet addedSheet = workbook.Worksheets.Add(Type.Missing, 
      workbook.Worksheets[count], Type.Missing, Type.Missing); 


      // get the reference of first sheet. By default its name is Sheet1. 
      // store its reference to worksheet 
      worksheet2 = workbook.Sheets["Sheet2"]; 
      worksheet2 = workbook.ActiveSheet; 

      // changing the name of active sheet 
      worksheet2.Name = "Itemised Extn"; 


      // storing header part in Excel 
      for (int i = 1; i < fullresult.Columns.Count + 1; i++) 
      { 
       worksheet2.Cells[1, i] = fullresult.Columns[i - 1].HeaderText; 
      } 

      // storing Each row and column value to excel sheet 

      for (int i = 0; i < fullresult.Rows.Count - 1; i++) 
      { 
       for (int j = 0; j < fullresult.Columns.Count; j++) 
       { 
        if (fullresult.Rows[i].Cells[j].Value == null) 
        { 
         fullresult.Rows[i].Cells[j].Value = "NA"; 
        } 
        worksheet2.Cells[i + 2, j + 1] = fullresult.Rows[i].Cells[j].Value.ToString(); 
       } 
      } 

      // save the application 
      string fileName = String.Empty; 
      SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
      saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*"; 
      saveFileDialog1.FilterIndex = 2; 
      saveFileDialog1.RestoreDirectory = true; 

      if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       fileName = saveFileDialog1.FileName; 
       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; 
     } 
      //Catch all errors. 
     catch (System.Exception) 
     { 
     } 
     finally 
     { 
      workbook = null; 
      app = null; 
     } 
    } 

回答

0

你可能會想要再次檢查你的outer for循環,例如, for (int i = 0; i < fullresult.Rows.Count - 1; i++),不應該使用小於或等於?

由於您正在使用Excel com interop,因此您需要調用此方法才能在完成後釋放資源Marshal.ReleaseComObject

一個好的做法是使用try {...} catch {...} finally {...},並在finally塊中釋放com對象。

+0

謝謝你是把它設置爲<=固定它。只需要解決爲什麼它被鎖定。 –