Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to upload a excel sheet in document library and have to show all the details of list in grid view. But I am getting error "external table is not in expected format:.

My code is

C#
void uploadDocument()
{

    try
    {
        string publicFSdocLibrary = "PublicFSdoc";

        if (uploadDoc.HasFile)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite oSite = new SPSite("http://chdsez301298d:1000/sites/Test/"))
                //using (SPSite oSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb myWeb = oSite.OpenWeb())
                    {
                        SPListItem lstItem = SPContext.Current.ListItem;
                        myWeb.AllowUnsafeUpdates = true;

                        //SPList docList = myWeb.Lists[docLibrary];
                        SPFolder finalDocument = myWeb.Folders[publicFSdocLibrary];
                        // Prepare to upload
                        Boolean replaceExistingFiles = true;
                        string fileName = Path.GetFileName(uploadDoc.FileName);
                        filePath = Path.GetFullPath(uploadDoc.PostedFile.FileName);
                        // string newFilePath = getFilePath(filePath);
                        FileStream fileStream = System.IO.File.OpenRead(filePath);
                        // Upload document
                        SPFile spfile = finalDocument.Files.Add(getFileName(), fileStream, replaceExistingFiles);
                        SPListItem item = spfile.Item;
                        item["RequestNo"] = generateRequestNo();
                        item["AssessmentType"] = "Financial Solvency";
                        // Commit all changes
                        item.Update();
                        //Update document url to Assessment request list                               
                        myWeb.AllowUnsafeUpdates = false;
                    }
                }
            });
        }

    }
    catch (Exception ex)
    {
        ex.Message.ToString();

    }

}

string getFileName()
{
    string newFileName = generateRequestNo() + ".xlsx";
    return newFileName;
}


void btnUpload_Click(object sender, EventArgs e)
{
    uploadDocument();
    getExcelData();
    //savePublicFS();
    //addAssesmentWfRequest();
    //CloseForm();
    //Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.close();", true);
    
}

void getExcelData()
{
    try
    {
        if (uploadDoc.HasFile)
        {

            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            OleDbDataAdapter da = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            //string sheetName = "Sourcing Questionnaire";
            string query = null;
            string connString = "";
            string fileName = Path.GetFileName(uploadDoc.FileName);
            string strFileType = System.IO.Path.GetExtension(uploadDoc.FileName).ToString().ToLower();
            string strNewPath = filePath;

            //Connection String to Excel Workbook
            if (strFileType.Trim() == ".xls")
            {
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (strFileType.Trim() == ".xlsx")
            {
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
           // query = "SELECT * FROM [" + sheetName + "$]";
            query = "SELECT * FROM [abc$]";
            //Create the connection object
            conn = new OleDbConnection(connString);
            //Open connection
            if (conn.State == ConnectionState.Closed) conn.Open();
            //Create the command object
            cmd = new OleDbCommand(query, conn);
            da = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);
            gvdetails.DataSource = ds.Tables[0];
            gvdetails.DataBind();
            da.Dispose();
            conn.Close();
            conn.Dispose();
            //return ds;

        }            

    }
    catch (Exception)
    {

        throw;
    }
}

private String generateRequestNo()
{
    try
    {
        String requestNo = String.Empty;
        if (txtSupplierName.Text.Length <= 4)
        {
            requestNo = txtSupplierName.Text
                          + "-" + requestNumber()
                          + DateTime.Today.ToString("ddMMyyyy");
        }
        else
        {
            requestNo = txtSupplierName.Text.Substring(0, 4)
                              + "-" + requestNumber()
                              + DateTime.Today.ToString("ddMMyyyy");
        }
        return requestNo;
    }
    catch (Exception)
    {
        throw;
    }
}
private String requestNumber()
{
    try
    {
        String number = String.Empty;
        number = "FS-";
        return number;
    }
    catch (Exception)
    {
        throw;
    }
}


and I am getting error in getExcelData() method at line cmd = new OleDbCommand(query, conn);

Please help me as I struck here since last week.
Posted
Updated 12-Jun-14 22:47pm
v2

1 solution

Remove HDR=Yes;IMEX=2\ and Update Part of code like:

HTML
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;"";
}


Hope this solve your problem

and mark itas solved if your problem is solved
 
Share this answer
 
Comments
Member 10882145 13-Jun-14 5:03am    
Still getting the same error .I have changed the connection string to
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;\"";
}
Kriti Sharma 10400841 13-Jun-14 5:04am    
remove HDR=Yes;IMEX=2\ from first string also and check if problem is solved or not.
if it remains check your db column and excel file columns these must be same
Member 10882145 13-Jun-14 5:07am    
I don't have database. I just want to display all the details of uploaded excel file in gridview so i can update my sharepoint list item as per the position of item in gridview.
Kriti Sharma 10400841 13-Jun-14 5:11am    
have you removed HDR=Yes;IMEX=2\ form first string and try???
Kriti Sharma 10400841 13-Jun-14 5:12am    
also Cross check your gridview column sequence.

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