Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
string path = Server.MapPath(fileuploadExcel.PostedFile.FileName);
Posted
Comments
Dave Kreskowiak 19-Dec-15 12:35pm    
Insufficient information. There isn't enough here to determine why the Excel file isn't opening.

You don't specify an error messages. You don't show how you're "saving" the Excel file on the server nor do you show how your sending the file back to the client for it to open.
Member 11118232 19-Dec-15 12:50pm    
i dont get any error message when i open uploaded file it says excel cannot open "file name" because the file format or file extension is not valid
Abhishek Sur 19-Dec-15 12:55pm    
But the code you written is just the Server.MapPath. Can you post the code which reads the file in Excel. Can you check File.Exists on the server path ?

Also Server.MapPath will only get you a disk path if the file is uploaded in server and written to the disk. Have you already written the content of File to the disk ?
Member 11118232 19-Dec-15 13:05pm    
string connString = "";
string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
string path = Server.MapPath(fileuploadExcel.PostedFile.FileName);
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{

connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT [UserName],[Education],[Location] FROM [sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
string a=""+conn.GetSchema();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
DataTable activityDataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);



da.Fill(ds);

grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
da.Dispose();
conn.Close();
conn.Dispose();
Jochen Arndt 20-Dec-15 5:58am    
First ensure that the file is a valid Excel file (e.g. by openening with Excel).

Then check if the uploaded file is binary identical with the source file (e.g. by copying it manually back and performing a file compare or by opening it with Excel on the server). If not, the problem is related to the upload process.

If the above checks succeeded, the problem may be related to your read code and you should edit your question to post the code there (use the green 'Inmprove question') link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900