您的位置:首页 > 数据库

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

2012-11-13 14:10 435 查看
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

using System;
using System.Collections.Generic;
using System.Web;
using System.Reflection;
using System.Text;

namespace Common.Classes
{
public class ExcelHelper
{  /**/
/// <summary>
/// 将一组对象导出成EXCEL
/// </summary>
/// <typeparam name="T">要导出对象的类型</typeparam>
/// <param name="objList">一组对象</param>
/// <param name="FileName">导出后的文件名</param>
/// <param name="columnInfo">列名信息</param>
public static void ExExcel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo, string FileType)
{
ExExcel(objList, FileName, columnInfo, FileType, null);
}
/**/
/// <summary>
/// 将一组对象导出成EXCEL
/// </summary>
/// <typeparam name="T">要导出对象的类型</typeparam>
/// <param name="objList">一组对象</param>
/// <param name="FileName">导出后的文件名</param>
/// <param name="columnInfo">列名信息</param>
/// <param name="other">追加其他内容</param>
public static void ExExcel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo, string FileType,string other )
{
if(columnInfo.Count == 0)
{
return;
}
if(objList.Count == 0)
{
return;
}
//生成EXCEL的HTML
string excelStr = "";

Type myType = objList[0].GetType();
//根据反射从传递进来的属性名信息得到要显示的属性
List<PropertyInfo> myPro = new List<PropertyInfo>();
foreach(string cName in columnInfo.Keys)
{
PropertyInfo p = myType.GetProperty(cName);
if(p != null)
{
myPro.Add(p);
excelStr += columnInfo[cName] + "\t";
}
}
//如果没有找到可用的属性则结束
if(myPro.Count == 0)
{
return;
}
excelStr += "\n";

foreach(T obj in objList)
{
foreach(PropertyInfo p in myPro)
{
//此处是为了对时间格式进行处理
if(p.Name == "CBirthday")
{
string str = p.GetValue(obj, null).ToString();
string strs = (str == "0001-1-1 0:00:00") ? "" : str;
if(strs == "")
{
excelStr += strs + "\t";
}
else
{
excelStr += Convert.ToDateTime(strs).ToShortDateString() + "\t";
}
}
else
{
excelStr += p.GetValue(obj, null) + "\t";
}
}
excelStr += "\n";
}
if(!string.IsNullOrEmpty(other))
{
excelStr += other;
}
//输出EXCEL
HttpResponse rs = System.Web.HttpContext.Current.Response;
rs.Clear();
rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
rs.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, Encoding.UTF8));
rs.ContentType = FileType;
rs.Write(excelStr);
rs.End();
}

}
}


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

接下来看一下调用方法

public class ContactPersonExport
{
public string CPName{get;set;}
public string CPSex{get;set;}
public string CPBirthday{get;set;}
}


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

List<ContactPersonExportDomain> contactList = (List<ContactPersonExportDomain>)Helper.ContactExport().ExportDataIntoExcel();
Dictionary<string, string> columnInfo = new Dictionary<string, string>();
columnInfo.Add("CPName", "联系人姓名");
columnInfo.Add("CPSex", "性别");
columnInfo.Add("CPBirthday", "生日");
string FileName = "test.xls";
string FileType = "application/ms-excel" //Excel的MIME类型
ExcelHelper.ExExcel<ContactPersonExportDomain>(contactList, FileName, columnInfo, FileType);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐