如何将datagridview控件上的数据引到excel...请高手指点一下.``
小弟没分了..请高手指教一下.``

解决方案 »

  1.   

    http://www.cnblogs.com/7402-shy/archive/2007/09/05/883407.html
      

  2.   


    数据导入Execl吗?还是引到,什么是引到呢?
      

  3.   

    是把数据转存到Excel吧,有很多方式写入。
    我有源码。
      

  4.   

    嗯..就是把datagridview显示的数据导入到excel中..路径啊什么的..谢谢5楼
      

  5.   

    #region 导出Excel表
            public void WriteToExcel(DataTable table)//查询结果导出Execl
            {
                try
                {
                    string tempImagePath = Application.StartupPath;//取你程序安装的目录
                    string temp = tempImagePath + "\\ExeclFiles";
                    Directory.CreateDirectory(@temp);
                    string strFilePath = @Application.StartupPath + "\\ExeclFiles\\" + LitianClass.GetServerTimeStr() + ".xls";
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, true, System.Text.Encoding.Default);
                    object[] values = new object[table.Columns.Count];
                    sw.Write("\r\n");
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        for (int j = 0; j < values.Length; ++j)
                        {
                            sw.Write(table.Rows[i][j].ToString());
                            sw.Write('\t');
                        }
                        sw.Write("\r\n");
                    }
                    sw.Flush();
                    sw.Close();
                    LitianClass.MessageBoxOK("成功导出[" + ds.Tables[0].Rows.Count.ToString() + "]行到Execl!");
                }
                catch
                {
                    LitianClass.MessageBoxNo("导出Execl失败!");
                }
            }
            #endregion
      

  6.   

    public static void ExportDataGridToExcel(DataGridView grid, string ReportTitle)
            ...{
                System.Data.DataTable myTable = (System.Data.DataTable)grid.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 (grid.Rows.Count > 0)
                    {
                        Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, grid.Columns.Count]);
                        range.MergeCells = true;
                        xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
                        xlApp.ActiveCell.Font.Size = 18;
                        xlApp.ActiveCell.Font.Bold = true;                    foreach (DataGridViewColumn colu in grid.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 < grid.Columns.Count; col++)
                            {
                                colIndex = colIndex + 1;
                                xlApp.Cells[rowIndex, colIndex] = grid.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 = ReportTitle;
                        xlApp.ActiveCell.Font.Size = 18;
                        xlApp.ActiveCell.Font.Bold = true;
                        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] = grid.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 e)
                {
                    throw e;
                }
                
            }
    http://topic.csdn.net/u/20081203/09/6bc987dc-7d65-466e-a2de-aefe02c6cbc5.html