Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Friends,
I am exporting data into Excel (2003 format).
But when I try to read the same Excel, I get this error
"External table is not in the expected format"

I have MS Office 2007 installed on my PC.

When I open that Excel and then try to read it, it works fine, whereas it is expected that it should give me error msg that the File is already being used.
(I think so. Please clear me if I am wrong)

Here is what my code says:
C#
public enum ExcelVersion
{
            Excel,
            Excel2007
};
private string ProviderExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
private string ExtendedPropertiesExcel = "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
private string ProviderExcel2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
private string ExtendedPropertiesExcel2007 = "Extended Properties=\"Excel 12.0;HDR=YES\"";
private string mConnString = "";



On Button_Click Even, I have..

C#
foreach (UploadedFile file in rdpUpload.UploadedFiles)
{
   FileName = file.GetName();
   sExtension = file.GetName();
   sExtension = sExtension.Substring(sExtension.LastIndexOf(".") + 1);
   if (sExtension != "xls")
     throw new System.Exception("Select .xls/.xlsx file ");
                    ExcelImport(ExcelVersion.Excel, FilePath + "//" + FileName);
                    GetPaymentVouchers();
}


And these are my methods
C#
public void ExcelImport(ExcelVersion ver, string FilePath)
{
	switch (ver)
        {
                case ExcelVersion.Excel:
                    mConnString = ProviderExcel + FilePath + ";" + ExtendedPropertiesExcel;
                    break;
                case ExcelVersion.Excel2007:
                    mConnString = ProviderExcel2007 + FilePath + ";" + ExtendedPropertiesExcel2007;
                    break;
         }
}
public void GetPaymentVouchers()
{
	OleDbConnection cn = new OleDbConnection(mConnString);
        try
        {
                cn.Open();
                DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                string SheetName = schemaTable.Rows[0][2].ToString();
                StringBuilder SelectStatement = new StringBuilder();
                SelectStatement.Append("SELECT * FROM [").Append(SheetName).Append("]");
                OleDbDataAdapter da = new OleDbDataAdapter(SelectStatement.ToString(), cn);
                DataSet ds = new DataSet();
                da.Fill(ds);
         }
         catch (Exception ex)
         {
                XITingExceptionProcessor.ProcessException(this, ex);
         }
         finally
         {
                cn.Close();
         }
}


Please help me out friends.
Thanks,
Lok..
Posted

 
Share this answer
 
Comments
Lokesh Zende 16-Nov-11 6:07am    
Thanks for your reply Deepak..

But while googling, I had already tried your solution..It doesn't work..:(
Hi,

I am facing same type of problem solution.

Just go to Open Excel file

1) Click on File Menu.

2) Click on Save As .

3) Show a Box.

4) Change File Name & Save As Type to Excel 97-2003 Workbook (*.xls) & click save then it work.

Also this type of problem occurs in .csv apply same procedure & solve it.
 
Share this answer
 
v2
Comments
Lokesh Zende 29-Nov-12 6:07am    
Hello Sanjay,
Yes, it definitely works. But I dont want to save the excel in other formats. Rather, how can I ask the user to do such things. hahaha..Hope u can understand.
Thanks for reply.
basunld 4-May-13 6:45am    
Nice its worked!...Thanks

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