using MESClassLibrary.BLL.Check; using MESClassLibrary.Model; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using org.in2bits.MyXls; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; namespace MESWebSite.HttpHandlers { /// /// QualifiedTrendExcelHandler 的摘要说明 /// public class QualifiedTrendExcelHandler : IHttpHandler { HttpRequest Request = null; HttpResponse Response = null; public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; Request = context.Request; Response = context.Response; string method = Request.Params["method"]; switch (method) { case "QueryList": QueryList(); break; case "QueryExcel": QueryExcel(); break; default: break; } } public bool IsReusable { get { return false; } } void QueryList() { } void QueryExcel() { string StartTime = Request.Params["StartTime"]; string EndTime = Request.Params["EndTime"]; string position = Request.Params["position"]; string product = Request.Params["product"]; InspectResultBLL bll = new InspectResultBLL(); List> list = bll.SearchQualifiedTrendForExcel(StartTime, EndTime, position, product); XSSFWorkbook book = new XSSFWorkbook(); ISheet sheet = book.CreateSheet("Sheet1"); for (int i = 0; i < list.Count; i++) { IRow row = sheet.CreateRow(i); for (int k = 0; k < list[i].Count; k++) { row.CreateCell(k).SetCellValue(list[i][k].ToString()); } if (i > 1 && (i % 2 == 1)) { sheet.AddMergedRegion(new CellRangeAddress(i - 2, i - 1, 0, 0)); } } // ... // 写入到客户端 MemoryStream ms = new MemoryStream(); book.Write(ms); Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmssfff"))); Response.BinaryWrite(ms.ToArray()); book = null; ms.Close(); ms.Dispose(); } public string GetModelValue(string FieldName, object obj) { try { Type Ts = obj.GetType(); object o = Ts.GetProperty(FieldName).GetValue(obj, null); string Value = Convert.ToString(o); if (string.IsNullOrEmpty(Value)) return null; return Value; } catch { return null; } } } }