您的位置:首页 > 编程语言 > C#

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

2007-09-14 09:14 1011 查看
用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.调试通过。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: