Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I am trying to upload excel file and adding excel content into dataset. have tried following solution but it gives me error:"External table is not in the expected format.". please help me.


C#
string Extension = "";
      string FileName = "";
      string fileExtension = "";
      string fileLocation = "";

      if (FuplAssetPath.HasFile)
      {
          FileName = FuplAssetPath.FileName;
          Directory.CreateDirectory(Server.MapPath(".") + "/UploadedCSV/");
          FuplAssetPath.PostedFile.SaveAs(Server.MapPath(".") + "/UploadedCSV/" + FileName);

          FileName = Path.GetFileName(FuplAssetPath.PostedFile.FileName);
          fileExtension = Path.GetExtension(FuplAssetPath.PostedFile.FileName);
          fileLocation = Server.MapPath("UploadedExcel/" + FileName);
          FuplAssetPath.SaveAs(fileLocation);




          DataSet ds = new DataSet();


          string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"+fileLocation+"';Extended Properties= 'Excel 12.0;HDR=Yes;IMEX=1'";

          OleDbConnection con = new OleDbConnection(SourceConstr);

          string query = "Select * from [e_1_2_3]";

          OleDbDataAdapter data = new OleDbDataAdapter(query, con);

          data.Fill(ds);
Posted
Updated 13-Aug-12 1:32am
v2
Comments
Kenneth Haugland 13-Aug-12 7:33am    
DO you have the correct excel string in your connection string?
Vaishali P. Patil 13-Aug-12 7:37am    
string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"+fileLocation+"';Extended Properties= 'Excel 12.0;HDR=Yes;IMEX=1'";

in this i am using fileLocation is for where my file is stored

 
Share this answer
 
I added exceldatareader.dll. i download it from
http://exceldatareader.codeplex.com/releases/view/31934[^]

protected void lnkDispaly_Click(object sender, EventArgs e)
{
string Extension = "";
string FileName = "";
string fileExtension = "";
string fileLocation = "";

if (FuplAssetPath.HasFile)
{
FileName = FuplAssetPath.FileName;
Directory.CreateDirectory(Server.MapPath(".") + "/UploadedExcel/");
FuplAssetPath.PostedFile.SaveAs(Server.MapPath(".") + "/UploadedExcel/" + FileName);

FileName = Path.GetFileName(FuplAssetPath.PostedFile.FileName);
fileExtension = Path.GetExtension(FuplAssetPath.PostedFile.FileName);
fileLocation = Server.MapPath("UploadedExcel/" + FileName);
FuplAssetPath.SaveAs(fileLocation);

//GetExcelSheets(fileLocation, fileExtension, "Yes");

///////////////............
FileStream oStream;

oStream = new FileStream(fileLocation, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
IExcelDataReader iExcelDataReader = null;
FileInfo fileInfo = new FileInfo(FuplAssetPath.PostedFile.FileName);
string file = fileInfo.Name;
//if (file.Split('.')[1].Equals("xls"))
//{
iExcelDataReader = ExcelReaderFactory.CreateBinaryReader(oStream);

iExcelDataReader.IsFirstRowAsColumnNames = true;
DataSet dsUnUpdated = new DataSet();
dsUnUpdated = iExcelDataReader.AsDataSet();
}
}
 
Share this answer
 
1. Build a universal ConnString method:
C#
public string GetConnectionString(string FileNamePath, bool HasHeader)
        {
            string ConnectionString = "";
            string Extension = Path.GetExtension(FileNamePath).ToLower();

            string BinaryExcelProvider = "Microsoft.Jet.OLEDB.4.0";
            string XmlExcelProvider = "Microsoft.ACE.OLEDB.12.0";
            string BinaryExcelExtProperties = "Excel 8.0";
            string XmlExcelExtProperties = "Excel 12.0";
            string XmlMacroExcelExtProperties = "EXCEL 12.0 Macro";

            string Provider = "";
            string ExtendedProperties = "";

            switch (Extension)
            {
                case ".xls":
                    Provider = BinaryExcelProvider;
                    ExtendedProperties = BinaryExcelExtProperties;
                    break;

                case ".xlsx":
                    Provider = XmlExcelProvider;
                    ExtendedProperties = XmlExcelExtProperties;
                    break;

                case ".xlsm":
                    Provider = XmlExcelProvider;
                    ExtendedProperties = XmlMacroExcelExtProperties;
                    break;
            }

            string Header = ";HDR=NO";
            if (HasHeader)
                Header = ";HDR=YES";
            string ConnectionStringFormat = "Provider={0};Data Source={1};Extended Properties=\"{2}{3}\";";

            ConnectionString = string.Format(ConnectionStringFormat, Provider, FileNamePath, ExtendedProperties, Header);
            return ConnectionString;
        }


2. Create DataSets

C#
  private DataSet GetDataSet(string excelFile, string WorkSheet, out bool Success)
        {
            Success = false;
            System.Data.DataSet excelDataSet = new DataSet();
            string connectionString = this.GetConnectionString(excelFile, true);


            using (OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(connectionString))
            {
                try
                {
                    objConn.Open();
                    OleDbDataAdapter cmd = new OleDbDataAdapter("select * from " + WorkSheet, objConn);
                    cmd.Fill(excelDataSet, WorkSheet);
                    cmd.Dispose();
                }
                catch { }
                finally
                {
                    if (objConn != null)
                    {
                        objConn.Close();
                        objConn.Dispose();
                    }

                }
            }
return excelDataSet;
}
 
Share this answer
 
v2

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