I need to read Excel Data in C# DataTable in my Visual web Part.
for this purpose I've uploaded the excel file in a temporary location in a document library, the file upload is successful, now I want to read the contents of Excel file in C# DataTable
I've tried the same using OLEDB but it's throwing 'Unspecified Exception'
Here is my code:
protected void btn_UploadItems_Click(object sender, EventArgs e)
{
Stream fStream = flu_items.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
SPSecurity.CatchAccessDeniedException = false;
try
{
string extension = Path.GetExtension(flu_items.PostedFile.FileName);
string filename = ViewState["UserID"].ToString().Replace(".","_") + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + extension;
obj_tempdoc.UploadFile(filename, fStream);
}
catch (UnauthorizedAccessException)
{
// Controls.Add(new LiteralControl("You are not authorized to upload files to the document library"));
}
fStream.Close();
}
public bool UploadFile(string filename, Stream fStream)
{
try
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder myLibrary = web.Folders["GatePassTemporaryDocuments"];
// Prepare to upload
Boolean replaceExistingFiles = true;
// Upload document
SPFile spfile = myLibrary.Files.Add(filename, fStream, replaceExistingFiles);
// Commit
myLibrary.Update();
//
ReadExcelFile(spfile.ServerRelativeUrl);
//
return true;
}
}
}
catch (Exception ex)
{
return false;
}
}
public DataTable ReadExcelFile(string fileName)
{
try
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder myLibrary = web.Folders["GatePassTemporaryDocuments"];
string fileExtension = Path.GetExtension(fileName).ToUpper();
string connectionString = "";
if (fileExtension == ".XLS")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + fileName + "'; Extended Properties='Excel 8.0;HDR=YES;'";
}
else if (fileExtension == ".XLSX")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties='Excel 12.0 Xml;HDR=YES;'";
}
if (!(string.IsNullOrEmpty(connectionString)))
{
string[] sheetNames = GetExcelSheetNames(connectionString);
if ((sheetNames != null) && (sheetNames.Length > 0))
{
DataTable dt = null;
OleDbConnection con = new OleDbConnection(connectionString);
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + sheetNames[0] + "]", con);
dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
}
}
catch (Exception ex)
{
}
return null;
}
private string[] GetExcelSheetNames(string strConnection)
{
var connectionString = strConnection;
String[] excelSheets;
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
var dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
}
return excelSheets;
}
The Exception is thrown at the time of Opening Connection
Please Help
I've also tried the following but still the Exception is same: