Exemplo n.º 1
0
        /// <summary>
        ///     Npoi处理Docx
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="uploadImgUrlyDelegate">上传图片事件 入参byte[] 为图片数据 string为图片类型 </param>
        /// <returns></returns>
        public async Task <string> NpoiDocx(Stream stream, Func <byte[], string, string> uploadImgUrlyDelegate = null)
        {
            var myDocx = new XWPFDocument(stream); //打开07(.docx)以上的版本的文档

            var picturesConvert = new PicturesConvert();

            picturesConvert.uploadImgUrlyDelegate += uploadImgUrlyDelegate;

            var paraGraphConvert = new ParaGraphConvert();


            var tableConvert = new TableConvert();
            var picInfoList  = await picturesConvert.PicturesHandleAsync(myDocx);

            var sb = new StringBuilder();

            foreach (var para in myDocx.BodyElements)
            {
                switch (para.ElementType)
                {
                case BodyElementType.PARAGRAPH:
                {
                    var paragraph = (XWPFParagraph)para;
                    sb.Append(paraGraphConvert.ParaGraphHandle(paragraph, picInfoList));

                    break;
                }

                case BodyElementType.TABLE:
                    var paraTable = (XWPFTable)para;
                    sb.Append(tableConvert.TableHandle(paraTable, picInfoList));
                    break;
                }
            }


            return(sb.Replace(" style=''", "").ToString());
        }
Exemplo n.º 2
0
 public NpoiDoc(PicturesConvert picturesConvert, ParaGraphConvert paraGraphConvert, TableConvert tableConvert)
 {
     this.picturesConvert  = picturesConvert;
     this.paraGraphConvert = paraGraphConvert;
     this.tableConvert     = tableConvert;
 }