Beispiel #1
0
        public void ExportStatistic(int formId, int year, Quarter quarter, string type)
        {
            string fileName = null;
            Stream stream   = null;

            if (formId == 0)
            {
                fileName = year + "年" + quarter.GetDescription() + "统计报表.xls";
                stream   = Core.ExportManager.ExportStatistics(CurrentIdentity.FormIds, year, quarter, CurrentIdentity.AreaIds);
            }
            else
            {
                var form = Core.FormManager.GetForm(formId);
                if (form == null)
                {
                    throw new ArgumentException("请选择一个报表");
                }

                stream   = Core.ExportManager.ExportFormStatistic(form, year, quarter, CurrentIdentity.Role == UserRole.Branch ? CurrentIdentity.AreaIds : null);
                fileName = form.Name + "-" + year + "-" + quarter.GetDescription() + ".xls";
            }
            Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(fileName)));
            Response.BinaryWrite(((MemoryStream)stream).GetBuffer());
            stream.Close();
        }
Beispiel #2
0
        public void ExportTrend(int year, Quarter quarter, int[] templateIds)
        {
            var name = year + "年" + quarter.GetDescription() + "国土资源形势";

            if (templateIds == null || templateIds.Length == 0)
            {
                throw new ArgumentException("请选择导出模板");
                //templateIds = Core.TrendTemplateManager.GetList().Select(e => e.ID).ToArray();
            }

            //导出word
            var docStream = Core.ExportManager.ExportTrend(year, quarter, CurrentIdentity.FormIds);

            using (var ms = new MemoryStream())
            {
                using (var zip = new ZipArchive(ms, ZipArchiveMode.Create))
                {
                    var doc = zip.CreateEntry(name + ".docx");
                    using (var sw = doc.Open())
                    {
                        sw.Write(((MemoryStream)docStream).GetBuffer(), 0, (int)docStream.Length);
                    }

                    foreach (var templateId in templateIds)
                    {
                        var trendTemplate = Core.TrendTemplateManager.GetModel(templateId);
                        //导出excel(word的配图)
                        var templatePath = Path.Combine(Request.MapPath("/TrendTemplates/"), trendTemplate.FilePath);
                        var excelStream  = Core.ExportManager.ExportTrendCharts(templatePath, year, quarter, CurrentIdentity.AreaIds);
                        var excelName    = Path.GetFileName(trendTemplate.FilePath);
                        var excel        = zip.CreateEntry(name + "-" + excelName);
                        using (var sw = excel.Open())
                        {
                            sw.Write(((MemoryStream)excelStream).GetBuffer(), 0, (int)excelStream.Length);
                        }
                    }
                }
                Response.ContentType = "application/zip";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(name + ".zip")));
                Response.BinaryWrite(ms.GetBuffer());
            }
        }
Beispiel #3
0
        public Stream ExportTrend(int year, Quarter quarter, int[] formIds)
        {
            var doc = WordHelper.CreateDoc("templates/资源形势模板.docx");

            doc.WriteTitle(year + "年" + quarter.GetDescription() + "国土资源主要指标走势", "2");
            foreach (var form in Core.FormManager.GetForms(formIds))
            {
                var parameter = new NodeValueParameter
                {
                    FormID       = form.ID,
                    Year         = year,
                    Quarter      = quarter,
                    GetNode      = false,
                    GetArea      = false,
                    GetValueType = true,
                    RateType     = RateType.YearOnYear,
                };
                var values = Core.FormManager.GetNodeValues(parameter);
                var nodes  = Core.FormManager.GetRootNodes(form.ID);
                doc.WriteTitle(form.Name, "3", NPOI.XWPF.UserModel.ParagraphAlignment.LEFT);
                var sb = new StringBuilder();
                foreach (var node in nodes)
                {
                    var nodeContent = GenerateContent(node, values);
                    if (nodeContent.Length > 0)
                    {
                        sb.Append(nodeContent.Trim(';').Replace(",,", ",") + "。");
                    }
                }


                doc.WriteContent(sb.ToString());
            }

            return(doc.GetStream());
        }
        /// <summary>
        /// 导出
        /// </summary>
        public List <ExcelCell> WriteDbDataToExcel(Form form, int year, Quarter quarter, int[] areaIds, Template template)
        {
            using (var db = GetDbContext())
            {
                var result = new List <ExcelCell>();
                var areas  = Core.AreaManager.GetAreas();
                foreach (var field in template.Fields)
                {
                    if (field.Parameters.Count > 0)
                    {
                        if (field.HasPrameter(FieldType.Hidden))
                        {
                            field.Value = string.Empty;
                            result.Add(field.Cell);
                            continue;
                        }

                        var firstParameter = field.Parameters[0];
                        switch (firstParameter.Type)
                        {
                        case FieldType.Unit:
                            field.Value = "";
                            break;

                        case FieldType.Form:
                            form        = form ?? Core.FormManager.GetForm(firstParameter.Value);
                            field.Value = form.Name;
                            break;

                        case FieldType.Area:
                            var area = Core.AreaManager.GetArea(firstParameter.Value);
                            field.Value = area == null ? "未知区域" : area.Name;
                            break;

                        case FieldType.Node:
                            var node = Core.FormManager.GetNode(firstParameter.Value);
                            field.Value = node == null ? "未知分类" : node.Name;
                            break;

                        case FieldType.Quarter:
                        case FieldType.Quarters:
                            //如果模板没有指定季度,则按照传参
                            if (firstParameter.Value == 0)
                            {
                                field.Value = quarter.GetDescription();
                            }
                            else
                            {
                                field.Value = ((Quarter)firstParameter.Value).GetDescription();
                            }
                            break;

                        case FieldType.Type:
                        {
                            var valueType = Core.FormManager.GetNodeValueType(firstParameter.Value);
                            field.Value = valueType == null ? "未知类型" : valueType.Name;
                        }
                        break;

                        case FieldType.Year:
                            field.Value = firstParameter.Value == 0 ? year : firstParameter.Value;
                            break;

                        case FieldType.LastYear:
                            field.Value = year - 1;
                            break;

                        case FieldType.Period:
                            field.Value = ((Period)firstParameter.Value).GetDescription();
                            break;

                        case FieldType.Value:
                        case FieldType.RateValue:
                            double           value  = 0;
                            List <NodeValue> values = null;
                            var parameter           = field.GetNodeValueParameter(year, quarter, areaIds);
                            if (parameter == null)
                            {
                                //没有权限导出该区域的数据
                                field.Value = 0;
                                break;
                            }
                            if (firstParameter.Type == FieldType.Value)
                            {
                                values = Core.FormManager.GetNodeValues(parameter);
                                value  = values.Select(e => e.Value).DefaultIfEmpty(0).Sum();
                            }
                            else if (firstParameter.Type == FieldType.RateValue)
                            {
                                //这里写死了是同比
                                parameter.RateType = RateType.YearOnYear;
                                values             = Core.FormManager.GetNodeValues(parameter);
                                var sumVal = values.Select(e => e.Value).DefaultIfEmpty(0).Sum();
                                var comVal = values.Select(e => e.CompareValue).DefaultIfEmpty(0).Sum();

                                value = MathHelper.GetRateValue(sumVal, comVal);
                            }
                            double ratio = 1;
                            if (field.HasPrameter(FieldType.Unit))
                            {
                                var typeId    = values.Count == 0 ? 1 : values.FirstOrDefault().TypeID;
                                var index     = field.Parameters.First(e => e.Type == FieldType.Unit).Value;
                                var valueType = Core.FormManager.GetNodeValueType(typeId);
                                ratio = 1.0 / (index == 0 ? 1 : (int)Math.Pow(valueType.Ratio, index));
                            }
                            if (firstParameter.Type == FieldType.Value)
                            {
                                field.Value = double.Parse((value * ratio).ToString("f4"));
                            }
                            else
                            {
                                field.Value = double.Parse((value * ratio).ToString("f2"));
                            }

                            break;
                        }
                    }
                    result.Add(field.Cell);
                }

                template.WriteData(result);
                return(result);
            }
        }