/// <summary> /// 返回查询表格 /// </summary> /// <returns></returns> public ActionResult getReportCountJson() { T_IPSRPT_REPORT_SW sw = new T_IPSRPT_REPORT_SW(); sw.TopORGNO = Request.Params["TopORGNO"]; sw.DateBegin = Request.Params["DateBegin"]; sw.DateEnd = Request.Params["DateEnd"]; IEnumerable <T_IPSRPT_REPORT_TypeCountModel> typeModel; //类别 var list = T_IPSRPT_REPORTCls.getModelCount(sw, out typeModel); //查询结果 StringBuilder sb = new StringBuilder(); sb.AppendFormat("<table id=\"tb\" cellpadding=\"0\" cellspacing=\"0\">"); sb.AppendFormat("<thead>"); sb.AppendFormat(" <tr>"); sb.AppendFormat(" <th>单位/姓名</th>"); sb.AppendFormat(" <th>总数</th>"); foreach (var v in typeModel) { sb.AppendFormat(" <th>{0}</th>", v.typeName); } sb.AppendFormat(" </tr>"); sb.AppendFormat("</thead>"); sb.AppendFormat("<tbody>"); int i = 0; foreach (var v in list) { i++; if (i % 2 == 0) { sb.AppendFormat("<tr>"); } else { sb.AppendFormat("<tr class='row1'>"); } if (string.IsNullOrEmpty(v.ORGNo)) { sb.AppendFormat("<td class=\"center\" style=\"{1}\">{0}</td>", v.HName, ""); } else { sb.AppendFormat("<td class=\"center\" style=\"{1}\">{0}</td>", v.ORGName, ""); } sb.AppendFormat("<td class=\"center\">{0}</td>", v.ReportCount);//总 var vvList = v.TypeCountModel; foreach (var vv in vvList) { sb.AppendFormat("<td class=\"center\">{0}</td>", vv.typeCount); } sb.AppendFormat("</tr>"); } sb.AppendFormat("</tbody>"); sb.AppendFormat("</table>"); return(Content(JsonConvert.SerializeObject(new Message(true, sb.ToString(), "")), "text/html;charset=UTF-8")); }
public FileResult ReportCountExportExcel() { //ViewBag.ExportExcelUrl = string.Format("/HUReport/ReportCountExportExcel?TIMEBegin={0}&TIMEEnd={1}&BYORGNO={2}&HID={3}&SYSTYPEVALUE={4}" string BYORGNO = Request.Params["BYORGNO"]; string TIMEBegin = Request.Params["TIMEBegin"]; string TIMEEnd = Request.Params["TIMEEnd"]; string HID = Request.Params["HID"]; string SYSTYPEVALUE = Request.Params["SYSTYPEVALUE"]; var vMenu = T_SYS_MENUCls.getModel(new T_SYS_MENU_SW { MENUCODE = "004003", SYSFLAG = ConfigCls.getSystemFlag() }); //vMenu.MENUNAME 页面/菜单名称 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1"); //添加一个sheet sheet1.IsPrintGridlines = true; //打印时显示网格线 sheet1.DisplayGridlines = true; //查看时显示网格线 IRow row = sheet1.CreateRow(0); row.CreateCell(0).SetCellValue(vMenu.MENUNAME); row.GetCell(0).CellStyle = getCellStyleTitle(book); IEnumerable <T_IPSRPT_REPORT_TypeCountModel> typeModel; var list = T_IPSRPT_REPORTCls.getModelCount(new T_IPSRPT_REPORT_SW { TopORGNO = BYORGNO, DateBegin = TIMEBegin, DateEnd = TIMEEnd }, out typeModel); int typeCount = 0;//计算类别有多少列 foreach (var v in typeModel) { typeCount++; } //设置宽度 sheet1.SetColumnWidth(0, 30 * 256); sheet1.SetColumnWidth(1, 20 * 256); for (int i = 0; i < typeCount; i++) { sheet1.SetColumnWidth(i + 2, 20 * 256); } row = sheet1.CreateRow(1); if (PublicCls.OrgIsZhen(BYORGNO) == false) { row.CreateCell(0).SetCellValue("单位"); } else { row.CreateCell(0).SetCellValue("姓名"); } row.CreateCell(1).SetCellValue("总数"); int indexType = 2;//从第二列开始 foreach (var v in typeModel) { row.CreateCell(indexType).SetCellValue(v.typeName); indexType++; } for (int i = 0; i < typeCount + 2; i++) { row.GetCell(i).CellStyle = getCellStyleHead(book); } sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, typeCount + 1)); int rowI = 0; //数据行 foreach (var v in list) //循环获取数据 { row = sheet1.CreateRow(rowI + 2); if (string.IsNullOrEmpty(v.ORGName) == false) { row.CreateCell(0).SetCellValue(v.ORGName); row.GetCell(0).CellStyle = getCellStyleLeft(book); } else { row.CreateCell(0).SetCellValue(v.HName); row.GetCell(0).CellStyle = getCellStyleCenter(book); } row.CreateCell(1).SetCellValue(v.ReportCount); row.GetCell(1).CellStyle = getCellStyleCenter(book); int TypeI = 2;//类型开始列 foreach (var vv in v.TypeCountModel) { row.CreateCell(TypeI).SetCellValue(vv.typeCount); row.GetCell(TypeI).CellStyle = getCellStyleCenter(book); TypeI++; } rowI++; } // 写入到客户端 System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); ms.Seek(0, SeekOrigin.Begin); string fileName = vMenu.MENUNAME + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"; return(File(ms, "application/vnd.ms-excel", fileName)); }