Winform datagridview Excel 导入导出

来源:互联网 发布:淘宝网数据包下载 编辑:程序博客网 时间:2024/05/01 21:48
 

 public class ExcelHelper
    {
        #region DataGidView数据到导出excel
   

   public static void ExportExcel(DataGridView myDGV)
        {
            SaveFileDialog savefile = new SaveFileDialog();
            savefile.Filter = "EXCEL文件|*.xls";
            if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
                if (excelapp == null)
                {
                    MessageBox.Show("无法创建Excel对象,可能你的机器上没有安装Excel!");
                    return;
                }
                Microsoft.Office.Interop.Excel.Workbook wbk = excelapp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet wst = (Microsoft.Office.Interop.Excel.Worksheet)wbk.Worksheets[1];
                try
                {
                    //写入标题
                    for (int i = 0; i < myDGV.ColumnCount; i++)
                    {
                        wst.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
                    }
                    //写入数据
                    for (int r = 0; r < myDGV.Rows.Count; r++)
                    {
                        for (int i = 0; i < myDGV.ColumnCount; i++)
                        {
                            wst.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
                        }
                        System.Windows.Forms.Application.DoEvents();
                    }
                    wst.Columns.EntireColumn.AutoFit();//列宽自适应
                    wbk.SaveAs(savefile.FileName);
                    MessageBox.Show("导出成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出失败:"+ex.Message,"导出异常");
                }


                //资源释放
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wst);
                wst = null;


                wbk.Close(null,null,null);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wbk);
                wbk = null;


                excelapp.Workbooks.Close();
                excelapp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelapp);
                excelapp = null;
                System.GC.Collect();
            }
        }


        #endregion

        #region Excel文件数据导入DataGidView
        /// <summary>
        /// 读取excel内的数据,返回DataTable
        /// </summary>
        /// <param name="strExcelFileName"></param>
        /// <returns></returns>
        public DataTable ImportExcel(string strExcelFileName)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + strExcelFileName + "';Excel 8.0; HDR=YES;IMEX=1;";
            string strExcel = string.Format("select * from [{0}$]", "sheet1");
            DataSet ds = new DataSet();
            using (OleDbConnection conn = new OleDbConnection(strConn))
            {
                conn.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                adapter.Fill(ds, "sheet1");
                return ds.Tables["sheet1"];
            }
        }
        #endregion
    }

原创粉丝点击