ASP.NET上传Excel并读取Excel中的内容

  1private void Button_Click(object sender, System.EventArgs e)
  2        {
  3            //获取完整路径
  4            string thefullname=this.uploadFile.PostedFile.FileName;
  5            
  6            if(thefullname == "")
  7            {
  8                Page.RegisterStartupScript("","<script language='javascript'>alert('请选择要上传得Excel文件');</script>");
  9                return;
 10            }

 11
 12            int fileLength = this.uploadFile.PostedFile.ContentLength;
 13            if(fileLength > 512000)
 14            {
 15                Page.RegisterStartupScript("","<script language='javascript'>alert('文件已超过500K,无法上传!');</script>");
 16                return;
 17            }

 18            FileInfo info = new FileInfo(thefullname);
 19
 20            string fileExt = info.Extension;
 21            if(fileExt.ToLower() != ".xls")
 22            {
 23                Page.RegisterStartupScript("","<script language='javascript'>alert('不是Excel文件,请使用正确的文件格式!');</script>");
 24                return;
 25            }

 26            
 27            string uploadPath = Page.MapPath(@"uploadfile\report.xls");
 28
 29            bool upSuccess = Upload(uploadPath);
 30            if(!upSuccess)
 31            {
 32                Page.RegisterStartupScript("","<script language='javascript'>alert('文件上传失败!');</script>");
 33                return;
 34            }

 35
 36            DataTable table = GetExcelTable(uploadPath);
 37            if(table == null)
 38            {
 39                Page.RegisterStartupScript("","<script language='javascript'>alert('文件读取失败!');</script>");
 40                return;
 41            }

 42
 53
 54        }

 55
 56private bool Upload(string uploadPath)
 57        {
 58            try
 59            {
 60                this.uploadFile.PostedFile.SaveAs(uploadPath);    //上传Excel并保存,在这里判断是否保存成功
 61                return true;
 62            }

 63            catch
 64            {
 65                return false;
 66            }

 67        }

 68
 69
 70        private DataTable GetExcelTable(string uploadPath)
 71        {
 72            DataSet ds;
 73            string Xls_ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + uploadPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";//HDR为yes 则第一数据行为列名,为no 则自动为列加列名F1  F2  F3
 74            OleDbConnection Conn = new OleDbConnection(Xls_ConnStr);
 75            try
 76            {    
 77                Conn.Open();
 78                string sql_str = "select * from [Sheet1$]";
 79                OleDbDataAdapter da = new OleDbDataAdapter(sql_str,Conn);
 80                ds = new DataSet();
 81                da.Fill(ds,"excel_data");
 82                Conn.Close();
 83            }

 84            catch
 85            {
 86                if(Conn.State == ConnectionState.Open)
 87                {
 88                    Conn.Close();
 89                }

 90                return null;
 91            }

 92            finally
 93            {
 94                Conn.Dispose();
 95            }

 96
 97            if(ds == null)
 98            {
 99                return null;
100            }

101
102            if(ds.Tables.Count < 1)
103            {
104                return null;
105            }

106            
107            return ds.Tables[0];
108        }
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.

posted on 2008-06-27 07:56 心界使者 阅读(11) 评论(0)  编辑 收藏 所属分类: ASP.NET


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-06-27 08:00 编辑过


相关链接:
 

导航

统计

公告

一切如来心秘密全身舍利宝箧印陀罗尼咒塔

与我联系

搜索

 

留言簿

随笔分类

文章分类

文章档案

Blogs

资源

积分与排名

最新评论