zoukankan      html  css  js  c++  java
  • NPOI word导出自动生成

            public async Task<Stream> GetExportOrderBoxStream(ExportOrderBoxRequest request)
            {
                H_Order order = await OrderService.GetOrder(request.OrderId);
                var config = _bookConfigs.FirstOrDefault(a => a.LogisticsId == order.LogisticsId);
    
                //创建document文档对象对象实例
                Word.XWPFDocument m_Docx = new Word.XWPFDocument();
    
                if (config != null)
                {
                    #region 头部图片
                    // 设置头部货代公司图片
                    string topImgFilePath = config.Image;
                    Stream gfs = null;
                    if (topImgFilePath.StartsWith("http") || topImgFilePath.StartsWith("https"))
                    {
                        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(topImgFilePath);
                        webRequest.Timeout = 5000;
                        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                        gfs = response.GetResponseStream();
                    }
                    else
                    {
                        gfs = new FileStream(topImgFilePath, FileMode.Open, FileAccess.Read);
                    }
                    //创建段落
                    CT_P m_p = m_Docx.Document.body.AddNewP();
                    m_p.AddNewPPr().AddNewJc().val = ST_Jc.center;//段落水平居中
                    Word.XWPFParagraph gp = new Word.XWPFParagraph(m_p, m_Docx);//创建段落
                    Word.XWPFRun gr = gp.CreateRun();//创建run
                                                     //在run中插入图片                                
                    gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg", 6270000, 830000);
                    gfs.Close();
                    #endregion
                }
    
                //创建段落
                var m_p2 = m_Docx.Document.body.AddNewP();
                var ppr = m_p2.AddNewPPr();
                ppr.AddNewJc().val = ST_Jc.center;//段落水平居中
                ppr.AddNewSpacing().before = (ulong)300;
                ppr.AddNewSpacing().after = (ulong)300;
    
                var p = new Word.XWPFParagraph(m_p2, m_Docx);//新建一个段落对象
                Word.XWPFRun r = p.CreateRun();//为段落创建一个可赋值对象
                r.SetText("做箱通知");
                r.FontSize = 20;
                r.IsBold = true;
                r.CharacterSpacing = 200;
    
                #region 表格
                //创建段落
                CT_Tbl m_CTTbl = m_Docx.Document.body.AddNewTbl();
                Word.XWPFTable tab = new Word.XWPFTable(m_CTTbl, m_Docx, 10, 4);//创建一个表格对象
                var newTblPr = m_CTTbl.AddNewTblPr();
                var ct_jc = new CT_Jc();
                ct_jc.val = ST_Jc.center;
                newTblPr.jc = ct_jc;
                var tblw = newTblPr.AddNewTblW();
                tblw.w = "100%";
                tblw.type = ST_TblWidth.dxa;
    
                //设置表格边框为无
                newTblPr.tblBorders = new CT_TblBorders();
                newTblPr.tblBorders.AddNewBottom().val = ST_Border.none;
    
                //创建表格
                //第一行
                var row = tab.GetRow(0);
                var cell = row.GetCell(0);
                cell.SetText("TO:");
                cell = row.GetCell(1);
                cell = row.GetCell(2);
                cell.SetText("FM:");
                cell = row.GetCell(3);
    
    
                //第二行
                var row2 = tab.GetRow(1);
                cell = row2.GetCell(0);
                cell.SetText("");
                cell = row2.GetCell(1);
                cell = row2.GetCell(2);
                cell.SetText("应急电话:");
                cell = row2.GetCell(3);
                //cell.SetText(DateTime.Now.ToString("yyyy-MM-dd"));
    
    
                //第三行
                var row3 = tab.GetRow(2);
                cell = row3.GetCell(0);
                cell.SetText("工作编号:");
                cell = row3.GetCell(1);
                //cell.SetText($"{order.TradeClauses}");
                cell = row3.GetCell(2);
                cell.SetText("通知日期:");
                cell = row3.GetCell(3);
                //cell.SetText(order.GoodsEnglishName);
    
                //第四行
                var row4 = tab.GetRow(3);
                cell = row4.GetCell(0);
                cell.SetText("箱型箱量:");
                cell = row4.GetCell(1);
                cell.SetText($"{order.BoxType.GetDescription() } * {order.BoxQuantity}");
                cell = row4.GetCell(2);
                cell.SetText("品    名:");
                cell = row4.GetCell(3);
                cell.SetText(order.GoodsChineseName);
    
                //第五行
                var row5 = tab.GetRow(4);
                cell = row5.GetCell(0);
                cell.SetText("起  运  港:");
                cell = row5.GetCell(1);
                cell.SetText(order.OriginPort);
                cell = row5.GetCell(2);
                cell.SetText("目  的  地:");
                cell = row5.GetCell(3);
                //cell.SetText(order.DestinationPort);
    
                //第六行
                var row6 = tab.GetRow(5);
                cell = row6.GetCell(0);
                cell.SetText("客户编号:");
                cell = row6.GetCell(1);
                //cell.SetText(order.OriginPort);
                cell = row6.GetCell(2);
                cell.SetText("卸  货  港:");
                cell = row6.GetCell(3);
                cell.SetText(order.DestinationPort);
    
                //第七行
                var row7 = tab.GetRow(6);
                cell = row7.GetCell(0);
                cell.SetText("委托件数:");
                cell = row7.GetCell(1);
                cell.SetText(order.Piece);
                cell = row7.GetCell(2);
                cell.SetText("委托毛重:");
                cell = row7.GetCell(3);
                cell.SetText(order.GrossWeight);
    
                //第八行
                var row8 = tab.GetRow(7);
                cell = row8.GetCell(0);
                cell.SetText("委托体积:");
                cell = row8.GetCell(1);
                cell.SetText(order.Volume);
                cell = row8.GetCell(2);
                cell.SetText("开  航  日:");
                cell = row8.GetCell(3);
                cell.SetText(order.PreplanShippingDate.HasValue ? order.PreplanShippingDate.Value.ToString("yyyy-MM-dd") : string.Empty);
    
                //第九行
                var row9 = tab.GetRow(8);
                cell = row9.GetCell(0);
                cell.SetText("船名航次:");
                cell = row9.GetCell(1);
                cell.SetText($"{order.ShipName}/{order.Voyage}");
                cell = row9.GetCell(2);
                cell.SetText("提  单  号:");
                cell = row9.GetCell(3);
                cell.SetText(order.ExtractOrderNo);
    
                //第十行
                var row10 = tab.GetRow(9);
                cell = row10.GetCell(0);
                cell.SetText("船  公  司:");
                cell = row10.GetCell(1);
                cell.SetText(order.ShippingCompany);
                cell = row10.GetCell(2);
                cell.SetText("进港代码:");
                cell = row10.GetCell(3);
                //cell.SetText(order.TransitPort);
    
                int rowCount = tab.Rows.Count;
                Word.XWPFTableRow tempRow = null;
                for (var i = 0; i < rowCount; i++)
                {
                    tempRow = tab.Rows[i];
                    SetRowStyle(tempRow);
                    var cells = tempRow.GetTableCells();
                    for (var j = 0; j < cells.Count; j++)
                    {
                        if (j % 2 == 0)
                        {
                            SetCellStyle(cells[j], ST_Jc.right);
                        }
                        else
                        {
                            SetCellStyle(cells[j], ST_Jc.left);
                        }
                    }
                }
                #endregion
    
    
                //创建段落
                var m_p3 = m_Docx.Document.body.AddNewP();
                var p3 = new Word.XWPFParagraph(m_p3, m_Docx);//新建一个段落对象
                Word.XWPFRun r3 = p3.CreateRun();//为段落创建一个可赋值对象
                string makeBoxDate = order.MakeBoxTime.HasValue ? order.MakeBoxTime.Value.ToString("yyyy-MM-dd") : string.Empty;
                r3.SetText($"       做箱时间:{makeBoxDate}");
                r3.AddCarriageReturn();
                r3.AppendText($"       做箱地点:{order.MakeBoxAddress}");
                m_p3.AddNewPPr().AddNewSpacing().before = (ulong)300;
                m_p3.AddNewPPr().spacing.line = "350";
    
                NPOIMemoryStream stream = new NPOIMemoryStream();
                stream.Position = 0;
                //保存到流当中
                m_Docx.Write(stream);
                return stream;
            }
  • 相关阅读:
    201504051930_《移动APP框架——MUI——HTML5》
    201503121644_《ios直播协议相关》
    2010502260926_《avolon》
    201502251333_《avolon作用域》
    201502251308_《fekit》
    使用Eclipse下载CRaSH源代码
    网络流量监控工具
    Map的putAll方法验证
    bat脚本中%~dp0含义解释
    CD管理和检索软件比较
  • 原文地址:https://www.cnblogs.com/yiyanwei/p/15237694.html
Copyright © 2011-2022 走看看