OleDbConnection读取Excel

 <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnXLS" runat="server" OnClick="btnXLS_Click" Text="导入公告" />
View Code
protected void btnXLS_Click(object sender, EventArgs e)
        {
            long ID = 0;
            int iii = 0;
            try
            {
                string savePath = HttpContext.Current.Server.MapPath("~/") + "Upload/" + FileUpload1.FileName;
                if (File.Exists(savePath))
                {
                    File.Delete(savePath);
                }
                this.FileUpload1.PostedFile.SaveAs(savePath);
                DataTable dt = GetExcelToDataTableBySheet(savePath, "Sheet1$");
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                   
                    Dictionary<int, string> dict = new Dictionary<int, string>();
                    ID = long.Parse(dt.Rows[i]["F2"].ToString());
                    string gsgg = dt.Rows[i]["F9"].ToString();//公司公告
                    string jysc = dt.Rows[i]["F10"].ToString();//经营市场
                    string scyx = dt.Rows[i]["F11"].ToString();//生产运行
                    string bmgg = dt.Rows[i]["F12"].ToString();//部门公告
                    string zhgl = dt.Rows[i]["F13"].ToString();//综合管理
                    string aqzl = dt.Rows[i]["F14"].ToString();//安全质量
                    #region 
                    if (gsgg.Trim() == "")
                    {
                        dict.Add(0, "公司公告");
                    }
                    if (jysc.Trim() == "")
                    {
                        dict.Add(5, "经营市场");
                    }
                    if (scyx.Trim() == "")
                    {
                        dict.Add(8, "生产运行");
                    }
                    if (bmgg.Trim() == "")
                    {
                        dict.Add(9, "部门公告");
                    }
                    if (gsgg.Trim() == "")
                    {
                        dict.Add(7, "综合管理");
                    }
                    if (aqzl.Trim() == "")
                    {
                        dict.Add(15, "安全质量");
                    }
                    if (dict.Count == 0) { continue; }
                    #endregion
                    //string sql = string.Format(" delete from T_ADM_NoticePublicTypes where ID={0}   ",ID);
                    //foreach (var item in dict)
                    //{
                    //    sql += string.Format(" insert into T_ADM_NoticePublicTypes values ({0},{1},'{2}')",ID,item.Key,item.Value);
                    //}
                    //HX.DBUtility.DbPortalHelper.ExecuteSql(sql);
                    //T_ADM_NoticeBll.Instance.Execute4NoticePublicTypes(ID, dict);
                }
                Response.Write("2333333333");
            }
            catch (Exception ex)
            {
                Response.Write(iii+"---------------
");
                Response.Write(ex);
            }
        }
View Code
public static DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName)
        {
            //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; // 
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; // 
            string strExtension = System.IO.Path.GetExtension(FileFullPath);
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn); //("select * from [Sheet1$]", conn);
            odda.Fill(ds, SheetName);
            conn.Close();
            return ds.Tables[0];
        }
View Code
public bool Execute4NoticePublicTypes(long id, Dictionary<int, string> noticeTypes)
        {
            bool ret = false;
            List<CommandInfo> strSqlNoticeTypes = new List<CommandInfo>();
            try
            {
                string delSql = "delete from T_ADM_NoticePublicTypes where ID=@ID 
";
                SqlParameter[] cmdParmsDel = new SqlParameter[1];
                cmdParmsDel[0] = new SqlParameter("@ID ", id);

                strSqlNoticeTypes.Add(new CommandInfo(delSql, cmdParmsDel));

                foreach (var i in noticeTypes)
                {
                    string insertSql = "insert into T_ADM_NoticePublicTypes values (@ID, @Type,@TypeName) 
";

                    SqlParameter[] cmdParmsInsert = new SqlParameter[3];
                    cmdParmsInsert[0] = new SqlParameter("@ID", id);
                    cmdParmsInsert[1] = new SqlParameter("@Type", i.Key);
                    cmdParmsInsert[2] = new SqlParameter("@TypeName", i.Value);
                    strSqlNoticeTypes.Add(new CommandInfo(insertSql, cmdParmsInsert));
                }
                DbPortalHelper.ExecuteSqlTran(strSqlNoticeTypes);

                ret = true;
            }
            catch (Exception)
            {
                ret = false;
            }
            return ret;
        }
View Code

未在本地计算机上注册“Microsoft.Ace.OleDb.12.0

http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe

收藏
关注
评论
原文地址:https://www.cnblogs.com/yidengbone/p/8301697.html