/// <summary>
/// 导出Excel数据
/// </summary>
/// <param name="search"></param>
/// <returns></returns>
public ActionResult ExportExcel(SearchEntity search)//查询条件
{
List<Models.PerLoan.PersonalLoanExcel> perLoanList = new List<Models.PerLoan.PersonalLoanExcel>();
形成的excel对象[Description("贷款期限")] public int? LoanLimit { get; set; } 形成表标题
perLoanList = PersonalLoanDAL.GetProcessLoanExcelNew(city, search, status, CurrentUser.RoleId, CurrentUser.UserId, CurrentUser);//查询到的列表
string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + "_个人业务查询";
DAL.Common.ExportExcel<Models.PerLoan.PersonalLoanExcel> exportExcel = new DAL.Common.ExportExcel<Models.PerLoan.PersonalLoanExcel>(fileName, perLoanList);
exportExcel.ResponseExcel(1);
return View();
}
public class ExportExcel<T> where T:class
{
public ExportExcel(string fileName, List<T> list)
{
_fileName = fileName;
_listT = list;
}
private HSSFWorkbook workbook;
private string _fileName;
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
private List<T> _listT;
public List<T> ListT
{
get { return _listT; }
set { _listT = value; }
}
/// <summary>
/// 向客户端输出Excel
/// </summary>
/// <param name="ordertype">1 个人 2 经纪人</param>
public void ResponseExcel(int ordertype)
{
HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/vnd.ms-excel";
response.HeaderEncoding = System.Text.Encoding.GetEncoding("gb2312");
response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", _fileName + ".xls"));
response.Clear();
InitialiseWorkBook();
CreateExcelData(ordertype);
using (MemoryStream file = new MemoryStream())
{
workbook.Write(file);
file.WriteTo(response.OutputStream);
}
response.End();
}
private void InitialiseWorkBook()
{
workbook = new HSSFWorkbook();
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "SouFun";
workbook.DocumentSummaryInformation = dsi;
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "Export data";
workbook.SummaryInformation = si;
}
/// <summary>
/// 根据List中的内容生成excel数据
/// </summary>
/// <param name="ordertype">订单类型 1个人 2经纪人</param>
private void CreateExcelData(int ordertype)
{
ISheet sheet = workbook.CreateSheet(_fileName);
IRow rowHead = sheet.CreateRow(0);
Type t = typeof(T);
int column=0;
int headNum = 0;
foreach (PropertyInfo pi in t.GetProperties())
{
string headValue = string.Empty;
object[] des = pi.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (des.Length > 0)
{
DescriptionAttribute da = (DescriptionAttribute)des[0];
headValue = da.Description;
rowHead.CreateCell(headNum, CellType.STRING).SetCellValue(headValue);
headNum++;
}
column++;
}
for (int r = 0; r < _listT.Count; r++)
{
IRow row = sheet.CreateRow(r + 1);
int c = 0;
int c1 = 0;
decimal money=0;
decimal interviermoney = 0;
decimal pidaimoney = 0;
decimal fangkuanmoney = 0;
decimal bankreturncharge = 0;
decimal commissionrate = 0;
int gender = -1;
foreach (PropertyInfo pi in t.GetProperties())
{
string v = string.Empty;
object o = pi.GetValue(_listT[r], null);
object[] des = pi.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (des.Length > 0)
{
if (o != null)
{
if (pi.Name.ToLower() == "status" || pi.Name.ToLower() == "prevstatus")
{
v = DAL.Common.Order.GetCurrentStatus(int.Parse(o.ToString()));
}
else if (pi.Name.ToLower() == "gender") //case when Gender = 0 then ‘女‘ when Gender = 1 then ‘男‘ else ‘‘ end as
{
gender = Convert.ToInt32(o);
if (o == null)
{
v = "";
}
else
{
if (gender==1)
{
v = "男";
}
else if (gender == 0)
{
v = "女";
}
}
}
else if (pi.Name.ToLower() == "datafrom")
{
v = DAL.Common.Order.GetOrderFrom(int.Parse(o.ToString()), ordertype);
}
else if (pi.Name.ToLower() == "interviewmoney")
{
interviermoney = Convert.ToDecimal(o);
v = interviermoney.ToString() + "万元";
}
else if(pi.Name.ToLower()=="allowappmoney")
{
pidaimoney = Convert.ToDecimal(o);
v = pidaimoney.ToString() + "万元";
}
else if (pi.Name.ToLower() == "fangkuanmoney")
{
fangkuanmoney = Convert.ToDecimal(o);
v = fangkuanmoney.ToString();
}
else if (pi.Name.ToLower() == "bankreturncharge")
{
bankreturncharge = Convert.ToDecimal(o);
v = bankreturncharge.ToString() + "%";
}
else if (pi.Name.ToLower() == "commissionrate")
{
commissionrate = Convert.ToDecimal(o);
v = commissionrate.ToString() + "%";
}
//else if (pi.Name.ToLower() == "nicknamefrom")
//{
// if (o.ToString() == "0") { v = "金融"; }
// else if (o.ToString() == "1") { v = "二手房"; }
//}
else if (pi.Name.ToLower() == "bankreturnmoney")
{
if (Convert.ToDecimal(o) <= 0M)
{
if (interviermoney > 0) { money = interviermoney; }
else if (pidaimoney > 0) { money = pidaimoney; }
else if (fangkuanmoney > 0) { money = fangkuanmoney; }
if (money > 0 && bankreturncharge > 0)
{
v = (money * 10000 * bankreturncharge / 100).ToString() + "元";
}
}
else
{
v = o.ToString() + "元";
}
}
else if (pi.Name.ToLower() == "commissionmoney")
{
if (interviermoney > 0) { money = interviermoney; }
else if (pidaimoney > 0) { money = pidaimoney; }
else if (fangkuanmoney > 0) { money = fangkuanmoney; }
if (money > 0 && commissionrate > 0)
{
v = (money * commissionrate / 100).ToString() + "万元";
}
}
else if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type ptype = pi.PropertyType.GetGenericArguments()[0];
if (ptype == typeof(System.DateTime))
{
v = Convert.ToDateTime(o).ToString("yyyy-MM-dd");
}
else
{
v = o.ToString();
}
}
else
{
v = o.ToString();
}
}
row.CreateCell(c1, CellType.STRING).SetCellValue(v);
c1++;
}
c++;
}
}
}
}