使用oledb写入excel文件时,Sheet1 $不存在错误 [英] Sheet1$ does not exist error coming when writing to excel file using oledb

查看:95
本文介绍了使用oledb写入excel文件时,Sheet1 $不存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btn_submit_Click(object sender, EventArgs e)
       {
           try
           {

               string name = textBox1.Text;
               string filepath="D://"+name;

               System.Data.OleDb.OleDbConnection MyConnection;
               System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
               string sql = null;
               MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filepath+".xls;Extended Properties=Excel 8.0;");
               MyConnection.Open();
               myCommand.Connection = MyConnection;
               sql = "Insert into [Sheet1$] (name,email) values('"+textBox1.Text+"','"+textBox2.Text+"')";
               myCommand.CommandText = sql;
               myCommand.ExecuteNonQuery();
               MyConnection.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }





我的尝试:



我正在尝试将数据写入excel文件,但它给我一个错误,因为sheet1 $不存在。我不知道该怎么办。请帮助



What I have tried:

i'm trying to write data to excel file but its giving me an error as sheet1$ does not exists.I don't know what to do.please help

推荐答案

(名称,电子邮件)值(' + textBox1.Text + ',' + textBox2.Text + ');
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
(name,email) values('"+textBox1.Text+"','"+textBox2.Text+"')"; myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }





我尝试过:



我正在尝试将数据写入excel文件,但是给我一个错误,如sheet1



What I have tried:

i'm trying to write data to excel file but its giving me an error as sheet1


不存在。我不知道该怎么做。请帮助
does not exists.I don't know what to do.please help


检查您的Excel文件:确保您没有重命名该表。

并且不要这样做:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。使用参数化查询 - SQL注入不仅限于数据库!
Check your excel file: make sure that you haven't renamed the sheet.
And don't do it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead - SQL Injection isn't just confined to databases!


这篇关于使用oledb写入excel文件时,Sheet1 $不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆