<%@ WebHandler Language="C#" Class="exportCountReport" %> using System; using System.Web; using NPOI; 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.Linq; using System.Text.RegularExpressions; public class exportCountReport : IHttpHandler, IReadOnlySessionState { public void ProcessRequest (HttpContext context) { authPermission objAuth = new authPermission(); if (!objAuth.isLogin()) { context.Response.Write("尚未登入! 或時間過久已被登出!"); return; } string project_uid = (context.Request["project_uid"] == null) ? "" : context.Request["project_uid"].ToString(); string startDate = (context.Request["startDate"] == null) ? "" : context.Request["startDate"].ToString(); string endDate = (context.Request["endDate"] == null) ? "" : context.Request["endDate"].ToString(); string url_uid = (context.Request["url_uid"] == null) ? "" : context.Request["url_uid"].ToString(); string type = (context.Request["type"] == null) ? "" : context.Request["type"].ToString(); string errmsg = ""; if (globalClass.IsDate(startDate) == false) { errmsg = "請輸入開始日期!\n"; } if (globalClass.IsDate(endDate) == false) { errmsg = "請輸入結束日期!\n"; } if (type != "day" && type != "total" && type != "month" && type != "hour") { errmsg += "無正確的統計方式!\n"; } if (errmsg.Length > 0) { context.Response.Write(errmsg); return; } string urlString = string.Format("select * from url where url_uid = '{0}' and url_isRevoke = 'N' ", url_uid); autoBindDataTable urlSQL = new autoBindDataTable(urlString); if (urlSQL.dataRows.Count == 0) { context.Response.Write("找不到此 url_uid 的資料!"); return; } string url_descript = urlSQL.dataRows[0]["url_descript"].ToString(); HSSFWorkbook workbook = new HSSFWorkbook(); MemoryStream ms = new MemoryStream(); HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(); HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0); DateTime dateStart = DateTime.Parse(startDate); DateTime dateEnd = DateTime.Parse(endDate + " 23:59:59"); if (type == "hour") { headerRow.CreateCell(0).SetCellValue("日期時間"); headerRow.CreateCell(1).SetCellValue("次數"); string projString = string.Format("select * from projects where project_isRevoke = 'N' and project_uid = '{0}'", urlSQL.dataRows[0]["project_uid"].ToString()); autoBindDataTable projSQL = new autoBindDataTable(projString); project_uid = urlSQL.dataRows[0]["project_uid"].ToString(); if (projSQL.dataRows.Count == 0) { context.Response.Write("找不到此URL的專案資料!"); return; } string recTableName = projSQL.dataRows[0]["project_recTableName"].ToString(); string rangeString = string.Format("select * from {0} where project_uid = '{1}' and url_uid = '{2}' and rec_createdate >= '{3}' and rec_createdate <= '{4}'", recTableName, project_uid, url_uid, dateStart.ToString("yyyy/MM/dd HH:mm:ss"), dateEnd.ToString("yyyy/MM/dd HH:mm:ss")); autoBindDataTable rangeSQL = new autoBindDataTable(rangeString); DateTime tmpDate = dateStart; int rowNum = 1; int intTotal = 0; while (tmpDate <= dateEnd) { DateTime dateS = DateTime.Parse(tmpDate.ToString("yyyy/MM/dd HH:00:00")); DateTime dateE = DateTime.Parse(tmpDate.AddHours(1).ToString("yyyy/MM/dd HH:00:00")).AddSeconds(-1); DataRow[] objTmp = rangeSQL.selectedRows(string.Format("rec_createdate >= '{0}' and rec_createdate <= '{1}'", dateS.ToString("yyyy/MM/dd HH:mm:ss"), dateE.ToString("yyyy/MM/dd HH:mm:ss"))); //countData objCount = new countData(); //objRet.data.Add(objCount); string col1 = tmpDate.ToString("yyyy/MM/dd") + " " + tmpDate.ToString("HH") + ":00:00 - " + tmpDate.ToString("HH") + ":59:59"; HSSFRow excelRow = (HSSFRow)sheet.CreateRow(rowNum); excelRow.CreateCell(0).SetCellValue(col1); if (objTmp.Length == 0) { excelRow.CreateCell(1).SetCellValue(0); } else { excelRow.CreateCell(1).SetCellValue(objTmp.Length); intTotal += objTmp.Length; } tmpDate = tmpDate.AddHours(1); rowNum += 1; } HSSFRow excelRow2 = (HSSFRow)sheet.CreateRow(rowNum); excelRow2.CreateCell(0).SetCellValue("總計"); excelRow2.CreateCell(1).SetCellValue(intTotal); workbook.Write(ms); ms.Flush(); context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8"; context.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=" + HttpUtility.UrlEncode(url_descript.Trim() + "_" + DateTime.Now.ToString("yyyy-MM-dd"), Encoding.UTF8) + ".xls")); context.Response.BinaryWrite(ms.ToArray()); return; } if (type == "month") { headerRow.CreateCell(0).SetCellValue("日期時間"); headerRow.CreateCell(1).SetCellValue("次數"); string rangeString = string.Format("select * from recSubTotal where url_uid = '{0}' and recSubTotal_date >= '{1}' and recSubTotal_date <= '{2}'", url_uid, dateStart.ToString("yyyy/MM/dd HH:mm:ss"), dateEnd.ToString("yyyy/MM/dd HH:mm:ss")); autoBindDataTable rangeSQL = new autoBindDataTable(rangeString); DateTime tmpDate = dateStart; int intTotal = 0; int rowNum = 1; while (tmpDate <= dateEnd) { DateTime dateS = DateTime.Parse(tmpDate.ToString("yyyy/MM/01 00:00:00")); DateTime dateE = DateTime.Parse(tmpDate.AddMonths(1).ToString("yyyy/MM/01 00:00:00")).AddSeconds(-1); if (dateStart > dateS) { dateS = dateStart; } if (dateE > dateEnd) { dateE = dateEnd; } DataRow[] objTmp = rangeSQL.selectedRows(string.Format("recSubTotal_date > '{0}' and recSubTotal_date <= '{1}'", dateS.ToString("yyyy/MM/dd 00:00:00"), dateE.ToString("yyyy/MM/dd 23:59:59"))); string col1 = tmpDate.ToString("yyyy") + "年" + tmpDate.ToString("MM") + "月"; HSSFRow excelRow = (HSSFRow)sheet.CreateRow(rowNum); excelRow.CreateCell(0).SetCellValue(col1); if (objTmp.Length == 0) { excelRow.CreateCell(1).SetCellValue(0); } else { excelRow.CreateCell(1).SetCellValue(objTmp.Sum(row => row.Field("recSubTotal_count"))); intTotal += objTmp.Sum(row => row.Field("recSubTotal_count")); } tmpDate = tmpDate.AddMonths(1); if (tmpDate > dateEnd && tmpDate.ToString("yyyy/MM") == dateEnd.ToString("yyyy/MM")) { tmpDate = dateEnd; } rowNum += 1; } HSSFRow excelRow2 = (HSSFRow)sheet.CreateRow(rowNum); excelRow2.CreateCell(0).SetCellValue("總計"); excelRow2.CreateCell(1).SetCellValue(intTotal); workbook.Write(ms); ms.Flush(); context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8"; context.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=" + HttpUtility.UrlEncode(url_descript.Trim() + "_" + DateTime.Now.ToString("yyyy-MM-dd"), Encoding.UTF8) + ".xls")); context.Response.BinaryWrite(ms.ToArray()); return; } if (type == "day") { headerRow.CreateCell(0).SetCellValue("日期時間"); headerRow.CreateCell(1).SetCellValue("次數"); string rangeString = string.Format("select * from recSubTotal where url_uid = '{0}' and recSubTotal_date >= '{1}' and recSubTotal_date <= '{2}'", url_uid, dateStart.ToString("yyyy/MM/dd HH:mm:ss"), dateEnd.ToString("yyyy/MM/dd HH:mm:ss")); autoBindDataTable rangeSQL = new autoBindDataTable(rangeString); DateTime tmpDate = dateStart; int intTotal = 0; int rowNum = 1; while (tmpDate < dateEnd) { DataRow[] objTmp = rangeSQL.selectedRows(string.Format("recSubTotal_date > '{0}' and recSubTotal_date <= '{1}'", tmpDate.ToString("yyyy/MM/dd"), tmpDate.ToString("yyyy/MM/dd 23:59:59"))); string col1 = tmpDate.ToString("yyyy/MM/dd"); HSSFRow excelRow = (HSSFRow)sheet.CreateRow(rowNum); excelRow.CreateCell(0).SetCellValue(col1); if (objTmp.Length == 0) { excelRow.CreateCell(1).SetCellValue(0); } else { excelRow.CreateCell(1).SetCellValue(int.Parse(objTmp[0]["recSubTotal_count"].ToString())); intTotal += int.Parse(objTmp[0]["recSubTotal_count"].ToString()); } tmpDate = tmpDate.AddDays(1); rowNum += 1; } HSSFRow excelRow2 = (HSSFRow)sheet.CreateRow(rowNum); excelRow2.CreateCell(0).SetCellValue("總計"); excelRow2.CreateCell(1).SetCellValue(intTotal); workbook.Write(ms); ms.Flush(); context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8"; context.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=" + HttpUtility.UrlEncode(url_descript.Trim() + "_" + DateTime.Now.ToString("yyyy-MM-dd"), Encoding.UTF8) + ".xls")); context.Response.BinaryWrite(ms.ToArray()); return; } if (type == "total") { string rangeString = string.Format("select sum(recSubTotal_count) as num from recSubTotal where url_uid = '{0}' and recSubTotal_date >= '{1}' and recSubTotal_date <= '{2}'", url_uid, dateStart.ToString("yyyy/MM/dd HH:mm:ss"), dateEnd.ToString("yyyy/MM/dd HH:mm:ss")); autoBindDataTable rangeSQL = new autoBindDataTable(rangeString); HSSFRow excelRow2 = (HSSFRow)sheet.CreateRow(1); excelRow2.CreateCell(0).SetCellValue("總計"); if (rangeSQL.dataRows.Count == 0) { excelRow2.CreateCell(0).SetCellValue("0"); } else { excelRow2.CreateCell(1).SetCellValue(int.Parse(rangeSQL.dataRows[0]["num"].ToString())); } workbook.Write(ms); ms.Flush(); context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8"; context.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=" + HttpUtility.UrlEncode(url_descript.Trim() + "_" + DateTime.Now.ToString("yyyy-MM-dd"), Encoding.UTF8) + ".xls")); context.Response.BinaryWrite(ms.ToArray()); return; } return; } public bool IsReusable { get { return false; } } }