关闭

用C#.net编写导入导出EXCEL文件的代码。编绎运行后,错误提示: 找不到可安装的ISAM

4617人阅读 评论(4) 收藏 举报

   用C#.net编写导入导出EXCEL文件的代码。编绎运行后,错误提示: 找不到可安装的ISAM
   解决方案:
   1。修复一下你的OFFICE,确定OFFICE没有问题。
   2。判断你的连接字符串是否有问题。
   3。我自己调试通过的代码:
///导入:
   string MyFileName = @"C://abc.XLS";
            string MyTableName = "Sheet1";
        
            try
            {
                string MyConnectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://abc.XLS;Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
                string MySQL = "SELECT * FROM [" + MyTableName + "$]";
               
          
                OleDbConnection myconn = new OleDbConnection(MyConnectionstring);
                myconn.Open();
                OleDbDataAdapter adp = new OleDbDataAdapter(MySQL, myconn);
                adp.Fill(ds, "res");
                myconn.Close();
                MessageBox.Show("Excel文件:" + MyFileName + "创建成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dataGridView1.DataSource = ds.Tables["res"];
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message , "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


///导出:

  DateTime dt=DateTime.Now ;
            string myname ="C://d1d.xls";
            string mytablename = "Sheet1";
            try
            {
                string sql = "编号 char(255),姓名 char(255),种类 char(255),组织 char(255),班组 char(255),职务 char(255),日期 char(255)";
                 sql = " Create Table " + mytablename + "(" + sql + ")";
                string MyConnectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + myname +"';Excel 8.0;HDR=YES";
                OleDbConnection olconn = new OleDbConnection(MyConnectionstring);
                 olconn.Open();
                 OleDbCommand cmd = new OleDbCommand(sql, olconn);
                 cmd.ExecuteNonQuery();
                 for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                 {
                     string a = ds.Tables[0].Rows[i][0].ToString();
                     string b = ds.Tables[0].Rows[i][1].ToString();
                     string c = ds.Tables[0].Rows[i][2].ToString();
                     string d = ds.Tables[0].Rows[i][3].ToString();
                     string f = ds.Tables[0].Rows[i][4].ToString();
                     string g = ds.Tables[0].Rows[i][5].ToString();
                     sql = "INSERT INTO [" + mytablename + "$]([编号],[姓名],[种类],[组织],[班组],[职务],[日期])VALUES('" + a + "','" + b + "','" + c + "','" + d + "','" + f + "','" + g+ "','" + dt.ToShortDateString() + "')";
                     OleDbCommand cmd1 = new OleDbCommand(sql, olconn);
                     cmd1.ExecuteNonQuery();
              
                 }
                   olconn.Close();
                 MessageBox.Show("Excel文件:" + mytablename + "创建成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

4.调试通过。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13939次
    • 积分:177
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:5篇
    • 译文:0篇
    • 评论:4条
    文章存档