C#如何把加密的Excel批量导入sql数据库

我是笨蛋宁宁 2014-08-25 11:12:45
各位大虾,我在做数据库的批量导入,读取的时候发现Excel是加密的,密码我知道(假设为:0),链接excel是这样子的:" Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filepath + ";Extended Properties='Excel 8.0;HDR=yes;IMEX=1';Jet OLEDB:Database Password=0;Persist Security Info=True;";,,但是每次都报错说无法解密加密文件。。。求各位大虾帮帮忙、、、、
...全文
163 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kankan_2532462061 2014-08-27
  • 打赏
  • 举报
回复
批量将Excel文件保存到SQL Server数据库 //导入到sql server数据库事件 private void btn_Export_Click(object sender, EventArgs e) { string[] P_str_Names = txt_Path.Text.Split(',');//存储所有选择的Excel文件名 string P_str_Name = "";//存储遍历到的Excel文件名 List<string> P_list_SheetNames = new List<string>();//实例化泛型集合对象,用来存储工作表名称 for (int i = 0; i < P_str_Names.Length - 1; i++)//遍历所有选择的Excel文件名 { P_str_Name = P_str_Names[i];//记录遍历到的Excel文件名 P_list_SheetNames = GetSheetName(P_str_Name);//获取Excel文件中的所有工作表名 for (int j = 0; j < P_list_SheetNames.Count; j++)//遍历所有工作表 { if (ckbox_Windows.Checked)//如果用Windows身份验证登录Sql Server ImportDataToSql(P_str_Name, P_list_SheetNames[j], "Data Source=" + txt_Server.Text + ";Initial Catalog =" + cbox_Server.Text + ";Integrated Security=SSPI;");//将工作表内容导出到Sql Server else if (ckbox_SQL.Checked)//如果用Sql Server身份验证登录Sql Server ImportDataToSql(P_str_Name, P_list_SheetNames[j], "Data Source=" + txt_Server.Text + ";Database=" + cbox_Server.Text + ";Uid=" + txt_Name.Text + ";Pwd=" + txt_Pwd.Text + ";");//将工作表内容导出到Sql Server } } MessageBox.Show("已经将所有选择的Excel工作表导入到了Sql Server数据库中!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } //对数据库写入的操作 public void ImportDataToSql(string P_str_Excel, string P_str_SheetName, string P_str_SqlCon)//将工作表内容导出到Sql Server { DataSet myds = new DataSet();//实例化数据集对象 try { //获得全部数据 string P_str_OledbCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + P_str_Excel + ";Extended Properties=Excel 8.0;"; OleDbConnection oledbcon = new OleDbConnection(P_str_OledbCon);//实例化Oledb数据库连接对象 string P_str_ExcelSql = "";//定义变量,用来记录要执行的Excel查询语句 OleDbDataAdapter oledbda = null;//实例化Oledb数据桥接器对象 P_str_ExcelSql = string.Format("select * from [{0}$]", P_str_SheetName);//记录要执行的Excel查询语句 oledbda = new OleDbDataAdapter(P_str_ExcelSql, P_str_OledbCon);//使用数据桥接器执行Excel查询 oledbda.Fill(myds, P_str_SheetName);//填充数据 string P_str_CreateSql = string.Format("create table {0}(", P_str_SheetName);//定义变量,用来记录创建表的SQL语句 foreach (DataColumn c in myds.Tables[0].Columns)//遍历数据集中的所有行 { P_str_CreateSql += string.Format("[{0}] text,", c.ColumnName);//在表中创建字段 } P_str_CreateSql = P_str_CreateSql + ")";//完善创建表的SQL语句 using (SqlConnection sqlcon = new SqlConnection(P_str_SqlCon))//实例化SQL数据库连接对象 { sqlcon.Open();//打开数据库连接 SqlCommand sqlcmd = sqlcon.CreateCommand();//实例化SqlCommand执行命令对象 sqlcmd.CommandText = P_str_CreateSql;//指定要执行的SQL语句 sqlcmd.ExecuteNonQuery();//执行操作 sqlcon.Close();//关闭数据连接 } using (SqlBulkCopy bcp = new SqlBulkCopy(P_str_SqlCon))//用bcp导入数据 { bcp.BatchSize = 100;//每次传输的行数 bcp.DestinationTableName = P_str_SheetName;//定义目标表 bcp.WriteToServer(myds.Tables[0]);//将数据写入Sql Server数据表 } } catch { MessageBox.Show("Sql Server数据库中已经存在" + P_str_SheetName + "表!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
yzf86211861 2014-08-26
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/370145527
threenewbee 2014-08-25
  • 打赏
  • 举报
回复
http://stackoverflow.com/questions/1356051/read-password-protected-excel-file-using-oledb-in-c-sharp

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧