How to trans data from datagridview to excel

解决方案 »

  1.   

    ASP.Net 2.0: Export GridView to Excel http://www.cnblogs.com/sparon/archive/2007/04/06/702946.html
      

  2.   

     public bool exportToExcel(DataGridView dataGridView1, string Title)
            {
                System.Data.DataTable myTable = (System.Data.DataTable)dataGridView1.DataSource;            try
                {
                    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();                int rowIndex;
                    int colIndex;                rowIndex = 2;
                    colIndex = 0;                Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);                if (dataGridView1.Rows.Count > 0)
                    {
                        Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, dataGridView1.Columns.Count]);
                        range.MergeCells = true;
                        xlApp.ActiveCell.FormulaR1C1 = Title;
                        xlApp.ActiveCell.Font.Size = 18;
                        xlApp.ActiveCell.Font.Bold = true;                    foreach (DataGridViewColumn colu in dataGridView1.Columns)
                        {
                            colIndex = colIndex + 1;
                            xlApp.Cells[2, colIndex] = colu.HeaderText;
                        }                    //得到的表所有行,赋值给单元格                    for (int row = 0; row < myTable.Rows.Count; row++)
                        {
                            rowIndex = rowIndex + 1;
                            colIndex = 0;
                            for (int col = 0; col < dataGridView1.Columns.Count; col++)
                            {
                                colIndex = colIndex + 1;
                                xlApp.Cells[rowIndex, colIndex] = dataGridView1.Rows[row].Cells[col].Value;
                            }
                        }
                    }
                    else
                    {
                        Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]);
                        range.MergeCells = true;
                        xlApp.ActiveCell.FormulaR1C1 = Title;
                        xlApp.ActiveCell.Font.Size = 18;
                        xlApp.ActiveCell.Font.Bold = true;                    //将表中的栏位名称填到Excel的第一行                    foreach (DataColumn Col in myTable.Columns)
                        {
                            colIndex = colIndex + 1;
                            xlApp.Cells[2, colIndex] = Col.ColumnName;
                        }                    //得到的表所有行,赋值给单元格                    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] = dataGridView1.Rows[row].Cells[col].Value;
                            }
                        }
                    }
                    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 = Microsoft.Office.Interop.Excel.Constants.xlCenter;
                    xlApp.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
                    xlApp.Visible = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return true;
            }
      

  3.   

    http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx