private void CopyGridToClipboard(DataGridView grid) { //Exclude row headers grid.RowHeadersVisible = false; //Include column headers grid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText; grid.SelectAll(); DataObject dataObj = grid.GetClipboardContent(); if (dataObj != null) Clipboard.SetDataObject(dataObj); //Set the visibility of row headers back grid.RowHeadersVisible = true; }
private void btnExport_Click(object sender, EventArgs e) { this.CopyGridToClipboard(dataGridView1); <code></code> //Open the excel application and add a workbook XL.Application application; XL.Workbook book; XL.Worksheet sheet; application = new XL.Application(); application.Visible = true; book = application.Workbooks.Add(); sheet = (XL.Worksheet)book.Worksheets[1]; //label1 Text in Cell[1,1] ((XL.Range)sheet.Cells[1, 1]).Value = this.label1.Text; //textBox1 Text in Cell[1,2] ((XL.Range)sheet.Cells[1, 2]).Value = this.combSOName.SelectedValue.ToString(); ((XL.Range)sheet.Cells[1, 4]).Value = this.label3.Text; //textBox1 Text in Cell[1,2] ((XL.Range)sheet.Cells[1, 5]).Value = this.comboPartno.Text; //Let row 3 empty //Paste grid into Cell[4,1] XL.Range gridRange = (XL.Range)sheet.Cells[6, 1]; gridRange.Select(); sheet.PasteSpecial(gridRange); this.CopyGridToClipboard(dataGridView2); XL.Range gridRange1 = (XL.Range)sheet.Cells[24, 1]; gridRange.Select(); sheet.PasteSpecial(gridRange1); }
Clipboard.SetDataObject
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)