• EXCEL导入到DataSet


     /// </summary>
            /// <param name="fileName">Excel全路径文件名</param>
            /// <returns>导入成功的DataSet</returns>
            public DataSet ImportExcel(string fileName)
            {
                //判断是否安装EXCEL
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                if (xlApp == null)
                {
                    _ReturnStatus = -1;
                    _ReturnMessage = "无法创建Excel对象,可能您的计算机未安装Excel";
                    return null;
                }
                //判断文件是否被其他进程使用            
                Microsoft.Office.Interop.Excel.Workbook workbook;
                try
                {
                    workbook = xlApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
                }
                catch(Exception ex)
                {
                    _ReturnStatus = -1;
                    _ReturnMessage = "Excel文件处于打开状态,请保存关闭,"+ex.Message;
                    return null;
                }
    
                //获得所有Sheet名称
                int n = workbook.Worksheets.Count;
                string[] SheetSet = new string[n];
                System.Collections.ArrayList al = new System.Collections.ArrayList();
                for (int i = 1; i <= n; i++)
                {
                    SheetSet[i - 1] = ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i]).Name;
                }
    
                //释放Excel相关对象
                workbook.Close(null, null, null);
                xlApp.Quit();
                if (workbook != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    workbook = null;
                }
                if (xlApp != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    xlApp = null;
                }
                GC.Collect();
    
                DataSet ds = new DataSet();
                string connStr = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + fileName + ";Extended Properties=Excel 8.0";
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    conn.Open();
                    OleDbDataAdapter da;
                    for (int i = 1; i <= n; i++)
                    {
                        string sql = "select * from [" + SheetSet[i - 1] + "$A:Z] ";
                        da = new OleDbDataAdapter(sql, conn);
                        da.Fill(ds, SheetSet[i - 1]);
                        da.Dispose();
                    }
                    conn.Close();
                    conn.Dispose();
                }
                return ds;
            }
    
  • 相关阅读:
    1.1 Recruitment 1.1.4 Sample Test(II)
    1.1 Recruitment 1.1.4 Sample Test(I)
    微信登录接口
    谷歌浏览器保留页面跳转前的请求
    积分墙项目接口文档
    @SneakyThrows
    security中使用单元测试
    Prometheus监控系统
    大数据-shell-脚本入门-开头格式、运行方式、多命令处理
    大数据-shell-概述
  • 原文地址:https://www.cnblogs.com/zhaolijing910/p/3683154.html
Copyright © 2020-2023  润新知