aspose word导出表格

[HttpGet]
        [Route("GetPurchaseItemWord")]
        public IHttpActionResult Get_PurchaseItemWord(string id)
        {
            #region 文件流
            var model = _purchaseRepository.Get(id);
            var purchaseDetail = _purchasedetailRepository.GetDetails(model.id);
            ItemPurchaseViewModel purchaseVM = new ItemPurchaseViewModel();
            purchaseVM = Mapper.Map<im_purchase, ItemPurchaseViewModel>(model);
            purchaseVM.itemList = purchaseDetail;
            var predicateTrail = Predicates.Field<wf_flow_trail>(f => f.business_id, Operator.Eq, model.id);
            var flow_trail = _trailRepository.GetList(predicateTrail).OrderBy(a => a.create_date).ToList();
            IList<ISort> sort = new List<ISort>()
                        {
                            Predicates.Sort<wf_flow_node>(x => x.create_date)
                        };
            var flow_node = _nodeRepository.GetList(Predicates.Field<wf_flow_node>(x => x.flow_id, Operator.Eq, "14e84dba-8546-46b0-989e-0ee1312345ad"), sort).ToList();
            flow_node = flow_node.Where(a => a.id != "20").ToList();
            purchaseVM.flow_node = Mapper.Map<List<wf_flow_node>, List<flow_node>>(flow_node);
            
            try
            {
                string Path = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "Templet";
                Path = Path.Replace("\\bin\\", "\\");
                string templatePath = Path + "\\impurchaseTml.doc";  //自己做好的word
                Document WordDoc = new Document(templatePath);

                DocumentBuilder builder = new DocumentBuilder(WordDoc);
                builder.MoveToBookmark("impurchase");
                if (WordDoc.Range.Bookmarks["impurchase"] != null)
                {
                    var table = builder.StartTable();
//设置文本垂直居中
                    builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
                    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//设置字体大小
                    Aspose.Words.Font f = builder.Font;
                    f.Size = 12;
                    builder.InsertCell();
                    builder.CellFormat.Width = 35; builder.Write("序号");
                    builder.InsertCell();
                    builder.CellFormat.Width = 100; builder.Write("名称");
                    builder.InsertCell();
                    builder.CellFormat.Width = 70; builder.Write("规格型号");
                    builder.InsertCell();
                    builder.CellFormat.Width = 40; builder.Write("数量");
                    builder.InsertCell();
                    builder.CellFormat.Width = 45; builder.Write("单价");
                    builder.InsertCell();
                    builder.CellFormat.Width = 70; builder.Write("总计");
                    builder.InsertCell();
                    builder.CellFormat.Width = 112; builder.Write("备注");
                    builder.EndRow();
                    decimal? money = 0;
                    foreach (var item in purchaseDetail)
                    {
                        builder.InsertCell();
                        builder.CellFormat.Width = 35; builder.Write((purchaseDetail.IndexOf(item)+1).ToString());
                        builder.InsertCell();
                        builder.CellFormat.Width = 100; builder.Write(item.name);
                        builder.InsertCell();
                        builder.CellFormat.Width = 70; builder.Write(item.specification != null ? item.specification : "");
                        builder.InsertCell();
                        builder.CellFormat.Width = 40; builder.Write(item.quantity.ToString());
                        builder.InsertCell();
                        builder.CellFormat.Width = 45; builder.Write(item.price.ToString());
                        builder.InsertCell();
                        builder.CellFormat.Width = 70; builder.Write((item.quantity*item.price).ToString());
                        builder.InsertCell();
                        builder.CellFormat.Width = 112; builder.Write(item.remark != null ? item.remark : "");
                        money += item.price * item.quantity;
                        builder.EndRow();
                    }
                    f.Size = 16;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;//合并单元格开始
                    builder.CellFormat.Width = 35; builder.Write("预计总额");
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 100; 
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; //合并单元格结束
builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 70; builder.Write(money.ToString());
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 40; 
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 45; 
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 70; 
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 112; 
                    builder.EndRow();
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 35; builder.Write("使用说明");
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 100;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 70; builder.Write(model.remark);
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 40;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 45;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 70;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 112;
                    builder.EndRow();

                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 35; builder.Write("申请人");
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 100;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 70; builder.Write(model.create_user);
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 40;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 45; builder.Write("申请时间");
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 70;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None;
                    builder.CellFormat.Width = 112; builder.Write(Convert.ToDateTime(model.create_date).ToString("yyyy年MM月dd日"));
                    builder.EndRow();

                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 35; builder.Write("申请部门");
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 100;
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
                    builder.CellFormat.Width = 70; builder.Write(model.dept_name);
                    builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                    builder.CellFormat.Width = 40;
                    foreach (var nodeitem in purchaseVM.flow_node)
                    {
                        var trail= flow_trail.Where(a => a.next_node_id == nodeitem.next_node).OrderByDescending(a => a.create_date).FirstOrDefault();
                        var filepath = (AppDomain.CurrentDomain.RelativeSearchPath).Replace("\\bin", "\\");
                        if (nodeitem.name== "后勤部门意见")
                        {
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
                            builder.CellFormat.Width = 45; builder.Write("部门意见");
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                            builder.CellFormat.Width = 70;
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None;
                            builder.CellFormat.Width = 112;
                            if (trail != null)
                            {
                                builder.Write(trail.approval != null ? trail.approval : "");
                                if (File.Exists(filepath + trail.sign_path))
                                {
                                    builder.InsertImage(filepath + trail.sign_path, RelativeHorizontalPosition.Margin, 1, RelativeVerticalPosition.Margin, 1, 100, 40, WrapType.Square);//插入图片
                                }
                            }
                            builder.EndRow();
                        }
                        else
                        {
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
                            builder.CellFormat.Width = 35; builder.Write(nodeitem.name);
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                            builder.CellFormat.Width = 100;
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
                            builder.CellFormat.Width = 70;
                            if (trail != null)
                            {
                                builder.Write(trail.approval != null ? trail.approval : "");
                                if (File.Exists(filepath + trail.sign_path))
                                {
                                    builder.InsertImage(filepath + trail.sign_path, RelativeHorizontalPosition.Margin, 1, RelativeVerticalPosition.Margin, 1, 100, 40, WrapType.Square);
                                }
                            }

                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                            builder.CellFormat.Width = 40;
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                            builder.CellFormat.Width = 45;
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                            builder.CellFormat.Width = 70;
                            builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
                            builder.CellFormat.Width = 112;
                            builder.EndRow();
                        }
                    }
                    table.AutoFit(AutoFitBehavior.FixedColumnWidths);
                    builder.EndTable(); table.Alignment = TableAlignment.Center;
//表格居中、固定宽高
                }
                
                string sPath = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "UploadFile";
                sPath = sPath.Replace("\\bin\\", "\\");
                var fileName = "物品采购单" + DateTime.Now.ToString("yyyy-MM-dd") + ".doc";
                fileName = HttpUtility.UrlEncode(fileName);
                string filePath = sPath + "\\" + fileName;
                WordDoc.Save(filePath);
                var stream = FileToStream(filePath);
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                result.Content.Headers.ContentDisposition.FileName = fileName;
                return ResponseMessage(result);

            }
            catch (Exception e)
            {
                //return new HttpResponseMessage(HttpStatusCode.NoContent);
                return Json(new { success = false, message = "导出有误,请联系管理员!" });
            }
            #endregion
        }

 

posted @ 2020-10-27 09:30  艺洁  阅读(360)  评论(0编辑  收藏  举报