public ActionResult Export(GetPlistEntity requestModel, string elements, string limit, string exportType) { if (!ModelState.IsValid) { return ModelState.ToJsonResult(); } OntologyDescriptor ontology; if (!AcDomain.NodeHost.Ontologies.TryGetOntology(requestModel.OntologyCode, out ontology)) { throw new ValidationException("非法的本体码"); } if (string.IsNullOrEmpty(requestModel.CatalogCode)) { throw new ValidationException("没有选中目录"); } CatalogState org; if (!AcDomain.CatalogSet.TryGetCatalog(requestModel.CatalogCode, out org)) { throw new ValidationException("非法的目录码" + requestModel.CatalogCode); } var selectElements = new OrderedElementSet(); if (string.IsNullOrEmpty(elements)) { foreach (var element in ontology.Elements.Values.Where(a => a.Element.IsEnabled == 1)) { selectElements.Add(element); } } else { string[] elementCodes = elements.Split(','); foreach (var elementCode in elementCodes) { ElementDescriptor element; if (!ontology.Elements.TryGetValue(elementCode, out element)) { throw new ValidationException("意外的本体元素码" + elementCode); } else { if (element.Element.IsEnabled != 1) { continue; } selectElements.Add(element); } } } if (selectElements.Count == 0) { throw new ValidationException("selectElements为空"); } if (string.IsNullOrEmpty(exportType)) { exportType = "currentPage"; } exportType = exportType.ToLower(); switch (exportType) { case "allpage": requestModel.PageSize = int.MaxValue; break; case "temp": requestModel.PageSize = 0; break; default: break; } if (!string.IsNullOrEmpty(limit)) { int size; if (int.TryParse(limit, out size)) { requestModel.PageSize = size; } } IDataTuples infoValues = !requestModel.ArchiveId.HasValue ? GetInfoValues(requestModel, selectElements) : GetArchivedInfoValues(requestModel); const string contentType = "application/vnd.ms-excel"; string fileName = org.Name + ontology.Ontology.Name + ".xls"; if (Request.Browser.Type.IndexOf("IE", StringComparison.OrdinalIgnoreCase) > -1) { fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8); } Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName)); Response.Clear(); //Excel功能 var hssfworkbook = new HSSFWorkbook(); //新建一个xls文件 #region Sheet1 数据 ISheet sheet1 = hssfworkbook.CreateSheet(ontology.Ontology.Name); //创建一个sheet int rowIndex = 0; var headRow = sheet1.CreateRow(rowIndex); sheet1.CreateFreezePane(0, 1, 0, 1); rowIndex++; ICellStyle helderStyle = hssfworkbook.CreateCellStyle(); IFont font = hssfworkbook.CreateFont(); font.FontHeightInPoints = 14; helderStyle.SetFont(font); helderStyle.BorderBottom = BorderStyle.Thin; helderStyle.BorderLeft = BorderStyle.Thin; helderStyle.BorderRight = BorderStyle.Thin; helderStyle.BorderTop = BorderStyle.Thin; helderStyle.FillForegroundColor = HSSFColor.LightGreen.Index; helderStyle.FillPattern = FillPattern.SolidForeground; int i = 0; foreach (var element in selectElements) { ICell cell = headRow.CreateCell(i, CellType.String); if (element.IsCodeValue) { cell.SetCellValue(element.Element.Name + "码"); } else { cell.SetCellValue(element.Element.Name); } if (!string.IsNullOrEmpty(element.Element.Description)) { //添加批注 IDrawing draw = sheet1.CreateDrawingPatriarch(); IComment comment = draw.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 1, 2, 4, 8));//里面参数应该是指示批注的位置大小吧 comment.String = new HSSFRichTextString(element.Element.Description);//添加批注内容 comment.Author = AcDomain.NodeHost.Nodes.ThisNode.Name;//添加批注作者 cell.CellComment = comment;//将之前设置的批注给定某个单元格 } cell.CellStyle = helderStyle; if (element.Element.Width > 0) { sheet1.SetColumnWidth(i, element.Element.Width * 256 / 5); } if (element.IsCodeValue) { i++; ICell nameValue = headRow.CreateCell(i, CellType.String); nameValue.SetCellValue(element.Element.Name + "名"); nameValue.CellStyle = helderStyle; if (element.Element.Width > 0) { sheet1.SetColumnWidth(i, element.Element.Width * 256 / 5); } } i++; } foreach (var record in infoValues.Tuples) { var row = sheet1.CreateRow(rowIndex); int j = 0; for (int col = 0; col < infoValues.Columns.Count; col++) { var element = infoValues.Columns[col]; var item = record[col]; ICell cell = row.CreateCell(j, CellType.String); cell.SetCellValue(item.ToString()); if (element.IsCodeValue) { j++; ICell nameCell = row.CreateCell(j, CellType.String); nameCell.SetCellValue(element.TranslateValue(item.ToString())); } j++; } rowIndex++; } #endregion #region orgSheet 目录字典 ISheet orgSheet = hssfworkbook.CreateSheet("目录字典"); //创建一个sheet ICellStyle invalidOrgCodeStyle = hssfworkbook.CreateCellStyle(); invalidOrgCodeStyle.BorderBottom = BorderStyle.Thin; invalidOrgCodeStyle.BorderLeft = BorderStyle.Thin; invalidOrgCodeStyle.BorderRight = BorderStyle.Thin; invalidOrgCodeStyle.BorderTop = BorderStyle.Thin; invalidOrgCodeStyle.FillForegroundColor = HSSFColor.LightOrange.Index; invalidOrgCodeStyle.FillPattern = FillPattern.SolidForeground; ICellStyle invalidParentOrgStyle = hssfworkbook.CreateCellStyle(); invalidParentOrgStyle.BorderBottom = BorderStyle.Thin; invalidParentOrgStyle.BorderLeft = BorderStyle.Thin; invalidParentOrgStyle.BorderRight = BorderStyle.Thin; invalidParentOrgStyle.BorderTop = BorderStyle.Thin; invalidParentOrgStyle.FillForegroundColor = HSSFColor.LightYellow.Index; invalidParentOrgStyle.FillPattern = FillPattern.SolidForeground; rowIndex = 0; headRow = orgSheet.CreateRow(rowIndex); orgSheet.CreateFreezePane(0, 1, 0, 1); rowIndex++; i = 0; var orgCols = new string[] { "父名称", "父编码", "名称", "编码", "创建时间", "最后修改时间", "备注" }; foreach (var colName in orgCols) { orgSheet.SetColumnWidth(i, 20 * 256); ICell cell = headRow.CreateCell(i, CellType.String); cell.SetCellValue(colName); cell.CellStyle = helderStyle; i++; } if (AcSession.IsDeveloper()) { foreach (var item in AcDomain.CatalogSet) { var parentOrg = item.Parent; var row = orgSheet.CreateRow(rowIndex); row.CreateCell(0, CellType.String).SetCellValue(parentOrg.Name); row.CreateCell(1, CellType.String).SetCellValue(parentOrg.Code); row.CreateCell(2, CellType.String).SetCellValue(item.Name); ICell codeCell = row.CreateCell(3, CellType.String); if (item != CatalogState.VirtualRoot) { if (parentOrg == CatalogState.Empty) { codeCell.CellStyle = invalidParentOrgStyle; //添加批注 IDrawing draw = orgSheet.CreateDrawingPatriarch(); IComment comment = draw.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 1, 2, 4, 8));//里面参数应该是指示批注的位置大小吧 comment.String = new HSSFRichTextString("警告:该目录的上级目录不存在。");//添加批注内容 comment.Author = AcDomain.NodeHost.Nodes.ThisNode.Name;//添加批注作者 codeCell.CellComment = comment;//将之前设置的批注给定某个单元格 } else if (parentOrg != CatalogState.VirtualRoot && !item.Code.StartsWith(parentOrg.Code)) { codeCell.CellStyle = invalidOrgCodeStyle; //添加批注 IDrawing draw = orgSheet.CreateDrawingPatriarch(); IComment comment = draw.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 1, 2, 4, 8));//里面参数应该是指示批注的位置大小吧 comment.String = new HSSFRichTextString("警告:该目录的编码没有以上级目录编码为前缀,这是错误的,后续是要改正的。");//添加批注内容 comment.Author = AcDomain.NodeHost.Nodes.ThisNode.Name;//添加批注作者 codeCell.CellComment = comment;//将之前设置的批注给定某个单元格 } } codeCell.SetCellValue(item.Code); string createOn = string.Empty; if (item.CreateOn.HasValue) { createOn = item.CreateOn.ToString(); } string modifiedOn = string.Empty; if (item.ModifiedOn.HasValue) { modifiedOn = item.ModifiedOn.ToString(); } row.CreateCell(4, CellType.String).SetCellValue(createOn); row.CreateCell(5, CellType.String).SetCellValue(modifiedOn); row.CreateCell(6, CellType.String).SetCellValue(item.Description); rowIndex++; } } else { foreach (var myOrg in AcSession.AccountPrivilege.Catalogs) { foreach (var item in AcDomain.CatalogSet) { if (item.Code.StartsWith(myOrg.Code, StringComparison.OrdinalIgnoreCase)) { var row = orgSheet.CreateRow(rowIndex); var parentOrg = item.Parent; row.CreateCell(0, CellType.String).SetCellValue(parentOrg.Name); row.CreateCell(1, CellType.String).SetCellValue(parentOrg.Code); row.CreateCell(2, CellType.String).SetCellValue(item.Name); ICell codeCell = row.CreateCell(3, CellType.String); if (item != CatalogState.VirtualRoot) { if (parentOrg == CatalogState.Empty) { codeCell.CellStyle = invalidParentOrgStyle; //添加批注 IDrawing draw = orgSheet.CreateDrawingPatriarch(); IComment comment = draw.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 1, 2, 4, 8));//里面参数应该是指示批注的位置大小吧 comment.String = new HSSFRichTextString("警告:该目录的上级目录不存在。");//添加批注内容 comment.Author = AcDomain.NodeHost.Nodes.ThisNode.Name;//添加批注作者 codeCell.CellComment = comment;//将之前设置的批注给定某个单元格 } else if (parentOrg != CatalogState.VirtualRoot && !item.Code.StartsWith(parentOrg.Code, StringComparison.OrdinalIgnoreCase)) { codeCell.CellStyle = invalidOrgCodeStyle; //添加批注 IDrawing draw = orgSheet.CreateDrawingPatriarch(); IComment comment = draw.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 1, 2, 4, 8));//里面参数应该是指示批注的位置大小吧 comment.String = new HSSFRichTextString("警告:该目录的编码没有以上级目录编码为前缀,这是错误的,后续是要改正的。");//添加批注内容 comment.Author = AcDomain.NodeHost.Nodes.ThisNode.Name;//添加批注作者 codeCell.CellComment = comment;//将之前设置的批注给定某个单元格 } } codeCell.SetCellValue(item.Code); string createOn = string.Empty; if (item.CreateOn.HasValue) { createOn = item.CreateOn.ToString(); } string modifiedOn = string.Empty; if (item.ModifiedOn.HasValue) { modifiedOn = item.ModifiedOn.ToString(); } row.CreateCell(4, CellType.String).SetCellValue(createOn); row.CreateCell(5, CellType.String).SetCellValue(modifiedOn); row.CreateCell(6, CellType.String).SetCellValue(item.Description); rowIndex++; } } } } #endregion #region infoDicSheet 信息字典 ISheet infoDicSheet = hssfworkbook.CreateSheet("信息字典"); //创建一个sheet rowIndex = 0; headRow = infoDicSheet.CreateRow(rowIndex); infoDicSheet.CreateFreezePane(0, 1, 0, 1); rowIndex++; i = 0; var dicCols = new string[] { "字典名", "字典码", "字典项名", "字典项码", "等级", "创建时间", "最后修改时间", "备注" }; foreach (var colName in dicCols) { infoDicSheet.SetColumnWidth(i, 20 * 256); ICell cell = headRow.CreateCell(i, CellType.String); cell.SetCellValue(colName); cell.CellStyle = helderStyle; i++; } foreach (var infoDic in AcDomain.NodeHost.InfoDics) { foreach (var infoDicItem in AcDomain.NodeHost.InfoDics.GetInfoDicItems(infoDic)) { var row = infoDicSheet.CreateRow(rowIndex); row.CreateCell(0, CellType.String).SetCellValue(infoDic.Name); row.CreateCell(1, CellType.String).SetCellValue(infoDic.Code); row.CreateCell(2, CellType.String).SetCellValue(infoDicItem.Name); row.CreateCell(3, CellType.String).SetCellValue(infoDicItem.Code); row.CreateCell(4, CellType.String).SetCellValue(infoDicItem.Level); string createOn = string.Empty; if (infoDicItem.CreateOn.HasValue) { createOn = infoDicItem.CreateOn.ToString(); } string modifiedOn = string.Empty; if (infoDicItem.ModifiedOn.HasValue) { modifiedOn = infoDicItem.ModifiedOn.ToString(); } row.CreateCell(5, CellType.String).SetCellValue(createOn); row.CreateCell(6, CellType.String).SetCellValue(modifiedOn); row.CreateCell(7, CellType.String).SetCellValue(infoDicItem.Description); rowIndex++; } } #endregion var filestream = new MemoryStream(); //内存文件流(应该可以写成普通的文件流) hssfworkbook.Write(filestream); //把文件读到内存流里面 return new FileContentResult(filestream.GetBuffer(), contentType); }
private IDataTuples GetArchivedInfoValues(GetPlistEntity requestModel) { ArchiveState archive; if (!requestModel.ArchiveId.HasValue || !AcDomain.NodeHost.Ontologies.TryGetArchive(requestModel.ArchiveId.Value, out archive)) { throw new ValidationException("意外的归档Id"); } OntologyDescriptor ontology = archive.Ontology; requestModel.Includedescendants = requestModel.Includedescendants ?? false; IDataTuples infoValues = null; var selectElements = new OrderedElementSet(); foreach (var element in ontology.Elements.Values.Where(a => a.Element.IsEnabled == 1)) { if (element.Element != null && element.Element.IsGridColumn) { selectElements.Add(element); } } if (string.IsNullOrEmpty(requestModel.CatalogCode) && !AcSession.IsDeveloper()) { throw new ValidationException("对不起,您没有查看全部数据的权限"); } else { if (ontology.Ontology.IsCataloguedEntity && !string.IsNullOrEmpty(requestModel.CatalogCode)) { if (requestModel.Includedescendants.Value) { requestModel.Filters.Add(FilterData.Like("ZZJGM", requestModel.CatalogCode + "%")); } else { requestModel.Filters.Add(FilterData.EQ("ZZJGM", requestModel.CatalogCode)); } } infoValues = ontology.EntityProvider.GetPlist( archive, selectElements, requestModel.Filters, requestModel); } return infoValues; }
private IDataTuples GetInfoValues( GetPlistEntity requestModel, OrderedElementSet selectElements) { OntologyDescriptor ontology; if (!AcDomain.NodeHost.Ontologies.TryGetOntology(requestModel.OntologyCode, out ontology)) { throw new ValidationException("非法的本体码"); } requestModel.Includedescendants = requestModel.Includedescendants ?? false; IDataTuples infoValues = null; if (string.IsNullOrEmpty(requestModel.CatalogCode) && !AcSession.IsDeveloper()) { throw new ValidationException("对不起,您没有查看全部数据的权限"); } else { if (ontology.Ontology.IsCataloguedEntity && !string.IsNullOrEmpty(requestModel.CatalogCode)) { if (requestModel.Includedescendants.Value) { requestModel.Filters.Add(FilterData.Like("ZZJGM", requestModel.CatalogCode + "%")); } else { requestModel.Filters.Add(FilterData.EQ("ZZJGM", requestModel.CatalogCode)); } } infoValues = ontology.EntityProvider.GetPlist( ontology, selectElements, requestModel.Filters, requestModel); } return infoValues; }
public ActionResult GetPlistEntity(GetPlistEntity requestModel) { if (!ModelState.IsValid) { return ModelState.ToJsonResult(); } OntologyDescriptor ontology; if (!AcDomain.NodeHost.Ontologies.TryGetOntology(requestModel.OntologyCode, out ontology)) { throw new ValidationException("非法的本体码"); } if (ontology.Ontology.IsCataloguedEntity) { if (string.IsNullOrEmpty(requestModel.CatalogCode)) { throw new ValidationException("没有选中目录"); } CatalogState org; if (!AcDomain.CatalogSet.TryGetCatalog(requestModel.CatalogCode, out org)) { throw new ValidationException("非法的目录码" + requestModel.CatalogCode); } } var selectElements = new OrderedElementSet(); foreach (var element in ontology.Elements.Values.Where(a => a.Element.IsEnabled == 1)) { if (element.Element != null && element.Element.IsGridColumn) { selectElements.Add(element); } } if (selectElements.Count == 0) { throw new ValidationException("selectElements为空"); } IDataTuples infoValues = !requestModel.ArchiveId.HasValue ? GetInfoValues(requestModel, selectElements) : GetArchivedInfoValues(requestModel); var data = new List<Dictionary<string, string>>(); foreach (var row in infoValues.Tuples) { var dic = new Dictionary<string, string>(); for (int i = 0; i < infoValues.Columns.Count; i++) { string value; if (requestModel.Translate.HasValue && requestModel.Translate.Value) { value = infoValues.Columns[i].TranslateValue(row[i].ToString()); } else { value = row[i].ToString(); } dic.Add(infoValues.Columns[i].Element.Code, value); } data.Add(dic); } Debug.Assert(requestModel.Total != null, "requestModel.Total != null"); return this.JsonResult(new MiniGrid { total = requestModel.Total.Value, data = data }); }