C# Excel读取导入数据库碰到的问题

  无成本创业,低风险:

1.未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序。

下载并安装驱动:http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe

2.可能出现解析Excel时导致服务“Office Software Protection Platform”停止,不能进行excel的继续解析导致程序假死;

  a) 服务“Office Software Protection Platform”设置权限如下

   

  b) 设置服务的恢复状况

    

 3.转换excel

  

 private void ConvertExcel(string savePath)
        {
            //将xml文件转换为标准的Excel格式 
            Object Nothing = Missing.Value;//由于yongCOM组件很多值需要用Missing.Value代替   
            Excel.Application ExclApp = new Excel.ApplicationClass();// 初始化
            Excel.Workbook ExclDoc = null;
            try
            {
                ExclDoc = ExclApp.Workbooks.Open(savePath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);//打开Excl工作薄   
                Object format = Excel.XlFileFormat.xlWorkbookNormal;//获取Excl 2007文件格式   //Excel.XlFileFormat.xlWorkbookDefault;
                ExclApp.DisplayAlerts = false;
                ExclDoc.SaveAs(savePath, format, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);//保存为Excl 2007格式   
                //ExclDoc.SaveAs(savePath, format, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);//保存为Excl 2007格式   
            }
            catch (Exception ex)
            {
                LogEntry.TraceError("ConvertExcel", ex);
            }
            finally
            {
                if (ExclDoc != null)
                {
                    ExclDoc.Close(Nothing, Nothing, Nothing);
                }
                ExclApp.Quit();
                this.KillExcel();
            }
        }

 4.OLEDB读取excel数据

  

private DataTable GetExcelData(string filePath)
        {
            DataTable result = null;
            //string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1'", filePath);//Excel 8.0;HDR=YES;IMEX=1;//Excel 97-2003            
            //string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES';", filePath);//Excel 2007-2013
            string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=1';", filePath);//Excel 2007-2013            
            //this.KillExcel();            
            using (OleDbConnection oleDbConn = new OleDbConnection(strConn))
            {
                try
                {
                    oleDbConn.Open();
                    DataTable schemaTable = oleDbConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                    string tableName = "";
                    if (schemaTable != null && schemaTable.Rows.Count > 0)
                    {
                        tableName = schemaTable.Rows[0][2].ToString().Trim();//获取到第一个Sheet1名称作为表名            
                        string strExcel = "";
                        OleDbDataAdapter oleDbAdp = null;
                        DataSet ds = new DataSet();
                        strExcel = "select * from [" + tableName + "]";
                        oleDbAdp = new OleDbDataAdapter(strExcel, strConn);
                        oleDbAdp.Fill(ds, tableName);
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            result = ds.Tables[tableName];
                        }
                    }
                    else
                    {
                        LogEntry.TraceError("Excel文件读取错误,内容为空,文件路径:" + filePath, null);
                    }
                }
                catch (Exception ex)
                {
                    LogEntry.TraceError("读取Excel异常", ex);
                    this.ConvertExcel(filePath);//出现不能读取时,转换下文件,下次可以读取                    
                }
                finally
                {
                    oleDbConn.Close();
                }
            }
            return result;
        }

   3.当程序的宿主为windows服务时

  读取一个excel后可能导致不能读取下一个excel文件,问题原因暂时怀疑权限阻止导致,暂时没解决;

 

posted @ 2018-07-12 16:27  流浪者的飘  阅读(918)  评论(0编辑  收藏  举报