码迷,mamicode.com
首页 > 其他好文 > 详细

Excel数据导出

时间:2017-03-20 00:13:59      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:datatable   mic   ini   closed   model   source   amp   文件名   str   

技术分享
 1 public static DataSet GetExcelData(string FilePathName, string Table)
 2         {
 3             try
 4             {
 5                 string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FilePathName + ";Extended Properties=‘Excel 12.0; HDR=NO; IMEX=1‘";
 6                 using (StreamWriter sw = new StreamWriter(@"c:\test1.txt"))
 7                 {
 8 
 9                     sw.Write(strConn);
10                     sw.Write("try");
11                     sw.WriteLine(DateTime.Now);
12                 }
13                 OleDbConnection conn = new OleDbConnection(strConn);
14                 conn.Open();
15 
16                 DataSet ds = new DataSet();
17                 OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$] where F1 <> ‘‘ ", conn);
18                 odda.Fill(ds, Table);
19                 conn.Close();
20                 return ds;
21             }
22             catch (Exception e)
23             {
24                 using (StreamWriter sw = new StreamWriter(@"c:\test1.txt"))
25                 {
26 
27                     sw.Write(e);
28                     sw.Write("catch");
29                     sw.WriteLine(DateTime.Now);
30                 }
31                 return null;
32             }
33             
34         }
View Code
技术分享
  1  protected void btnAdd_Click(object sender, EventArgs e)
  2         {
  3             try
  4             {
  5                 //HasFile用来检查FileUpload是否有指定文件
  6                 if (fupTeaInfo.HasFile == false)
  7                 {
  8                     GetJsMessage("请您选择Excel文件");
  9 
 10                     return;
 11                 }
 12 
 13                 //获得文件的扩展名
 14                 string IsExcel = System.IO.Path.GetExtension(fupTeaInfo.FileName).ToString().ToLower();
 15                 if (IsExcel != ".xls" && IsExcel != ".xlsx")
 16                 {
 17                     GetJsMessage("请您选择Excel文件");
 18 
 19                     return;
 20                 }
 21 
 22                 //获取Execle文件名  DateTime日期函数
 23                 string filename = DateTime.Now.ToString("yyyymmddhhMMss") + fupTeaInfo.FileName;
 24                 //Server.MapPath 获得虚拟服务器相对路径
 25                 string savePath = Server.MapPath(("~\\upfiles\\") + filename);
 26                 //SaveAs 将上传的文件内容保存在服务器上
 27                 fupTeaInfo.SaveAs(savePath);
 28                 //调用自定义方法   
 29                 DataSet ds = PagerHelper.GetExcelData(savePath, filename);
 30                 if (ds == null)
 31                 {
 32                     GetJsMessage("操作失败!");
 33 
 34                     return;
 35                 }
 36 
 37                 DataTable dt = ds.Tables[0];
 38                 if (dt.Rows.Count == 0)
 39                 {
 40                     GetJsMessage("Excel表为空");
 41 
 42                     return;
 43                 }
 44                 else
 45                 {
 46                     DataView Dv = new DataView(dt);
 47                     //存储错误的数据
 48                     Dictionary<string, JiaSoft.RWXY.Entity.TeacherInError> TeacherInError_dic = new Dictionary<string, Entity.TeacherInError>();
 49                     //存储正确的数据
 50                     Dictionary<string, JiaSoft.RWXY.Entity.Teacher> Teacher_dic = new Dictionary<string, Entity.Teacher>();
 51                     Dictionary<string, JiaSoft.RWXY.Entity.TeaDetail> TeaDetail_dic = new Dictionary<string, Entity.TeaDetail>();
 52 
 53                     for (int i = 1; i < Dv.Count; i++)
 54                     {
 55                         DataRowView Row = Dv[i];
 56                         Dictionary<string, string> Parameters = new Dictionary<string, string>();                       
 57 
 58                         //判断是否为空行
 59                         if (Row[0] == null || Row[0].ToString().Trim() == "")
 60                         {
 61                             break;
 62                         }
 63                         //获取主表实体信息
 64                         JiaSoft.RWXY.Entity.Teacher TeaEntity = new Entity.Teacher();
 65                         TeaEntity = GetTeaEntity(Row, i, Teacher_dic, TeaDetail_dic, TeacherInError_dic);
 66 
 67                         //获取明细表信息
 68                         JiaSoft.RWXY.Entity.TeaDetail DelEntity = new Entity.TeaDetail();
 69                         DelEntity = GetDelEntity(Row, i, Teacher_dic, TeaDetail_dic, TeacherInError_dic);
 70                                              
 71                     }
 72 
 73                     int Result = 0;
 74                     if (Teacher_dic.Keys.Count > 0)
 75                     {
 76                         Result = TeacherInfo.ApplyMoreAdd(Teacher_dic, TeaDetail_dic);
 77                         GetJsMessage("操作成功");
 78                     }
 79                     else
 80                     {
 81                         foreach (string keyValue in TeacherInError_dic.Keys)
 82                         {
 83                            GetJsMessage(TeacherInError_dic[keyValue].ErrMsg.ToString()); //这个就是 value         
 84                         }
 85                         //GetJsMessage("无正确数据导入!");
 86                     }
 87 
 88                     if (TeacherInError_dic.Keys.Count > 0)
 89                     {
 90                         ViewState["TeacherInError_dic"] = TeacherInError_dic;
 91                         btnImport.Visible = true;
 92                     }
 93                     else
 94                     {
 95                         btnImport.Visible = false;
 96                     }
 97 
 98                 //添加操作日志信息
 99                 LogModel.UserName = hidUserName.Value;
100                 LogModel.Menu = "教师档案导入";
101                 LogModel.FunctionName = "btnAdd_Click";
102                 LogModel.Type = "添加";
103                 //操作日志添加方法
104                 LogInfo.Add(LogModel);
105                     
106                 }
107             }
108             catch(Exception ex)
109             {
110                 GetJsMessage("操作失败");
111             }
112             
113         }
View Code

 

Excel数据导出

标签:datatable   mic   ini   closed   model   source   amp   文件名   str   

原文地址:http://www.cnblogs.com/wangsen123/p/6582829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!