如何把DataGridView中数据导出到 文本文档 和 EXCEL 以及 如何把 文本文档和 EXCE中数据倒入到 DataGridView 希望大家帮忙
多谢了

解决方案 »

  1.   

    /// <summary>
            /// 以操作Excel控件的方式将DataGridView数据导出到Excel add by sunny 2007/1/18
            /// </summary>
            /// <param name="GridView">DataGridView对象</param>
            /// <param name="strExcelFile">Excel文件名</param>
            /// <param name="strError">out参数,返回出错信息</param>
            /// <returns>
            ///    -1 出错
            ///    0 成功
            /// </returns>
            public static int DataGridViewToExcel(System.Windows.Forms.DataGridView GridView,
                string strExcelFileName,
                out string strError)
            {
                strError = "";
                int nRet = 0;            Excel.Application xlApp = new Excel.Application();
                Excel.Workbooks workbooks = xlApp.Workbooks;
                Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1            try
                {
                    //~~
                    // 写字段名
                    for (int i = 0; i < GridView.Columns.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = GridView.Columns[i].HeaderText.ToString();
                    }                // 写记录
                    for (int i = 0; i < GridView.Rows.Count; i++)
                    {
                        for (int j = 0; j < GridView.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, j + 1] = GridView.Rows[i].Cells[j].Value.ToString(); ;
                        }
                    }                worksheet.Columns.EntireColumn.AutoFit();//自动适应每列的宽度 add by sunny.li
                    Excel.Range rg = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, GridView.Columns.Count]);
                    rg.Font.Bold = true;
                    workbook.Saved = true;
                    workbook.SaveCopyAs(strExcelFileName);
                   
                    // 关掉内存中的进程
                    xlApp.Quit();
                    nRet = 0;
                }
                catch (Exception ex)
                {
                    strError = ex.ToString();
                    nRet = -1;
                }            return nRet;
            }
      

  2.   

    Excel内容读入到datagridview中
            public const string EXCELCONNECTION = "Provider=Microsoft.Jet.OLEDB.4.0;"
                + " Extended Properties='Excel 8.0;"
                + " HDR=YES';";        public const string EXCEL_FILTER = "Microsoft Office Excel 工作簿 (﹡.xls)|*.xls";        #endregion        /// <summary>
            /// 将Excel文件的数据导出到DataTable
            /// </summary>
            /// <param name="strExcelFileName">Excel 文件名</param>
            /// <param name="dt">out参数,返回DataTable</param>
            /// <param name="strError">out参数,返回出错信息</param>
            /// <returns>
            ///    -1 出错
            ///     0  成功
            /// </returns>
            /// 注:本函数目前只导出Sheet1$表的数据
            public static int Excel2DataTable(string strExcelFileName,
                out DataTable dt,
                out string strError)
            {
                strError = "";
                int nRet = 0;
                dt = null;            if (String.IsNullOrEmpty(strExcelFileName) == true)
                {
                    strError = "strExcelFile参数不能为空";
                    return -1;
                }            if (File.Exists(strExcelFileName) == false)
                {
                    strError = "文件'" + strExcelFileName + "'不存在";
                    return -1;
                }            // 执行导出数据
                string strConnection = ExcelADOUtil.EXCELCONNECTION
                        + "Data Source=" + strExcelFileName + ";";
                OleDbConnection connection = new OleDbConnection(strConnection);
                connection.Open();
                try
                {
                    //-----------------------------------
                    string strCommand = "SELECT * FROM [Sheet1$]";                OleDbDataAdapter adapter = new OleDbDataAdapter(strCommand, connection);
                    dt = new DataTable();
                    int nCount = adapter.Fill(dt);                //-------------------------------------
                }
                catch (Exception ex)
                {
                    strError = ex.Message;
                    return -1;
                }
                finally
                {
                    connection.Close();
                }            return 0;
            }
      

  3.   

    把datagridview的数据导入的文本文件中
     /// <summary>
            /// exprot the data to txt
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnPrint_Click(object sender, EventArgs e)
            {
                string strLine = "";
                if (sgItemErrorList.Rows.Count < 1)
                {
                    AppInfo.ErrorMsg(this, "There Is No Record To Print");
                }
                else
                {
                    this.Cursor = Cursors.WaitCursor;
                    string filename = "C:\\CST.txt";                FileStream sr = File.Open(filename, FileMode.Create);
                    for (int i = 0; i < sgItemErrorList.Rows.Count; i++)
                    {  
                        strLine +="    "+sgItemErrorList.Rows[i].Cells[0].Value.ToString().PadRight(19,' ')
                                  + sgItemErrorList.Rows[i].Cells[1].Value.ToString().PadRight(34,' ')
                                  + sgItemErrorList.Rows[i].Cells[2].Value.ToString().PadRight(6,' ')
                                  + sgItemErrorList.Rows[i].Cells[3].Value.ToString().PadRight(9,' ')
                                  + sgItemErrorList.Rows[i].Cells[4].Value.ToString().PadRight(19,' ');
                        sw.WriteLine(strLine);
                        strLine = "";
                    }
                    sw.WriteLine("");
                    sw.WriteLine("                            *** End of report ***                            ");
                    sw.Close();
                    sr.Close();
                    System.Diagnostics.Process.Start("notepad.exe", "c:\\CST.txt");
                    this.Cursor = Cursors.Default;
                }
            }
      

  4.   

    Excel.Application 
    using 什么?
      

  5.   

    如何将文本文件的内容在datagridView中显示呢!
      

  6.   

    http://csharp123.blog.163.com/blog/static/18437108120113280334179/