外部表格不是预期的格式

时间:2014-06-13 09:18:40

标签: c# asp.net sharepoint-2010

我必须在文档库中上传Excel表格,并且必须在gridview中显示列表的所有详细信息。但是我得到了错误:"外部表格不是预期的格式":

我的代码是:

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;
            }
        }

我在getExcelData()方法中遇到错误 cmd = new OleDbCommand(query,conn);

自上周以来我一直在这里帮助我。

0 个答案:

没有答案