예제 #1
0
    public Boolean ShowExcelToDataGrid()
    {
        bool IsSuccess = true;

        if (FileLead.HasFile)
        {
            //try
            //{

            string strError = "";
            objCommon   = new clsCommon();
            strFileName = Session[appFunctions.Session.UserID.ToString()].ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
            objCommon.FileUpload_Commomn(FileLead, strFileName, "Uploads/Import/", ".xls,.xlsx", ref strError, 0);
            strFileName = "Uploads/Import/" + strFileName + Path.GetExtension(FileLead.FileName);
            objCommon   = null;
            if (strError != "")
            {
                DInfo.ShowMessage(strError, Enums.MessageType.Error);
                return(false);
            }

            string strFilePath = System.IO.Path.GetFullPath(Server.MapPath("~/admin/" + strFileName));
            if (strFilePath != "")
            {
                if (Path.GetExtension(strFilePath) == ".xls")
                {
                    objOleDbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
                }
                else if (Path.GetExtension(strFilePath) == ".xlsx")
                {
                    objOleDbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFilePath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
                }
                else
                {
                    DInfo.ShowMessage("File Formate not Valid.", Enums.MessageType.Error);
                    IsSuccess = false;
                }

                objOleDbConn.Open();

                OleDbCommand     objcmd         = new OleDbCommand();
                OleDbDataAdapter objDataAdapter = new OleDbDataAdapter();
                DataSet          objDataset     = new DataSet();

                dgvGridData.DataSource = null;
                dgvGridData.DataBind();


                objcmd.Connection  = objOleDbConn;
                objcmd.CommandType = CommandType.Text;
                objcmd.CommandText = "SELECT * FROM [Sheet1$]";
                objDataAdapter     = new OleDbDataAdapter(objcmd);
                objDataAdapter.Fill(objDataset);

                if (objDataset.Tables[0].DefaultView.Table.Rows.Count > 0)
                {
                    int intColumn = objDataset.Tables[0].DefaultView.Table.Columns.Count;
                    if (intColumn > 0)
                    {
                        dgvGridData.DataSource = objDataset.Tables[0];
                        dgvGridData.DataBind();
                        for (int i = 0; i < intColumn; i++)
                        {
                            objDataset.Tables[0].DefaultView.Table.Columns[i].ColumnName = objDataset.Tables[0].DefaultView.Table.Columns[i].ColumnName.ToString().ToLower();
                        }
                    }
                    else
                    {
                        DInfo.ShowMessage("Defind Column name", Enums.MessageType.Error);
                        IsSuccess = false;
                    }
                    Session[appFunctions.Session.LeadDataTable.ToString()] = objDataset.Tables[0].DefaultView.Table.Copy();
                }
                else
                {
                    IsSuccess = false;
                }
                objDataset.Clear();
                objcmd.Dispose();
                objOleDbConn.Close();
            }
            else
            {
                DInfo.ShowMessage("File Not Exist.", Enums.MessageType.Error);
                IsSuccess = false;
            }

            if (strFileName != "")
            {
                if (System.IO.File.Exists(Server.MapPath(strFileName)))
                {
                    System.IO.File.Delete(Server.MapPath(strFileName));
                }
                strFileName = "";
            }
        }
        else
        {
            DInfo.ShowMessage("Choose Product Data File.", Enums.MessageType.Error);
            IsSuccess = false;
        }
        return(IsSuccess);
    }