C#数据库数据导入导出系列之三 数据库导出到Excel下

C#数据库数据导入导出系列之三 数据库导出到Excel下

在日常的项目中,Excel,Word,txt等格式的数据导入到数据库中是很常见的,我在这里做一下总结

这里将分为Asp.net导入Sql Server,Oracle数据库和WinForm导入Sql Server,Oracle数据库。

这里将的数据库数据库导入导出,其实对Sql Server 和Oracle都是通用的

如果使用ADO.Net连接Oracle数据库,需要在引用里添加“System.Data.OracleClient ”,其他方面与连接Sql Server数据库是一样的

SqlConnection cn = new SqlConnection();
OracleConnection oraleCn = new OracleConnection();

如果使用诸如Ibatis等持久层框架的话,唯一的区别就是在数据库连接语句上的差别而已。下面是两个例子

Oracle:Data Source=192.168.0.11/Contact;User ID=system;Password=ss;Unicode=True

Sql Server: Data Source=Contact;Server=localhost;uid=sa;pwd=ss

接着上一篇继续。

2,使用对象集合导出Excel

先给出在网上下载的一个ExcelHelper的类

其基本思想就是先根据反射从传递进来的属性名信息得到要显示的属性

然后根据属性取它的值

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Web;
  4 using System.Reflection;
  5 using System.Text;
  6 
  7 namespace Common.Classes
  8 {
  9     public class ExcelHelper
 10     {  /**/
 11         /// <summary>
 12         /// 将一组对象导出成EXCEL
 13         /// </summary>
 14         /// <typeparam name="T">要导出对象的类型</typeparam>
 15         /// <param name="objList">一组对象</param>
 16         /// <param name="FileName">导出后的文件名</param>
 17         /// <param name="columnInfo">列名信息</param>
 18         public static void ExExcel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo, string FileType)
 19         {
 20             ExExcel(objList, FileName, columnInfo, FileType, null);
 21         }
 22         /**/
 23         /// <summary>
 24         /// 将一组对象导出成EXCEL
 25         /// </summary>
 26         /// <typeparam name="T">要导出对象的类型</typeparam>
 27         /// <param name="objList">一组对象</param>
 28         /// <param name="FileName">导出后的文件名</param>
 29         /// <param name="columnInfo">列名信息</param>
 30         /// <param name="other">追加其他内容</param>
 31         public static void ExExcel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo, string FileType,string other )
 32         {
 33             if(columnInfo.Count == 0)
 34             {
 35                 return;
 36             }
 37             if(objList.Count == 0)
 38             {
 39                 return;
 40             }
 41             //生成EXCEL的HTML
 42             string excelStr = "";
 43 
 44             Type myType = objList[0].GetType();
 45             //根据反射从传递进来的属性名信息得到要显示的属性
 46             List<PropertyInfo> myPro = new List<PropertyInfo>();
 47             foreach(string cName in columnInfo.Keys)
 48             {
 49                 PropertyInfo p = myType.GetProperty(cName);
 50                 if(p != null)
 51                 {
 52                     myPro.Add(p);
 53                     excelStr += columnInfo[cName] + "\t";
 54                 }
 55             }
 56             //如果没有找到可用的属性则结束
 57             if(myPro.Count == 0)
 58             {
 59                 return;
 60             }
 61             excelStr += "\n";
 62 
 63             foreach(T obj in objList)
 64             {
 65                 foreach(PropertyInfo p in myPro)
 66                 {
 67                     //此处是为了对时间格式进行处理
 68                     if(p.Name == "CBirthday")
 69                     {
 70                         string str = p.GetValue(obj, null).ToString();
 71                         string strs = (str == "0001-1-1 0:00:00") ? "" : str;
 72                         if(strs == "")
 73                         {
 74                             excelStr += strs + "\t";
 75                         }
 76                         else
 77                         {
 78                             excelStr += Convert.ToDateTime(strs).ToShortDateString() + "\t";
 79                         }
 80                     }
 81                     else
 82                     {
 83                         excelStr += p.GetValue(obj, null) + "\t";
 84                     }
 85                 }
 86                 excelStr += "\n";
 87             }
 88             if(!string.IsNullOrEmpty(other))
 89             {
 90                 excelStr += other;
 91             }
 92             //输出EXCEL
 93             HttpResponse rs = System.Web.HttpContext.Current.Response;
 94             rs.Clear();
 95             rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
 96             rs.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, Encoding.UTF8));
 97             rs.ContentType = FileType;
 98             rs.Write(excelStr);
 99             rs.End();
100         }
101 
102     }
103 }

数据库中默认的时间是“0001-1-1 0:00:00”,这个在界面上显示的话肯定不好看,在程序中为了适应具体情况,因此,我对“CBirthday”这一列进行了简单的处理。

接下来看一下调用方法

1 public class ContactPersonExport
2 {
3     public string CPName{get;set;}
4     public string CPSex{get;set;}
5     public string CPBirthday{get;set;}
6 }

联系人类。
然后在方法里调用就可以了,contactList 集合的具体值,就靠大家自己去写了

1  List<ContactPersonExportDomain> contactList = (List<ContactPersonExportDomain>)Helper.ContactExport().ExportDataIntoExcel();
2             Dictionary<string, string> columnInfo = new Dictionary<string, string>();
3             columnInfo.Add("CPName", "联系人姓名");
4             columnInfo.Add("CPSex", "性别");
5             columnInfo.Add("CPBirthday", "生日");
6             string FileName = "test.xls";
7             string FileType = "application/ms-excel" //Excel的MIME类型
8             ExcelHelper.ExExcel<ContactPersonExportDomain>(contactList, FileName, columnInfo, FileType);
原文地址:https://www.cnblogs.com/cpcpc/p/2767932.html