using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GWT.APP.Water.WDM.IBusiness;
using GWT.APP.Water.WDM.Entity;
using GWT.Framework.Communication.Portal.Web;
using GWT.APP.Water.WBM.Entity.Model;
using System.Drawing;
using System.Data;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Reflection;
using System.ComponentModel;
using System.IO;
namespace GWT.APP.Water.WDM.Web.Controllers
{
/// <summary>
/// 区域生态补偿
/// </summary>
public class EcologicalCompensationController : WDMBaseController
{
private IHourDataBusiness hourDataBusiness;
public EcologicalCompensationController()
{
hourDataBusiness = BusinessServiceManager.GetServiceByType(typeof(IHourDataBusiness)) as IHourDataBusiness;
}
/// <summary>
/// 区域生态补偿
/// </summary>
/// <returns></returns>
public ActionResult EcologicalCompensationReport()
{
ViewBag.StartTime = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
return View();
}
[AllowAnonymous]
public ActionResult GetEcologicalCompensationDayReport(EcologicalCompensationReportParams ecologicalCompensationReportParams)
{
BaseResult br = new BaseResult();
try
{
IList <EcologicalCompensationReport> list = hourDataBusiness.GetEcologicalCompensationReport(ecologicalCompensationReportParams);
br.State = 1;
List<EcologicalCompensationReport> lstDay = list.Where(p => p.Flag == "2").ToList();
List<EcologicalCompensationReport> lstMonth = list.Where(p => p.Flag == "3").ToList();
br.Data = lstDay;
var relativePath = ("~/Customiz/Document/Excel/");
string fileName = string.Format("江苏省太湖流域所有地区{0}年{1}月生态补偿站报表.xls",ecologicalCompensationReportParams.YearMonth.Substring(0,4),
ecologicalCompensationReportParams.YearMonth.Substring(5, 2));
string [] sheetName ={ "日均值", "月均值" };
List<EcologicalCompensationReport>[] data = { lstDay, lstMonth };
Dictionary<int, int> colWidth = new Dictionary<int, int>();
colWidth.Add(0, 10);
colWidth.Add(1, 20);
colWidth.Add(2, 20);
colWidth.Add(3, 20);
colWidth.Add(4, 20);
colWidth.Add(5, 20);
colWidth.Add(6, 40);
ExcelHelper<EcologicalCompensationReport>.SaveExcelFile(Server.MapPath(relativePath) + "//" + fileName,sheetName, colWidth, data);
//br.Data = new
//{
// EcologicalCompensationReportMonth = list.Where(p=>p.Flag == "2").ToList<EcologicalCompensationReport>(),
// EcologicalCompensationReportDay = list.Where(p => p.Flag == "3").ToList<EcologicalCompensationReport>()
//};
}
catch (Exception ex)
{
br.State = 0;
}
return Json(br);
}
[AllowAnonymous]
public ActionResult GetEcologicalCompensationMonthReport(EcologicalCompensationReportParams ecologicalCompensationReportParams)
{
BaseResult br = new BaseResult();
try
{
IList<EcologicalCompensationReport> list = hourDataBusiness.GetEcologicalCompensationReport(ecologicalCompensationReportParams);
br.State = 1;
br.Data = list.Where(p => p.Flag == "3").ToList();
//br.Data = new
//{
// EcologicalCompensationReportMonth = list.Where(p=>p.Flag == "2").ToList<EcologicalCompensationReport>(),
// EcologicalCompensationReportDay = list.Where(p => p.Flag == "3").ToList<EcologicalCompensationReport>()
//};
}
catch (Exception ex)
{
br.State = 0;
}
return Json(br);
}
/// <summary>
/// 下载导出的 Excel
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public FileStreamResult DownloadFile(EcologicalCompensationReportParams ecologicalCompensationReportParams)
{
var relativePath = ("~/Customiz/Document/Excel/");
string fileName = string.Format("江苏省太湖流域所有地区{0}年{1}月生态补偿站报表.xls", ecologicalCompensationReportParams.YearMonth.Substring(0, 4),
ecologicalCompensationReportParams.YearMonth.Substring(5, 2));
string absoluFilePath = Server.MapPath(relativePath) + fileName;
if (!System.IO.File.Exists(absoluFilePath))
return null;
if (HttpContext.Request.ServerVariables["http_user_agent"].ToLower().IndexOf("firefox") == -1)
{
return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
}
else
{
return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", fileName);
}
}
}
/// <summary>
/// Excel操作类
/// </summary>
/// <typeparam name="T">实体类</typeparam>
public class ExcelHelper<T> where T : class
{
/// <summary>
/// 保存Excel文件
/// </summary>
/// <param name="excelName">Excel文件名</param>
/// <param name="sheetName">Sheet工作表名</param>
/// <param name="data">实体类对象</param>
public static void SaveExcelFile(string excelName, string[] sheetName,Dictionary<int,int> colWidth, List<T>[] data)
{
IWorkbook workBook = new HSSFWorkbook(); //创建一个Excel文档
for (int k = 0; k < sheetName.Length; k++)
{
ISheet sheet = workBook.CreateSheet(sheetName[k]); //创建一个工作表Sheet
int rowNum = 0;
var row = sheet.CreateRow(sheet.LastRowNum); //LastRowNum记录当前可用写入的行索引
PropertyInfo[] preInfo = typeof(T).GetProperties();//获取这个实体对象的所有属性
foreach (var item in preInfo)
{
object[] objPres = item.GetCustomAttributes(typeof(DescriptionAttribute), true);//获取当前属性的自定义特性列表
if (objPres.Length > 0)
{
for (int i = 0; i < objPres.Length; i++)
{
row.CreateCell(rowNum).SetCellValue(((DescriptionAttribute)objPres[i]).Description);//创建行,将当前自定义特性写入
rowNum++;//行索引加1,下次往后一格创建行
}
}
}
int j = sheet.LastRowNum + 1, columnNum = 0;
foreach (var item in data[k])
{
columnNum = 0;
row = sheet.CreateRow(j++);
var itemProps = item.GetType().GetProperties(); //获取当前对象的属性列表
foreach (var itemPropSub in itemProps)
{
//获取当前对象特性中的自定义特性[Description("自定义特性")]
var objs = itemPropSub.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (objs.Length > 0)
{
//将当前对象的特性值,插入当前行的第n列单元格
row.CreateCell(columnNum).SetCellValue(itemPropSub.GetValue(item, null) == null ? "" : itemPropSub.GetValue(item, null).ToString());
sheet.SetColumnWidth(columnNum, colWidth[columnNum] * 256);
columnNum++;
}
}
}
//文件流写入
using (MemoryStream ms = new MemoryStream())
{
workBook.Write(ms);
using (FileStream fs = new FileStream(excelName, FileMode.Create, FileAccess.Write))
{
ms.WriteTo(fs);
}
ms.Flush();
ms.Position = 0;
}
}
}
}
}