如何将数据网格数据导出到Excel的不同表格?

时间:2015-09-02 10:58:53

标签: c# excel datagridview

我使用以下代码块将所有datagrid数据导出到Excel工作簿表(Sheet1):

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),并将剩余的行导出到另一个工作表(Sheet3)。所以,我应该将datagrid数据分成X个部分。并且Excel工作表计数应该是相同的X并且应该包含一些特定的数据网格数据。我怎么能这样做?

1 个答案:

答案 0 :(得分: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();