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並且應該包含一些特定的數據網格數據。我怎樣才能做到這一點?
遍歷DataGridView中的所有單元格,並執行所有你想要的操作。在通過DataSource(例如DataTable)填充它的情況下,您也可能依賴於LINQ。 – varocarbas