using MESClassLibrary.BLL.Stock; using MESClassLibrary.EFModel; using MESClassLibrary.Model; using org.in2bits.MyXls; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; namespace MESWebSite.HttpHandlers { /// /// StockInHandler 的摘要说明 /// public class StockInHandler : 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() { string page = Request.Params["page"]; string pagesize = Request.Params["rows"]; string StartTime = Request.Params["StartTime"]; string EndTime = Request.Params["EndTime"]; string workClass = Request.Params["workClass"]; string color = Request.Params["color"]; string productInfo = Request.Params["productInfo"]; if (string.IsNullOrEmpty(page)) { page = "0"; } if (string.IsNullOrEmpty(pagesize)) { pagesize = "15"; } StockInBLL bll = new StockInBLL(); Response.Write(bll.SearchInfo(page, pagesize, StartTime, EndTime, workClass, color, productInfo)); Response.End(); } void QueryExcel() { string StartTime = Request.Params["StartTime"]; string EndTime = Request.Params["EndTime"]; string workClass = Request.Params["workClass"]; string color = Request.Params["color"]; string productInfo = Request.Params["productInfo"]; StockInBLL bll = new StockInBLL(); List list = bll.SearchInfoAll(StartTime, EndTime, workClass, color, productInfo); XlsDocument xls = new XlsDocument(); xls.FileName = "喷涂入库统计-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; // Sheet标题样式 XF titleXF = xls.NewXF(); // 为xls生成一个XF实例,XF是单元格格式对象 titleXF.HorizontalAlignment = HorizontalAlignments.Centered; // 设定文字居中 titleXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中 titleXF.UseBorder = true; // 使用边框 titleXF.TopLineStyle = 1; // 上边框样式 titleXF.TopLineColor = Colors.Black; // 上边框颜色 titleXF.LeftLineStyle = 1; // 左边框样式 titleXF.LeftLineColor = Colors.Black; // 左边框颜色 titleXF.RightLineStyle = 1; // 右边框样式 titleXF.RightLineColor = Colors.Black; // 右边框颜色 titleXF.Font.FontName = "宋体"; // 字体 titleXF.Font.Bold = true; // 是否加楚 titleXF.Font.Height = 20 * 20; // 字大小(字体大小是以 1/20 point 为单位的) // 列标题样式 XF columnTitleXF = xls.NewXF(); // 为xls生成一个XF实例,XF是单元格格式对象 columnTitleXF.HorizontalAlignment = HorizontalAlignments.Centered; // 设定文字居中 columnTitleXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中 columnTitleXF.UseBorder = true; // 使用边框 columnTitleXF.TopLineStyle = 1; columnTitleXF.TopLineColor = Colors.Black; columnTitleXF.BottomLineStyle = 1; columnTitleXF.BottomLineColor = Colors.Black; columnTitleXF.LeftLineStyle = 1; columnTitleXF.LeftLineColor = Colors.Black; columnTitleXF.RightLineStyle = 1; columnTitleXF.RightLineColor = Colors.Black; columnTitleXF.Pattern = 1; // 单元格填充风格。如果设定为0,则是纯色填充(无色),1代表没有间隙的实色 columnTitleXF.PatternBackgroundColor = Colors.Red; // 填充的底色 columnTitleXF.PatternColor = Colors.Default2F; // 填充背景色 // 数据单元格样式 XF dataXF = xls.NewXF(); // 为xls生成一个XF实例,XF是单元格格式对象 dataXF.HorizontalAlignment = HorizontalAlignments.Centered; // 设定文字居中 dataXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中 dataXF.UseBorder = true; // 使用边框 dataXF.TopLineStyle = 1; dataXF.TopLineColor = Colors.Black; dataXF.BottomLineStyle = 1; dataXF.BottomLineColor = Colors.Black; dataXF.LeftLineStyle = 1; dataXF.LeftLineColor = Colors.Black; dataXF.RightLineStyle = 1; dataXF.RightLineColor = Colors.Black; dataXF.Font.FontName = "宋体"; dataXF.Font.Height = 9 * 20; // 设定字大小(字体大小是以 1/20 point 为单位的) dataXF.UseProtection = false; // 默认的就是受保护的,导出后需要启用编辑才可修改 dataXF.TextWrapRight = true; // 自动换行 Worksheet sheet = xls.Workbook.Worksheets.Add("喷涂入库统计"); //设定列 ArrayList col; List listColumn = new List(); col = new ArrayList() { 25, "条码", "barcode" }; listColumn.Add(col); col = new ArrayList() { 10, "班次", "workClass" }; listColumn.Add(col); col = new ArrayList() { 30, "产品信息", "productInfo" }; listColumn.Add(col); col = new ArrayList() { 20, "入库时间", "createTime" }; listColumn.Add(col); for (int i = 0; i < listColumn.Count; i++) { ArrayList c = listColumn[i]; ColumnInfo colinfo = new ColumnInfo(xls, sheet); int w = (int)c[0]; string s = (string)c[1]; colinfo.ColumnIndexStart = (ushort)i; colinfo.ColumnIndexEnd = (ushort)i; colinfo.Width = (ushort)(w * 256); sheet.AddColumnInfo(colinfo); } org.in2bits.MyXls. //行设置 RowInfo rolinfo = new RowInfo(); rolinfo.RowHeight = 16 * 20; //行高 rolinfo.RowIndexStart = 3; //起始行,从1开始计算 rolinfo.RowIndexEnd = (ushort)(list.Count + 2); //结束行 sheet.AddRowInfo(rolinfo); //设置表头 MergeArea titleArea = new MergeArea(1, 1, 1, listColumn.Count); sheet.AddMergeArea(titleArea); //合并单元格 Cells cells = sheet.Cells; //Sheet标题行,行和列的索引都是从1开始的 for (int i = 0; i < listColumn.Count; i++) cells.Add(1, i + 1, "", titleXF); //合并单元格后仍需要设置每一个单元格,样式才有效 sheet.Rows[1].RowHeight = 40 * 20; //设置标题行行高 cells.Add(1, 1, "喷涂入库统计", titleXF); //标题内容 for (int i = 0; i < listColumn.Count; i++) //设置表头行 { ArrayList c = listColumn[i]; string s = (string)c[1]; cells.Add(2, i + 1, s, columnTitleXF); } sheet.Rows[2].RowHeight = 18 * 20; //设置表头行高 //数据写入cell int k = 0; foreach (var item in list) { for (int j = 0; j < listColumn.Count; j++) { ArrayList c = listColumn[j]; string strColName = (string)c[2]; string s = GetModelValue(strColName, item); cells.Add(3 + k, j + 1, s); } k++; } xls.Send(); Response.End(); } 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; } } } }