zoukankan      html  css  js  c++  java
  • C#excel导入

      1   protected void btnImport_Click(object sender, EventArgs e)
      2     {
      3         string strName = "";
      4         string strFileTailName = "";
      5         String strError = string.Empty;
      6         //选取文件的完整路径
      7         strName = uploadFile.PostedFile.FileName.ToString();
      8         if (string.IsNullOrEmpty(strName))
      9         {
     10             webTools.InsertAlert(this.Page, "请选择导入文件!");
     11             return;
     12         }
     13         int intExt = strName.LastIndexOf(".");
     14         string strExt = strName.Substring(intExt);
     15         DateTime datNow = DateTime.Now;
     16         string strNewName = datNow.DayOfYear.ToString() +
     17             uploadFile.PostedFile.ContentLength.ToString() + strExt;
     18         int intPath = strName.LastIndexOf("\");
     19         strNewName = strName.Substring(intPath);
     20         string strSaveDir = Request.ApplicationPath + "/upload/";
     21         string xlsFilePath = Server.MapPath(strSaveDir + strNewName);
     22         strFileTailName = Path.GetExtension(xlsFilePath);
     23         uploadFile.PostedFile.SaveAs(xlsFilePath);
     24         DataSet ds = new DataSet();
     25         //String cnnstring = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + xlsFilePath + ";Extended Properties="Excel 12.0;HDR=No;IMEX=1"";
     26         String cnnstring = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + xlsFilePath + ";Extended Properties="Excel 12.0 Xml;HDR=No;IMEX=1"";
     27         System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(cnnstring);
     28         System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", conn);
     29         try
     30         {
     31             adapter.Fill(ds);
     32         }
     33         catch (OleDbException ex)
     34         {
     35             webTools.InsertAlert(this.Page, "输入文件格式不正确");
     36         }
     37 
     38         if (ds == null)
     39         {
     40             return;
     41         }
     42         LY_S_DOCBASE_TABLE tableDoc = new LY_S_DOCBASE_TABLE();
     43         DataRow rowDoc = null;
     44         bool retValue = true;
     45         try
     46         {
     47             foreach (DataRow row in ds.Tables[0].Rows)
     48             {
     49                 if (!string.IsNullOrEmpty(row["F7"].ToString()))
     50                 {
     51                     rowDoc = tableDoc.NewRow();
     52                     rowDoc[LY_S_DOCBASE_TABLE.REGISTER_NUM_FIELD] = row["F7"].ToString();
     53                     rowDoc[LY_S_DOCBASE_TABLE.APPLYER_FIELD] = row["F2"].ToString();
     54                     String strGzResult = row["F8"].ToString();
     55                     if (strGzResult == "合格")
     56                         rowDoc[LY_S_DOCBASE_TABLE.FIRST_GSJG_FIELD] = 0;
     57                     else if (strGzResult == "有异议已通过")
     58                         rowDoc[LY_S_DOCBASE_TABLE.FIRST_GSJG_FIELD] = 1;
     59                     else if (strGzResult == "有异议不通过")
     60                         rowDoc[LY_S_DOCBASE_TABLE.FIRST_GSJG_FIELD] = 2;
     61                     else
     62                         throw new NullReferenceException("公示结果请输入合格或有异议已通过或有异议不通过");
     63                     try
     64                     {
     65                         rowDoc[LY_S_DOCBASE_TABLE.FIRST_GSREMARK_FIELD] = row["F9"].ToString();
     66                     }
     67                     catch
     68                     {
     69                         rowDoc[LY_S_DOCBASE_TABLE.FIRST_GSREMARK_FIELD] = string.Empty;
     70                     }
     71                     try
     72                     {
     73                         String strIDCard = row["F4"].ToString();
     74                         if (strIDCard.Length != 15 || strIDCard.Length != 18)
     75                         {
     76                             rowDoc[LY_S_DOCBASE_TABLE.ID_CARD_FIELD] = strIDCard;
     77                         }
     78                         else
     79                         {
     80                             throw new NullReferenceException("身份证号请输入15位或18位");
     81                         }
     82                     }
     83                     catch
     84                     {
     85                         throw new NullReferenceException("身份证号请输入15位或18位");
     86                     }
     87                     tableDoc.Rows.Add(rowDoc);
     88                     tableDoc.AcceptChanges();
     89                 }
     90             }
     91         }
     92 
     93         catch (System.Exception ex)
     94         {
     95             retValue = false;
     96             webTools.InsertAlert(this.Page, "公示数据导入失败,请检查导入的数据后,重新导入!" + ex.Message);
     97         }
     98         if (retValue)
     99         {
    100 
    101             BhDdsXjspfSystem mySystem = new BhDdsXjspfSystem(this.UserSessionID);
    102             retValue = mySystem.UpdateGsInfo(tableDoc, out strError);
    103         }
    104         try
    105         {
    106             if (retValue)
    107             {
    108                 webTools.InsertAlert(this.Page, "已成功导入公示数据!");
    109                 rptImport.DataSource = tableDoc;
    110                 rptImport.DataBind();
    111             }
    112             else
    113             {
    114                 webTools.InsertAlert(this.Page, webTools.FilterInvalidString(strError));
    115             }
    116 
    117         }
    118         catch (System.Exception ex)
    119         {
    120             webTools.InsertAlert(this.Page, "公示数据导入失败:" + ex.Message);
    121         }
    122     }
  • 相关阅读:
    html5 Canvas画图3:1px线条模糊问题
    移动端适配
    JSON数据转换方法 parse()和stringify()
    原生的强大DOM选择器querySelector
    正则去空格方法
    HTML5开发手机应用--viewport的作用
    编译原理的认识与感悟
    CSS实例:图片导航块
    导航,头部,CSS基础
    web基础,用html元素制作web页面
  • 原文地址:https://www.cnblogs.com/wangjp-1233/p/10610604.html
Copyright © 2011-2022 走看看