zoukankan      html  css  js  c++  java
  • Winform datagridview Excel 导入导出

     

     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
        }

  • 相关阅读:
    【Linux 编程】进程间通信
    毕设进行时——4.3寸在富士通ARM中实现
    spcomm使用:在编译运行时为什么总出现"unable to open include file 'spcomm.hpp'"?
    Xilinx LVDS
    Xilinx selectIO
    xilinx 原理图输入
    http消息头(转)
    用java语言将数据库中的数据表转换为xml文件的通用程序(转)
    数据字典实例
    Web Service工作原理初探
  • 原文地址:https://www.cnblogs.com/swarb/p/9924448.html
Copyright © 2011-2022 走看看