c# winform 程序 读取Excel

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
        
/// 上传Excel文件到服务器端
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

ExpandedBlockStart.gif ContractedBlock.gif          protected   void  btnUpdateComment_Click( object  sender, EventArgs e)  {
            
//第一步,上传EXCEL到服务器端
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if (FileUpload1.HasFile) {
                
//验证EXCEL文件格式
ExpandedSubBlockStart.gifContractedSubBlock.gif
                if (FileUpload1.FileName.ToLower().IndexOf(".xls"== -1{
                    RequiredFieldValidator1.ErrorMessage 
= "不是有效的Excel文件";
                    RequiredFieldValidator1.IsValid 
= false;
                    
return;
                }


                
//EXCEL评论保存位置
                string _FilePath = string.Empty;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (System.Configuration.ConfigurationManager.AppSettings["CommentExcelFile"!= null{
                    _FilePath 
= System.Configuration.ConfigurationManager.AppSettings["CommentExcelFile"].ToString();
                }


                
//如果此目录不存在则创建此目录
ExpandedSubBlockStart.gifContractedSubBlock.gif
                if (!System.IO.Directory.Exists(_FilePath)) {
                    System.IO.Directory.CreateDirectory(_FilePath);
                }


                
//生成文件名称
                string _FileName = DateTime.Now.ToString("yyyyMMddHHmmss"+ "-" + DateTime.Now.Ticks.ToString().Substring(04+ ".xls";
                _FileName 
= _FilePath + @"\" + _FileName;

                
//保存此文件到服务器指定位置
ExpandedSubBlockStart.gifContractedSubBlock.gif
                try {
                    FileUpload1.SaveAs(_FileName);
                    Response.Redirect(
string.Format("CommentDetail.aspx?FileName={0}", _FileName));
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 catch (Exception exp) {
                    
throw exp;
                }

            }

        }


ContractedBlock.gifExpandedBlockStart.gif
读取EXCEL #region 读取EXCEL
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 读取Excel文档
        
/// </summary>
        
/// <param name="Path">文件名称</param>
        
/// <returns>返回一个数据集</returns>
        
/// http://dev.csdn.net/article/72/72658.shtm

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static DataSet ExcelToDS(string Path) {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (!string.IsNullOrEmpty(Path)) {
                
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn 
= new OleDbConnection(strConn);
                conn.Open();
                
string strExcel = "";
                OleDbDataAdapter myCommand 
= null;
                DataSet ds 
= null;
                strExcel 
= "select * from [sheet1$]";
                myCommand 
= new OleDbDataAdapter(strExcel, strConn);
                ds 
= new DataSet();
                myCommand.Fill(ds);
                
return ds;
            }

            
return null;
        }

        
#endregion


ExpandedBlockStart.gifContractedBlock.gif
try   {
                    DataSet ds 
= CommUtil.ExcelToDS(_FileName);
                    
if (ds != null
                        
&& ds.Tables.Count > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
&& ds.Tables[0].Rows.Count > 0{

//

转载于:https://www.cnblogs.com/greatandforever/archive/2009/07/22/1528275.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值