WinForm imports data from the DataGrid into Excel and displays the Excel application

Source: Internet
Author: User
Tags foreach count range first row tostring
datagrid|excel| Program | data | show

<summary>
Import the data from the DataGrid into Excel and display the Excel application.
Note Calling this method must have an Excel 2000 application installed, and assume that the DataGrid is bound to a dataset
</summary>
<param name= "Grid" ></param>
<param name= "Reporttitle" ></param>
public static void Exportdatagridtoexcel (DataGrid grid,string reporttitle)
{
DataTable myTable = (DataSet) grid. DataSource). Tables[0];

Try
{
Excel.Application xlapp = new Excel.applicationclass ();

int rowIndex;
int colindex;

RowIndex = 2;
Colindex = 0;

Excel.Workbook xlbook =xlapp.workbooks.add (true);

if (grid. Tablestyles.count >0)
{
Excel.Range Range = Xlapp.get_range (Xlapp.cells[1,1],xlapp.cells[1,grid. Tablestyles[0]. Gridcolumnstyles.count]);
Range. MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = Reporttitle;
XlApp.ActiveCell.Font.Size = 18;
XlApp.ActiveCell.Font.Bold = true;

foreach (DataGridColumnStyle Colu in grid. Tablestyles[0]. GridColumnStyles)
{
Colindex=colindex +1;
Xlapp.cells[2,colindex] = Colu. HeaderText;
}

All rows of the resulting table, assigned to cells
for (int row = 0;row < mytable.rows.count;row++)
{
RowIndex = RowIndex + 1;
Colindex = 0;
for (int col=0;col<grid. Tablestyles[0]. gridcolumnstyles.count;col++)
{
Colindex = Colindex + 1;
Xlapp.cells[rowindex, Colindex] = Grid[row,col]. ToString ();
}
}
}
Else
{
Excel.Range Range = Xlapp.get_range (Xlapp.cells[1,1],xlapp.cells[1,mytable.columns.count]);
Range. MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = Reporttitle;
XlApp.ActiveCell.Font.Size = 18;
XlApp.ActiveCell.Font.Bold = true;

Fill in the first row of Excel with the field names in the table
foreach (DataColumn Col in Mytable.columns)
{
Colindex = Colindex + 1;
Xlapp.cells[2, Colindex] = col.columnname;
}

All rows of the resulting table, assigned to cells
for (int row = 0;row < mytable.rows.count;row++)
{
RowIndex = RowIndex + 1;
Colindex = 0;
for (int col=0;col<mytable.columns.count;col++)
{
Colindex = Colindex + 1;
Xlapp.cells[rowindex, Colindex] = Grid[row,col]. ToString ();
}
}
}

Xlapp.get_range (xlapp.cells[2, 1], xlapp.cells[2, Colindex]). Font.Bold = true;
Xlapp.get_range (xlapp.cells[2, 1], Xlapp.cells[rowindex, Colindex]). Borders.LineStyle = 1;

XlApp.Cells.EntireColumn.AutoFit ();
XlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
XlApp.Cells.HorizontalAlignment = Excel.Constants.xlCenter;

Xlapp.visible = true;
}
catch (Exception e)
{
Throw e;
}


}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.