private DataSet importExcelToDataSet(string FilePath)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
try
{
myCommand.Fill(myDataSet);
}
catch (Exception ex)
{
//throw new InvalidFormatException("该Excel文件的工作表的名字不正确," + ex.Message);
MessageBox.Show(ex.Message,"Error");
}
return myDataSet;
}
这是2003的,读excel2010时的时候显示"外部表不是预期的格式"
怎么改成2010的啊???
还有InvalidFormatException 要用哪个using啊,没有提示...
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
try
{
myCommand.Fill(myDataSet);
}
catch (Exception ex)
{
//throw new InvalidFormatException("该Excel文件的工作表的名字不正确," + ex.Message);
MessageBox.Show(ex.Message,"Error");
}
return myDataSet;
}
这是2003的,读excel2010时的时候显示"外部表不是预期的格式"
怎么改成2010的啊???
还有InvalidFormatException 要用哪个using啊,没有提示...
{
try
{
if (!File.Exists(txtSourceFile.Text))
return string.Empty; string strConn;
string excelFilename = txtSourceFile.Text;
if (excelFilename.EndsWith("xls", StringComparison.InvariantCultureIgnoreCase))
{
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=" + excelFilename + ";" + "Extended Properties=\"Excel 8.0;HDR=No\"";
}
else if (excelFilename.EndsWith("xlsx", StringComparison.InvariantCultureIgnoreCase))
{
//strConn = @"Data Source=" + _strExcelFilename +
// ";Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";
strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=" + excelFilename + ";" +
"Extended Properties=\"Excel 12.0 Xml;HDR=No\"";
}
else
return string.Empty;
return strConn;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
return string.Empty;
}
}
所有连接字符串里面有12.0
2010安装完以后目录里面是多少? 应该可以替换吧
但是我不知道你想说的是向下兼容还是向上兼容……
@"Data Source=" + excelFilename + ";" +
"Extended Properties=\"Excel 12.0 Xml;HDR=No\"";
我把这个改成14.0,下载了AccessDatabaseEngine.exe,安装了,但为什么还提示,未找到Microsoft.ACE.oleDB.14.0提供程序啊...
楼主 用12就行了吗?需要添加哪些dll