abbott_2024_event/BackEnd/api/exportExcel.ashx.cs

213 lines
7.9 KiB
C#

using System;
using System.Web;
using NPOI;
//using NPOI.SS.UserModel;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.POIFS;
using NPOI.Util;
using System.IO;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Runtime.Remoting;
using Dapper;
using System.Linq;
using NPOI.SS.Formula.Functions;
namespace abbott_2024_event.BackEnd.api
{
/// <summary>
/// exportExcel 的摘要描述
/// </summary>
public class exportExcel : IHttpHandler
{
SqlConnection conn = new SqlConnection(globalClass.appsettings("DBConnectionString"));
public authToken authToken;
public void ProcessRequest(HttpContext context)
{
authToken objAuth = new authToken();
if (!objAuth.user_isLogin)
{
context.Response.Write("尚未登入,請登入後使用");
return;
}
string start = (context.Request["start"] == null) ? "" : context.Request["start"].ToString();
string end = (context.Request["end"] == null) ? "" : context.Request["end"].ToString();
string gender = (context.Request["gender"] == null) ? "" : context.Request["gender"].ToString();
string min_age = (context.Request["min_age"] == null) ? "" : context.Request["min_age"].ToString();
string max_age = (context.Request["max_age"] == null) ? "" : context.Request["max_age"].ToString();
DateTime dateStart;
DateTime dateEnd;
try
{
dateStart = DateTime.Parse(start);
dateEnd = DateTime.Parse(end + " 23:59:59");
}
catch
{
context.Response.Write("測驗起訖日錯誤");
return;
}
if (gender != "%" && gender != "M" && gender != "F")
{
context.Response.Write("性別代碼錯誤!");
return;
}
string filiterString = "";
filiterString += " Where babyData_bindedLine = 'Y' and babyRec_newestRec = 'Y' and babyRec_recdate >= '" + dateStart.ToString("yyyy/MM/dd") + "' and babyRec_recdate <= '" + dateEnd.ToString("yyyy/MM/dd HH:mm:ss") + "' ";
if (gender != "%")
{
filiterString += " and babyData_sexual = '" + gender + "' ";
}
filiterString += " and babyRec_months >= " + (int.Parse(min_age) * 12).ToString() + " and babyRec_months <= " + (int.Parse(max_age) * 12).ToString();
List<babyDataRecView> babyDataRecViews = conn.Query<babyDataRecView>("select * from babyDataRecView " + filiterString + " ").ToList();
HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
headerRow.CreateCell(0).SetCellValue("Line Uid");
headerRow.CreateCell(1).SetCellValue("Line 名稱");
headerRow.CreateCell(2).SetCellValue("電話");
headerRow.CreateCell(3).SetCellValue("寶寶姓名");
headerRow.CreateCell(4).SetCellValue("寶寶性別");
headerRow.CreateCell(5).SetCellValue("生日");
headerRow.CreateCell(6).SetCellValue("測驗日期");
headerRow.CreateCell(7).SetCellValue("測驗時年齡");
headerRow.CreateCell(8).SetCellValue("寶寶身高");
headerRow.CreateCell(9).SetCellValue("寶寶成長百分比");
headerRow.CreateCell(10).SetCellValue("utm_source");
headerRow.CreateCell(11).SetCellValue("utm_medium");
headerRow.CreateCell(12).SetCellValue("utm_campaign");
headerRow.CreateCell(13).SetCellValue("utm_content");
headerRow.CreateCell(14).SetCellValue("utm_term");
int pageNum = 1;
foreach (babyDataRecView view in babyDataRecViews)
{
string baby_gender = "";
if (view.babyData_sexual == "M")
{
baby_gender = "男";
}
else {
baby_gender = "女";
}
string inpercent = "";
switch (view.babyRec_inpercent)
{
case 3:
{
inpercent = "0%~3%";
break;
}
case 15:
{
inpercent = "3%~15%";
break;
}
case 25:
{
inpercent = "15%~25%";
break;
}
case 50:
{
inpercent = "25%~50%";
break;
}
case 75:
{
inpercent = "50%~75%";
break;
}
case 85:
{
inpercent = "75%~85%";
break;
}
case 97:
{
inpercent = "85%~97%";
break;
}
case 100:
{
inpercent = "97%~100%";
break;
}
}
HSSFRow excelRow = (HSSFRow)sheet.CreateRow(pageNum);
excelRow.CreateCell(0).SetCellValue(view.line_uid);
excelRow.CreateCell(1).SetCellValue(view.line_displayName);
excelRow.CreateCell(2).SetCellValue(view.line_phone);
excelRow.CreateCell(3).SetCellValue(view.babyData_name);
excelRow.CreateCell(4).SetCellValue(baby_gender);
excelRow.CreateCell(5).SetCellValue(view.babyData_birthday.ToString("yyyy/MM/dd"));
excelRow.CreateCell(6).SetCellValue(view.babyRec_recdate.ToString("yyyy/MM/dd"));
excelRow.CreateCell(7).SetCellValue(view.babyRec_yearMonthStr);
excelRow.CreateCell(8).SetCellValue(view.babyRec_height);
excelRow.CreateCell(9).SetCellValue(inpercent);
excelRow.CreateCell(10).SetCellValue(view.utm_source);
excelRow.CreateCell(11).SetCellValue(view.utm_medium);
excelRow.CreateCell(12).SetCellValue(view.utm_campaign);
excelRow.CreateCell(13).SetCellValue(view.utm_content);
excelRow.CreateCell(14).SetCellValue(view.utm_term);
pageNum++;
}
for (int j = 0; j <= 14; j++)
{
sheet.AutoSizeColumn(j);
sheet.SetColumnWidth(j, sheet.GetColumnWidth(j) * 17 / 10);
}
workbook.Write(ms);
ms.Flush();
string fileName = HttpUtility.UrlEncode("測驗者寶寶成長資料_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls", Encoding.UTF8);
context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8";
context.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=" + fileName + ";filename*=utf-8''" + fileName));
//context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8";
context.Response.BinaryWrite(ms.ToArray());
}
public bool IsReusable
{
get
{
return false;
}
}
}
}