2015-09-02 93 views
0

我使用這些代碼塊的所有數據網格的數據導出到Excel工作簿片(工作表Sheet1):如何將datagrid數據導出到Excel的不同工作表?

private void copyAlltoClipboard() 
{ 
    dataGridView1.SelectAll(); 
    DataObject dataObj = dataGridView1.GetClipboardContent(); 
    if (dataObj != null) 
     Clipboard.SetDataObject(dataObj); 
} 
private void button3_Click_1(object sender, EventArgs e) 
{ 
    copyAlltoClipboard(); 
    Microsoft.Office.Interop.Excel.Application xlexcel; 
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
    object misValue = System.Reflection.Missing.Value; 
    xlexcel = new Excel.Application(); 
    xlexcel.Visible = true; 
    xlWorkBook = xlexcel.Workbooks.Open("C:\\Test.xls", Type.Missing, true, Type.Missing, Type.Missing, 
               Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
               Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
               Type.Missing, Type.Missing); 
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
    Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1]; 
    CR.Select(); 
    xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);   
} 

但是,我想一些數據網格行導出到另一個工作表(Sheet2中),以及剩餘行到另一片(表Sheet 3)。所以,我應該將datagrid數據分成X塊。並且Excel工作表計數應該是相同的X並且應該包含一些特定的數據網格數據。我怎樣才能做到這一點?

+0

遍歷DataGridView中的所有單元格,並執行所有你想要的操作。在通過DataSource(例如DataTable)填充它的情況下,您也可能依賴於LINQ。 – varocarbas

回答

0

這是我要做的事,

Microsoft.Office.Interop.Excel.Application xlexcel; 
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet1;// defines sheet1 
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet2;// defines sheet2 
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet3;// defines sheet3 
    object misValue = System.Reflection.Missing.Value; 
    xlWorkBook = xlexcel.Workbooks.Add(misValue); 
    for (int i = 0; i < 2; i++) // here we add the rest of sheet into the excel 
    { 
     xlexcel.Sheets.Add(After: xlexcel.Sheets[xlexcel.Sheets.Count]); 
    }  
    xlWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //setting the first sheet equal to first sheet in excel 
    xlWorkSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);//setting the 2nd sheet equal to first sheet in excel 
    xlWorkSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3); 

//here we add the data to the sheet from datagridview  
    for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++) 
      { 
       xlWorkSheet1.Cells[1, j + 1] = dataGridView1.Columns[j].HeaderText; 
      } 
     for (int i = 0; i <= dataGridView1.RowCount - 1; i++) 
      { 
      for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++) 
        { 
         DataGridViewCell cell = dataGridView1[j, i]; 
         xlWorkSheet1.Cells[i + 2, j + 1] = cell.Value; 
        } 
      }      
      xlWorkSheet1.Name = "put name here"; 
      xlWorkSheet1.Columns.AutoFit(); 

     for (int j = 0; j <= dataGridView2.ColumnCount - 1; j++) 
      { 
       xlWorkSheet2.Cells[1, j + 1] = dataGridView2.Columns[j].HeaderText; 
      } 
     for (int i = 0; i <= dataGridView2.RowCount - 1; i++) 
      { 
      for (int j = 0; j <= dataGridView2.ColumnCount - 1; j++) 
        { 
         DataGridViewCell cell = dataGridView2[j, i]; 
         xlWorkSheet2.Cells[i + 2, j + 1] = cell.Value; 
        } 
      }      
      xlWorkSheet2.Name = "put name here"; 
      xlWorkSheet2.Columns.AutoFit(); 

     for (int j = 0; j <= dataGridView3.ColumnCount - 1; j++) 
      { 
       xlWorkSheet3.Cells[1, j + 1] = dataGridView3.Columns[j].HeaderText; 
      } 
     for (int i = 0; i <= dataGridView3.RowCount - 1; i++) 
      { 
      for (int j = 0; j <= dataGridView3.ColumnCount - 1; j++) 
        { 
         DataGridViewCell cell = dataGridView3[j, i]; 
         xlWorkSheet3.Cells[i + 2, j + 1] = cell.Value; 
        } 
      }      
      xlWorkSheet3.Name = "put name here"; 
      xlWorkSheet3.Columns.AutoFit(); 

xlWorkBook.SaveAs("put path here", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
        xlWorkBook.Close(true, misValue, misValue); 
        xlexcel.Quit(); 
相關問題