/// <summary>
/// 读取Excel内容到数据集里面
/// </summary>
/// <param name="filepath">Excel文件路径</param>
/// <returns>Excel内容</returns>
public static DataSet ReadExcel(string filepath)
{
string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
if (filepath.Contains(".xlsx"))
{
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
}
string strMASTCon = "SELECT * FROM [Sheet1$]";
DataSet dsSrc;
OleDbConnection myConn = new OleDbConnection(conn);
try
{
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strMASTCon, myConn);
dsSrc = new DataSet();
myCommand.Fill(dsSrc, "[Sheet1$]");
myConn.Close();
return dsSrc;
}
catch
{
return null;
}
}
//=================================================================
解决 Application is busy (RPC_E_CALL_REJECTED 0x80010001) Call was rejected by callee (RPC_E_SERVERCALL_RETRYLATER 0x8001010A)
/* Fixing 'Application is Busy' and 'Call was Rejected By Callee' Errors
* If you programatically call into Visual Studio automation from an external (out-of-proc), multi-threaded application, you can occasionally get the errors "Application is busy" or "Callee was rejected by caller." These errors occur due to threading contention issues between external multi-threaded applications and Visual Studio. They can be eliminated by implementing IOleMessageFilter error handlers in your Visual Studio automation application. (Not to be confused with System.Windows.Forms.IMessageFilter.)
*
* To fix errors
*
* 1. Add the following class to your application.
* 2. Add a COM reference to "Microsoft Development Environment 8.0." This adds references to EnvDTE and EnvDTE80 to your solution.
* 3. In the code, create an instance of EnvDTE80, as outlined in the following example.
* 4. Call Message.Register to handle thread errors.
* 5. Call your automation code as usual.
* 6. When your automation code is finished, call Message.Revoke to remove the thread error handlers.
*/