| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 431 人关注过本帖
标题:高手给看看 谢谢!!!
只看楼主 加入收藏
wenzhiyuan
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-11-13
收藏
 问题点数:0 回复次数:1 
高手给看看 谢谢!!!
我要在GV里把数据导到Excel里,但是导出来的Excel修改数据在导回数据库的时候数据库没有修改后的数据。。。。


       protected void Page_Load(object sender, EventArgs e)
        {
            BindGridData();
        }

        private void BindGridData()
        {
            gvSuccessManage.DataSource = nbo.dao.FindAll();
            gvSuccessManage.DataBind();
        }

        private void Export(string FileType, string FileName)
        {
            string style = @"<style> .text { mso-number-format:\@; } </script> ";
            gvSuccessManage.AllowPaging = false;
            gvSuccessManage.AllowSorting = false;
            BindGridData();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            gvSuccessManage.RenderControl(hw);
            Response.Write(style);
            Response.Write(tw.ToString());
            Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            Export("application/ms-excel", "学生成绩报表.xls");
        }

        protected void gvSuccessManage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Attributes.Add("class", "text");
                e.Row.Cells[3].Attributes.Add("class", "text");
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
                //上传文件的绝对路径
                string sFile = FileUpload1.PostedFile.FileName;
                //获取文件全名
                sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
                //获取后缀名
                sFile = sFile.Substring(sFile.LastIndexOf("."));
                string dbName = Path.GetFileName(FileUpload1.FileName).Replace(".xls", "");
                if (sFile.ToLower() != ".xls")
                {
                    Response.Write("请选择Excel文件!");
                    Response.End();
                }
                //为了防止重名,获得日期为文件名年月日时分秒毫秒
                string datatime = System.DateTime.Now.ToString("yyyMMddHHmmssffff");
                //上传后文件的新名
                sFile = datatime + sFile;
                AppDomain.CurrentDomain.BaseDirectory.ToString(); //获取此项目的根目录
                //sPath 获取上传后的路径
                var directory = AppDomain.CurrentDomain.BaseDirectory.ToString() + "ExcelFiles\\";
                string sPath = directory + sFile;
                if (!Directory.Exists(directory))
                    Directory.CreateDirectory(directory);
                //上传文件
                FileUpload1.PostedFile.SaveAs(sPath);
                var ds = nbo.GetExcelContent(sPath, dbName);
                var dt = ds.Tables[0];
                int count = 0;
                NRSVO vo  = new NRSVO();
                foreach (DataRow dr in dt.Rows)
                {
                    if (count != 0)
                    {
                        vo.username = dr[0].ToString();
                        vo.age = Convert.ToInt32(dr[1].ToString());
                        vo.birthday  = Convert.ToDateTime(dr[2].ToString());
                        vo.identityID = dr[3].ToString();
                        vo. unit = dr[4].ToString();
                        vo. phone = dr[5].ToString();
                        vo. address = dr[6].ToString();
                        vo. grade = Convert.ToInt32(dr[7].ToString());
                        nbo.Save(vo);                        
                    }
                    count++;
                }

            }
            BindGridData();
        }


    }
搜索更多相关主题的帖子: private 数据库 Excel style 
2009-11-13 18:06
wenzhiyuan
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-11-13
收藏
得分:0 
现在每导入一次能修改一条数据,帮忙给看看谢谢大家了 !
2009-11-13 18:09
快速回复:高手给看看 谢谢!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033787 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved