Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将datagrid数据导出到Excel';不同的床单是什么?_C#_Excel_Datagridview - Fatal编程技术网

C# 如何将datagrid数据导出到Excel';不同的床单是什么?

C# 如何将datagrid数据导出到Excel';不同的床单是什么?,c#,excel,datagridview,C#,Excel,Datagridview,我使用以下代码块将所有datagrid数据导出到Excel工作簿表(Sheet1): private void copyAlltoClipboard() { dataGridView1.SelectAll(); DataObject dataObj = dataGridView1.GetClipboardContent(); if (dataObj != null) Clipboard.SetDataObject(dataObj); } private v

我使用以下代码块将所有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);          
}
但是,我想将一些datagrid行导出到另一个工作表(Sheet2),将其余的行导出到另一个工作表(Sheet3)。所以,我应该将datagrid数据分成X个部分。Excel工作表的计数应该是相同的X,并且应该包含一些特定的datagrid数据。我该怎么做?

我就是这样做的

    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();
Microsoft.Office.Interop.Excel.Application-xlexcel;
Microsoft.Office.Interop.Excel.工作簿;
Microsoft.Office.Interop.Excel.Worksheet XLWorksheet 1;//定义表1
Microsoft.Office.Interop.Excel.Worksheet XLWorksheet 2;//定义表2
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet3;//定义表3
对象错误值=System.Reflection.Missing.Value;
xlWorkBook=xlexcel.Workbooks.Add(错误值);
对于(int i=0;i<2;i++)//这里我们将工作表的其余部分添加到excel中
{
xlexcel.Sheets.Add(在:xlexcel.Sheets[xlexcel.Sheets.Count]之后);
}       
xlWorkSheet1=(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_项(1)//将第一张图纸设置为excel中的第一张图纸
xlWorkSheet2=(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_项(2)//将第二张图纸设置为excel中的第一张图纸
xlWorkSheet3=(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_项(3);
//这里,我们从datagridview将数据添加到工作表中

for(int j=0;j)循环遍历DataGridView中的所有单元格,并执行您希望执行的所有操作。如果通过DataSource(例如DataTable)填充它,您也可以依赖LINQ。