Click here to Skip to main content
16,021,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I copied datagridview data to clipboard and paste it in excel.
But I want border to all cells in datagridview when exported in excel.

What I have tried:

private void CopyGridToClipboard(DataGridView grid)
{
//Exclude row headers
grid.RowHeadersVisible = false;
grid.BorderStyle = BorderStyle.FixedSingle;

//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)
{


//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];

this.CopyGridToClipboard(dataGridView1);
XL.Range gridRange = (XL.Range)sheet.Cells[9, 2];
gridRange.Select();
sheet.PasteSpecial(gridRange);
}
Posted
Comments
Member 12305778 17-Nov-17 23:47pm    
Please help if anyone understand my query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900