数据导出

1.Web层方法

1  getNewExcel(export_Infoservice.GetcourseInfoforIPN(start, end), type, "XXXX数据导出", out path, out b);
View Code

将所要导出的数据 dt 导出

 1 private void getNewExcel(DataTable dt, string fileName, string type, out string 
 2 
 3 filePath, out bool b)
 4         {
 5             b = false;
 6             StringWriter stringWriter = new StringWriter();
 7             HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
 8             DataGrid excel = new DataGrid();
 9             excel.DataSource = dt.DefaultView;
10             //绑定到DataGrid
11             excel.DataBind();
12             excel.RenderControl(htmlWriter);
13             //这里指定文件的路径
14             string time = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-ms");
15             string fullFileName = fileName + type + "-" + time + ".xls";
16             string iisPath = HostingEnvironment.MapPath(@"/Excels/" + 
17 
18 fullFileName);
19             filePath = VCommons.WebConfig.GetWebConfig("ExportData_path", 
20 
21 "/ExportData/Excels/");
22             int pos = iisPath.LastIndexOf("\");
23             string files = iisPath.Substring(0, pos);
24             if (!Directory.Exists(files))
25             {
26                 Directory.CreateDirectory(files);
27             }
28             System.IO.StreamWriter sw = new StreamWriter(iisPath, false, 
29 
30 Encoding.UTF8);
31             sw.Write(stringWriter.ToString());
32             sw.Close();
33             FtpUpload ft = new FtpUpload();
34             if (ft.Upload(iisPath, fullFileName, filePath))
35             {
36                 filePath = VCommons.WebConfig.GetWebConfig("website_resource", 
37 
38 "http://192.168.2.101:807") + filePath + fullFileName;
39                 FileInfo fileInfo = new FileInfo(iisPath);
40                 fileInfo.Delete();
41                 b = true;
42             }
43         }
View Code

GetWebConfig获取webconfig中的参数

 1 public static string GetWebConfig(string strKey, string strDefault)
 2         {
 3             if (System.Configuration.ConfigurationManager.AppSettings[strKey] == 
 4 
 5 null)
 6             {
 7                 return strDefault;
 8             }
 9             else
10             {
11                 return System.Configuration.ConfigurationManager.AppSettings
12 
13 [strKey].ToString();
14             }
15         }
View Code

配置Web.config

1 <configuration>
2     <appSettings>
3         <add key="ExportData_path" value="/ExportData/Excels/" />
4         <add key="website_resource" value="http://192.168.2.101:807" />
5     </appSettings>
6 </configuration>
View Code

2.BLL层方法

1 public DataTable GetcourseInfoforIPN(DateTime start, DateTime end)
2         {
3             return data.GetcourseInfoforIPN(start, end);
4         }
View Code

3.DAL层方法
从数据库查找所需数据

 1 public DataTable GetcourseInfoforIPN(DateTime start, DateTime end)
 2         {
 3             var linq = from _ClassroomInfo in new 
 4 
 5 TsingDa_NewLearningBarRepository<Classroom_Info>(base.UnitWork).GetModel().Where(i 
 6 
 7 => i.AddTime >= start && i.AddTime <= end && i.Status != (int)
 8 
 9 ClassroomStatus.Delete && i.Status != 5 && i.Status != 516)
10                        join _PlanTask in new CourseManagement_PlanTaskRepository
11 
12 (base.UnitWork).GetModelExterior()
13                         on _ClassroomInfo.ClassroomInfoID equals 
14 
15 _PlanTask.ClassroomInfoID into _PlanTaskS
16                        select new courseInfo
17                        {
18                            Id = _ClassroomInfo.ClassroomInfoID,
19                            userInfoid = _ClassroomInfo.UserInfoID,
20                            Title = _ClassroomInfo.Title,
21                            Addtime = _ClassroomInfo.AddTime    
22                           
23 
24 
25                        };
26             List<courseInfo> list = linq.ToList();
27             DataTable dt = CopyToDataTable(list);
28             return dt;
29         }
View Code

格式转换

 1 public DataTable CopyToDataTable<T>(List<T> array)
 2         {
 3             var ret = new DataTable();
 4             Type type = typeof(T);
 5             foreach (var item in type.GetProperties())
 6             {
 7                 DescriptionAttribute descriptAttribute = (DescriptionAttribute)
 8 
 9 Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute));
10                 string columeName = descriptAttribute == null ? item.Name : 
11 
12 descriptAttribute.Description;
13                 ret.Columns.Add(columeName, item.PropertyType);
14             }
15 
16             foreach (T item in array)
17             {
18                 var Row = ret.NewRow();
19                 foreach (var item2 in type.GetProperties())
20                 {
21                     DescriptionAttribute descriptAttribute = 
22 
23 (DescriptionAttribute)Attribute.GetCustomAttribute(item2, typeof
24 
25 (DescriptionAttribute));
26                     string columeName = descriptAttribute == null ? item2.Name : 
27 
28 descriptAttribute.Description;
29                     Row[columeName] = item2.GetValue(item);
30                 }
31                 ret.Rows.Add(Row);
32             }
33             return ret;
34         }
View Code
原文地址:https://www.cnblogs.com/xhyang/p/caoshangchenguang.html