private void initialize(string path, bool hasHeaders, bool hasMixedData)
{
string fullPath = Path.GetFullPath(path);
string extension = Path.GetExtension(path);
if (!File.Exists(fullPath))
throw new FileNotFoundException("File could not be found.", fullPath);
string tempFileName = Path.GetTempFileName();
File.Copy(fullPath, tempFileName, true);
// Reader Settings
HasHeaders = hasHeaders;
HasMixedData = hasMixedData;
switch (extension)
{
case ".xls": Version = "Excel 8.0"; break; // Excel 95-2003
case ".xlsx": Version = "Excel 12.0"; break; // Excel 2007+
default: throw new ArgumentException("File type could not be determined by file extension.", "path");
}
if (IntPtr.Size == 4 && extension == ".xls")
Provider = "Microsoft.Jet.OLEDB.4.0"; // for x86/95-2003
else Provider = "Microsoft.ACE.OLEDB.12.0"; // for x64/95-2007+
var strBuilder = new DbConnectionStringBuilder();
strBuilder.Add("Provider", Provider);
strBuilder.Add("Data Source", tempFileName);
strBuilder.Add("Extended Properties", Version + ";" +
"HDR=" + (HasHeaders ? "Yes" : "No") + ';' +
"Imex=" + (HasMixedData ? "2" : "0") + ';');
strConnection = strBuilder.ToString();
}