如何在导入时在excel文件中获得正确的字符?

时间:2015-08-26 11:16:25

标签: c# asp.net excel datetime

我有一个文件excel,我上传文件,阅读它并在gridview中显示,没关系,但格式错误(数据性别和数据生日不一样),这是excel数据

enter image description here

这是我的代码



   public void BindGridView()
        {
            string FilePath = ResolveUrl("~/Uploads/"); // Give Upload File Path
            string filename = string.Empty;
            if (FileUploadToServer.HasFile) // Check FileControl has any file or not
            {
                try
                {
                    string[] allowdFile = { ".xls", ".xlsx" };
                    string FileExt = System.IO.Path.GetExtension(FileUploadToServer.PostedFile.FileName).ToLower();// get extensions
                    bool isValidFile = allowdFile.Contains(FileExt);

                    // check if file is valid or not
                    if (!isValidFile)
                    {
                        lblMsg.Visible = true;
                        lblMsg.Style.Add("color", "red");
                        lblMsg.Text = "Please upload only Excel";
                    }
                    else
                    {
                        int FileSize = FileUploadToServer.PostedFile.ContentLength; // get filesize
                        if (FileSize <= 1048576) //1048576 byte = 1MB
                        {
                            filename = Path.GetFileName(Server.MapPath(FileUploadToServer.FileName));// get file name
                            FileUploadToServer.SaveAs(Server.MapPath(FilePath) + filename); // save file to uploads folder
                            string filePath = Server.MapPath(FilePath) + filename;
                            string conStr = "";
                            if (FileExt == ".xls") // check for excel file type
                            {
                                conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                            }
                            else if (FileExt == ".xlsx")
                            {
                                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                            }

                            conStr = String.Format(conStr, filePath, "Yes");
                            OleDbConnection con = new OleDbConnection(conStr);
                            OleDbCommand ExcelCommand = new OleDbCommand();
                            ExcelCommand.Connection = con;
                            con.Open();
                            DataTable ExcelDataSet = new DataTable();
                            ExcelDataSet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                            DataTable dt = new DataTable();
                            if (ExcelDataSet != null && ExcelDataSet.Rows.Count > 0)
                            {
                                string SheetName = ExcelDataSet.Rows[0]["TABLE_NAME"].ToString(); // get sheetname
                                ExcelCommand.CommandText = "SELECT * From [" + SheetName + "]";
                                OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
                                ExcelAdapter.SelectCommand = ExcelCommand;
                                ExcelAdapter.Fill(dt);
                            }
                            con.Close();
                            if (dt != null && dt.Rows.Count > 0) // Check if File is Blank or not
                            {
                                grvBatchUpload.DataSource = dt;
                                grvBatchUpload.DataBind();
                                lblMsg.Visible = false;
                            }
                            else
                            {
                                lblMsg.Visible = true;
                                lblMsg.Style.Add("color", "red");
                                lblMsg.Text = "There are No Rows in this File!!!";
                            }
                            //FilePath = ResolveUrl("~/Uploads/");
                            //string fileName = Server.MapPath(FilePath) + filename;
                            //FileInfo f = new FileInfo(fileName);
                            //if (f.Exists)
                            //{
                            //    f.IsReadOnly = false;
                            //    f.Delete();
                            //}
                        }
                        else
                        {
                            lblMsg.Visible = true;
                            lblMsg.Style.Add("color", "red");
                            lblMsg.Text = "Attachment file size should not be greater then 1 MB!";
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblMsg.Visible = true;
                    lblMsg.Style.Add("color", "red");
                    lblMsg.Text = "Error occurred while uploading a file: " + ex.Message;
                }
            }
            else
            {
                lblMsg.Visible = true;
                lblMsg.Style.Add("color", "red");
                lblMsg.Text = "Please select a file to upload.";
            }
        }
&#13;
&#13;
&#13;

结果 enter image description here

你能看到吗。谢谢你。

0 个答案:

没有答案