0

**我有 Excel 工作表名称 xyz.xls 我能够验证 FileUploaded 只能是 .xls 扩展名。现在,我想检查 Xls 的 ColumnsName 是否必须与数据库列匹配...OtherWise Display error.. Excel 工作表列必须是.. [st_id],[st_rollno],[st_name],[branch_name],[sem_no ],[batch_year] * *

 protected void ShowGridView()
    {

        if ((txtFilePath.HasFile))
        {

            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            OleDbDataAdapter da = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            string query = null;
            string connString = "";
            string strFileName = txtFilePath.FileName;
            //string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
            string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();

            //Check file type
            if (strFileType == ".xls" || strFileType == ".xlsx")
            {
                //print progressbar
            txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));



            }
            else
            {
                lblMessage.Text = "Only Excel files(.xls or .xlsx) allowed";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Visible = true;
                return;
            }

            string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);

            //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 [st_id],[st_rollno],[st_name],[branch_name],[sem_no],[batch_year] FROM [student$]";
            //query = "SELECT [Country],[Capital] FROM [Sheet1$] WHERE [Currency]='Rupee'"
            //query = "SELECT [Country],[Capital] FROM [Sheet1$]"

            //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);

            grvExcelData.DataSource = ds.Tables[0];
            grvExcelData.DataBind();

            lblMessage.Text = "Data retrieved successfully! Total Recodes:" + ds.Tables[0].Rows.Count;
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Visible = true;

            da.Dispose();
            conn.Close();
            conn.Dispose();
        }
        else
        {
            lblMessage.Text = "Please select an excel file first";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }

    }
4

0 回答 0