Reputation: 1926
I am trying to read an Excel sheet (.csv) by opening a dialogue box from SAP business one. I have not tried this before and am receiving the following error when trying to read the excel sheet:
private void GetFile()
{
using (GetFileNameClass oGetFileName = new GetFileNameClass())
{
oGetFileName.Filter = "Excel files (*.csv)|*.csv";
oGetFileName.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
Thread threadGetExcelFile = new Thread(new ThreadStart(oGetFileName.GetFileName));
threadGetExcelFile.SetApartmentState(ApartmentState.STA);
try
{
threadGetExcelFile.Start();
while (!threadGetExcelFile.IsAlive) ; // Wait for thread to get started
Thread.Sleep(1); // Wait a sec more
threadGetExcelFile.Join(); // Wait for thread to end
var fileName = string.Empty;
fileName = oGetFileName.FileName;
if (fileName != string.Empty)
{
string connString = "";
System.Data.DataTable dt = new System.Data.DataTable();
connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
connString += fileName;
connString += ";Extended Properties=Excel 12.0 Xml;HDR=YES;IMEX=1;";
OleDbConnection myConnection = new OleDbConnection(connString);
if (myConnection.State != ConnectionState.Open)
myConnection.Open();
string sql = "SELECT * From [Sheet1$]"; <-------Error thrown here
using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, myConnection))
{
adapter.Fill(dt);
}
}
}
catch (RulesException ex)
{
SboConnection.SboApplication.SetStatusBarMessage(ex.GetErrorMessages(), SAPbouiCOM.BoMessageTime.bmt_Medium, true);
}
}
}
The error "Could not find installable ISAM" occurs at the sql statement. How do I solve this and get to read the excel sheet? Any help appreciated.
Upvotes: 0
Views: 356
Reputation: 7628
Actuality in connection string, it needs two ; (semicolons) at the end. So *connString * must be like follow:
var connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;';", saved_FileName);
It worked for me.
Upvotes: 1