I have developed a web application, which runs good when executed from Visual studio.
The theme of the application is to get the content in the excel file and display it.
We had a file upload control in our application, to get the excel file.
It works like charm when executed from the VS and i could see the desired result, but it is giving error when browsed through IIS.
Code is developed in Framework 4.0
The excel file is not uploading, and throws an error that error occured while reading the file.
Here is my code ..
Can you detect what goes wrong when browsed through IIS ?
DataSet dsRates = new Schemas.Rates();
DataTable dtExcel = new DataTable();
dtDBTable = dsRates.Tables[0];
DataTable dtColumnNameNotFound = new DataTable();
FileStream stream;
string changed = string.Empty;
string FilePath = string.Empty;
try
{
if (ValidateUserInputs())
{
DataSet dsExcel = new DataSet();
OleDbConnection con = new OleDbConnection();
try
{
if (fupExtract.HasFile == true)
{
FilePath = Server.MapPath("~/Temp/" + fupExtract.PostedFile.FileName);
fupExtract.SaveAs(FilePath);
}
else
{
fupExtract = ((FileUpload)Session["FileUploadCtrl"]);
FilePath = Server.MapPath("~/Temp/" + fupExtract.PostedFile.FileName);
fupExtract.SaveAs(FilePath);
}
//Read the Excel Data in to Datatable
string _ConnectionString = string.Empty;
string _Extension = Path.GetExtension(FilePath);
if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
{
_ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + FilePath + ";Extended Properties=" + (char)34 + "Excel 8.0;HDR=NO;IMEX=1;" + (char)34;
}
//Use ACE OleDb
else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
_ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=" + (char)34 + "Excel 8.0;HDR=NO;IMEX=1;" + (char)34;
}
else
{
lblMessage.Text = fupExtract.FileName + "is not a supported format, only '.xls|.xlsx' files are supported";
return;
}
int i = 0;
con = new OleDbConnection(_ConnectionString);
con.ResetState();
con.Open();
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow Sheet in dt.Rows)
{
OleDbDataAdapter daExcel = new OleDbDataAdapter("SELECT * FROM [" + Sheet["TABLE_NAME"].ToString().Trim() + "]", con);
DataTable dtData = new DataTable();
daExcel.Fill(dtData);
var filteredRows = dtData.Rows.Cast<DataRow>().Where(row => row.ItemArray.Any(field => !(field is System.DBNull)));
if (filteredRows.Count() > 0)
{
dsExcel.Tables.Add(dtData);
}
i++;
}
con.Close();
}
catch (Exception ex)
{
lblMessage.Text = "Error occured while reading the file";
con.Close();
}
finally
{
if (File.Exists(FilePath))
{
File.Delete(FilePath);
}
}
Please help
Thanks,
Tou can use Interop in order to read your excel document
object misValue = System.Reflection.Missing.Value;
var xlApp = new Excel.ApplicationClass();
var xlWorkBook = xlApp.Workbooks.Open("yourFile.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Console.Write(xlWorkSheet.get_Range("A1","A1").Value2.ToString());
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
I've got the answer to my question.
It's that I haven't installed Office 64-bit and Microsoft.ACE.OLEDB.12.0. The one I'm using is 32-bit.
Related
when i am reading excel(2010) file, "External table is not in the expected format." error occurs. i am using Oledb connection to read excel file.please provide me best solution for this issue. thank you...
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;IMEX=1;HDR=YES'";
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
System.Data.DataTable dtExcelSchema;
dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string firstSheet = "";
int count = dtExcelSchema.Rows.Count;
conn.Close();
//Read Data from First Sheet
conn.Open();
DataTable dt = new DataTable();
var tempDataTable = (from dataRow in dtExcelSchema.AsEnumerable()
where !dataRow["TABLE_NAME"].ToString().Contains("FilterDatabase")
select dataRow).CopyToDataTable();
dt = tempDataTable;
firstSheet = dt.Rows[0]["TABLE_NAME"].ToString();
if (!firstSheet.EndsWith("$"))
{
firstSheet = dt.Rows[0]["TABLE_NAME"].ToString() + "$";
}
cmd.CommandText = "select * from [" + firstSheet + "]";
string query1 = "SELECT count(*) FROM [" + firstSheet + "]";
cmd = new OleDbCommand(query1, conn);
cmd.CommandText = query1;
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
{
string sheetName = firstSheet.Replace(" ", "").Replace("'", "");
string query = "SELECT * FROM [" + sheetName + "]";
dtnew.TableName = firstSheet;
OleDbDataAdapter oda = new OleDbDataAdapter(query, conn);
oda.Fill(dtnew);
}
}
catch (Exception ex)
{
}
}
Hello I'm trying to export my data from a gridview to excel the problem is I have a nvarchar column the have the following barcode: 00373228210001695726 and in excel after export it looks like this 3,73228E+17
my query looks like this:
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection("Data Source=AP;Initial Catalog=MGW;User Id=sa;Password=GaUbdFO2;App=EntityFramework;");
try
{
connection.Open();
string sqlStatement = " ";
sqlStatement += " SELECT UserName AS UserID, Site, AddressID, Parameters, Content AS Barcode , TriggerDate, ReceivedDate, Action, DeviceID, Longitude, Latitude, CASE WHEN Latitude != '' THEN 'View Map Location' Else 'No Location' END AS LatitudeMSG FROM RequestWithLocation WHERE";
sqlStatement += " Content = #Content ";
if (!string.IsNullOrWhiteSpace(TextBoxStart.Text) && !string.IsNullOrWhiteSpace(TextBoxEnd.Text))
{
sqlStatement += " AND TriggerDate >= #TriggerDateStart AND TriggerDate <= #TriggerDateEnd ";
}
sqlStatement += " ORDER BY TriggerDate DESC";
using (SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection))
{
sqlCmd.Parameters.AddWithValue("#Content", TextBoxSearch.Text);
if (!string.IsNullOrWhiteSpace(TextBoxStart.Text) && !string.IsNullOrWhiteSpace(TextBoxEnd.Text))
{
sqlCmd.Parameters.AddWithValue("#TriggerDateStart", Convert.ToDateTime(TextBoxStart.Text));
sqlCmd.Parameters.AddWithValue("#TriggerDateEnd", Convert.ToDateTime(TextBoxEnd.Text).AddDays(+1));
}
using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
{
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridDisplayData.DataSource = dt;
GridDisplayData.DataBind();
lblError.Text = "";
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('No data found');", true);
}
}
}
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Search timed-out! try again..');", true);
}
EXPORT FUNCTION
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachchment; filename=Report_EventScan.xls; IMEX=1;");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridDisplayData.RenderControl(hw);
//Panel7.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
}
Use Response.Write("'" + sw.ToString()); to force a prefixed single quotation character in front of the number. That forces Excel to adopt text formatting.
(I'm assuming that + is a string concatenation in your language; it might be &).
I solved my problem. this is what I did:
You can download the EPPlus Excel reader from here http://epplus.codeplex.com/
Then using OfficeOpenXml; on top of your project and the following code to your export button, this should work for everyone who use a DataSet which is saved in a ViewState
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
DataSet dt = new DataSet();
dt = (DataSet)ViewState["QueryTable"];
MemoryStream ms = new MemoryStream();
int i = 1;
using (ExcelPackage package = new ExcelPackage(ms))
{
foreach (DataTable table in dt.Tables)
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("My Excel Report " + i++);
worksheet.Cells["A1"].LoadFromDataTable(table, true);
}
Response.Clear();
package.SaveAs(Response.OutputStream);
Response.AddHeader("content-disposition", "attachchment; filename= test.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
Response.End();
}
}
I run this code two times from the same Excel file, and it works fine, and I'm getting the right table. But when I try to run it a third time it crashes and throws external table is not in the expected format.
Filename = filename;
if (ExcelSet == null)
{
string HDR = firstRowContainsColumnNames ? "Yes" : "No";
string strConn;
if (filename.Substring(filename.LastIndexOf('.')).ToLower() == ".xlsx")
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Filename + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=1\"";
}
else if (filename.Substring(filename.LastIndexOf('.')).ToLower() == ".xls")
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1\"";
}
else
{
throw new Exception("File is not an Excel file");
}
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow schemaRow in schemaTable.Rows)
{
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
try
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;
DataTable outputTable = new DataTable(sheet);
ds.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
outputTable.Dispose();
}
catch (Exception ex)
{
throw new Exception(ex.Message + string.Format("Sheet: {0}.File.F{1}", sheet, Filename), ex);
}
}
}
conn.Close();
conn.Dispose();
}
ExcelSet = ds;
}
I have no clue why it crahes, anyone having the same problem?
I am creating a sharepoint webpart solution where i need to upload a excel file
Here is my code:
string tempFilename = "";
SPSecurity.RunWithElevatedPrivileges(delegate
{
tempFilename = System.IO.Path.GetTempFileName();
flUpload.SaveAs(tempFilename);
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";", tempFilename);
connectionString = #"Provider=Microsoft.ACE.OLEDB.14.0;Data Source="+tempFilename+#";ExtendedProperties=""Excel 12.0;HDR=YES;""";
var adapter = new OleDbDataAdapter("SELECT * FROM [Failed Trades$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "anyNameHere");
DataTable data = ds.Tables["anyNameHere"];
The Connection String generated is:
Provider=Microsoft.ACE.OLEDB.14.0;Data Source=C:\Windows\ServiceProfiles
\NetworkService\AppData\Local\Temp\Demo1.xls;
ExtendedProperties="Excel 12.0;HDR=YES;"
I had looked hundrededs of solutions but none of them is workimg.
Here are few solutions what i tried:
Installed setups from microsoft (64 bit)
Change the application pool with 32 bit enabled but that caused my pool to stopped again and again
I checked my DSN as well
Change the version number of the OLEDB driver in your connection string from:
Microsoft.ACE.OLEDB.14.0
to
Microsoft.ACE.OLEDB.12.0
This should work presuming you've installed the Microsoft Access Database Engine 2010 Redistributable.
You can try this:
In ODBC Data source administration click in "User DNS", and click "Excel files", and "Configuration..."
In the next window: "ODBC Microsoft Excel Setup", click in "Version" and see the list of drives.
Put the correct version in your connection String. like:
Microsoft.ACE.OLEDB.12.0 or Microsoft.ACE.OLEDB.14.0
You can select all:
public string tables(string strFileName)
{
DataTable dt = null;
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.15.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception a)
{
MessageBox.Show("15 - " + a.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.14.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception b)
{
MessageBox.Show("14 - " + b.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.13.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception c)
{
MessageBox.Show("13 - " + c.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception d)
{
MessageBox.Show("12 - " + d.Message);
}
}
}
}
}
I need to fetch a sheet from excel to a datatable. I first tried with LinqToExcel library, but this fetched the large numbers from the excel sheet as exponential numbers. I'm talking about big numbers like "2352143523453452334544". Only if they are formated as text it would work ok.
After that i've tried this :
OleDbConnection con = null;
System.Data.DataTable dt = null;
System.Data.DataTable dataTable1 = new System.Data.DataTable();
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + UploadFileName + ";Extended Properties=Excel 8.0;";
string sql_xls;
con = new OleDbConnection(conStr);
con.Open();
//OracleDataAdapter oda = new OracleDataAdapter();
//OracleCommand cmd = new OracleCommand("select * from [Sheet1$]", con);
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] excelSheetNames = new string[dt.Rows.Count];
int i = 0;
foreach (System.Data.DataRow row in dt.Rows)
{
excelSheetNames[i] = row["TABLE_NAME"].ToString(); i++;
}
sql_xls = "SELECT * FROM [" + excelSheetNames[0] + "]";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql_xls, conStr);
System.Data.DataSet myDataSet = new System.Data.DataSet();
dataAdapter.Fill(myDataSet, "ExcelInfo");
dataTable1 = myDataSet.Tables["ExcelInfo"];
This one returned the same values in the same conditions as null.
Isn't there a simple way to fetch data from a excel file as it is? No conversions, no nothing. Just take it all as a string, and put it into a datatable ?
This is what i used and it worked for me:
private DataTable LoadXLS(string strFile, String sheetName)
{
DataTable dtXLS = new DataTable(sheetName);
try
{
string strConnectionString = "";
if(strFile.Trim().EndsWith(".xlsx"))
{
strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", strFile);
}
else if(strFile.Trim().EndsWith(".xls"))
{
strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", strFile);
}
OleDbConnection SQLConn = new OleDbConnection(strConnectionString);
SQLConn.Open();
OleDbDataAdapter SQLAdapter = new OleDbDataAdapter();
string sql = "SELECT * FROM [" + sheetName + "$]";
OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn);
SQLAdapter.SelectCommand = selectCMD;
SQLAdapter.Fill(dtXLS);
SQLConn.Close();
}
catch (Exception)
{
throw;
}
return dtXLS;
}
But you can try to export to CSV as well:
LinqToCSV