C#excel导入

Posted wangjp-1233

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     }

 

以上是关于C#excel导入的主要内容,如果未能解决你的问题,请参考以下文章

C# Excel 文件导入到 GridView 导致 OleDB 错误

封装excel数据层代码,log模块导入

怎么把C语言中产生的数据导入到Excel表格

通过 VBA 将 XML 导入 EXCEL 不会刷新

C#Winform怎么将excel一列数据导入程序中

用lingo导入excel数据生成矩阵并对元素进行累加怎么敲代码