用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.调试通过。
解决方案:
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.调试通过。
相关文章推荐
- C#程序读取Excel文件后读入DataSet,提示错误:找不到可安装的ISAM
- 导入导出Excel数据错误,找不到可安装的 ISAM错误!
- C#导入导出EXCEL文件的代码实例
- 导入Excel数值读不到,找不到可安装的 ISAM错误
- 导入Excel数值读不到,找不到可安装的 ISAM错误
- 导入Excel数值读不到,找不到可安装的 ISAM错误!
- plsql 中使用odbc导入器导入excel 时提示“找不到可安装的ISAM ”
- 导入Excel数值读不到,找不到可安装的 ISAM错误
- 导入Excel数值读不到,找不到可安装的 ISAM错误!(导入Excel常见错误)
- ASP.NET导出excel文件提示“未指定的错误”!
- c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
- Oledb导入Excel 找不到可安装的isam错误
- C# .Net :Excel NPOI导入导出操作教程之将Excel文件读取并写到数据库表,示例分享
- 导入Excel数值读不到,找不到可安装的 ISAM错误!
- [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- c# .Net :Excel NPOI导入导出操作教程之数据库表信息数据导出到一个Excel文件并写到磁盘示例分享
- asp.net c# repeater或gridview导出EXCEL的详细代码。
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- asp.net/c#代码实现excel to mssql数据导入