DataSet 操作Excel导入sql
2012-03-20 23:07
183 查看
用dataset读取Excel数据并导入到sql,在已知列数前提下,去掉空行插入只插入数据行
以下 是按钮事件下的方法
private void button2_Click(object sender, EventArgs e) { try { #region DataSet读取Excel string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\121.xlsx; Extended Properties='Excel 8.0'"; string strExcel = string.Format("select * from [第一个工作表$]"); OleDbConnection connExcel = new OleDbConnection(strConn); OleDbCommand comm = new OleDbCommand(strExcel, connExcel); OleDbDataAdapter oledbDA = new OleDbDataAdapter(comm); DataSet ds = new DataSet(); connExcel.Open(); oledbDA.Fill(ds, "shee1"); connExcel.Close(); #endregion int rowCount = ds.Tables[0].Rows.Count; string sqlCom = "insert into AE_Workflow_Exam values("; string sqlCom1 = sqlCom; #region 在已知列数情况下判断空行, for (int rows = 0; rows < rowCount; rows++) { int number = 0; for (int col = 0; col < 11; col++) { //判断某行 的单元格内容是否为空,为空就累加 if (ds.Tables[0].Rows[rows][col].ToString().Trim() == "") { number++; } //当空行的个数等于 列数 则获得当前行索引, if (number == 11) { rowCount = rows; break; } } } #endregion for (int i = 0; i < rowCount; i++) { for (int j = 0; j < 11; j++) { sqlCom = sqlCom + "'" + ds.Tables[0].Rows[i][j].ToString().Replace("'", "'") + "',"; } sqlCom = sqlCom.Substring(0, sqlCom.Length - 1) + ");"; SqlCommand commAdd = new SqlCommand(sqlCom, conn); commAdd.ExecuteNonQuery(); sqlCom = sqlCom1; } } catch (Exception) { throw; } }
相关文章推荐
- .NET + Excel + SQL 的导入导出相关操作
- SQL 数据的导入导出,对远程(MSsql,OracleAccess,)数据库的操作以及读取Excel,txt文件中的数据
- mysql命令行的导入导出sql,txt,excel(都在linux或windows命令行操作)
- sql 导入导出操作Excel
- 将EXCEL导入到SQL,自动添加表,只可以导入03的,07的不可以
- aspx excel导入到SQL
- 有什么常用的JAVA API可以操作excel的导入导出(读取和写入)功能?
- VB中Excel 2010的导入导出操作
- sql 将excel文本导入时出错
- C#net快速导入excel sql oledb 第一行 orcale比对
- 用SQL查询分析器操纵Excel及导入导出数据
- SQL本地分布式操作远程数据库,可用于实现导入,导出等数据操作功能
- PL/SQL DEVELOPER怎样导入excel数据
- 将数据从excel导入到sql时出现空值.
- 使用sql语句导入/导出 数据为Excel
- SQL导入导出到Excel的语句
- 通过excel生成SQL语句完成数据库操作
- 将Excel中所有Sheet的数据导入一个DataSet
- java导入导出excel操作(jxl)
- Java导入excel的操作