예제 #1
0
        internal void InitLvl()
        {
            List <CT_Lvl> list = new List <CT_Lvl>();

            for (int i = 0; i < 9; i++)
            {
                CT_Lvl lvl = new CT_Lvl();
                lvl.start.val   = "1";
                lvl.tentative   = i == 0? ST_OnOff.on : ST_OnOff.off;
                lvl.ilvl        = i.ToString();
                lvl.lvlJc.val   = ST_Jc.left;
                lvl.numFmt.val  = ST_NumberFormat.bullet;
                lvl.lvlText.val = lvlText[i % 3].ToString();
                CT_Ind ind = lvl.pPr.AddNewInd();
                ind.left    = (420 * (i + 1)).ToString();
                ind.hanging = 420;
                CT_Fonts fonts = lvl.rPr.AddNewRFonts();
                fonts.ascii = "Wingdings";
                fonts.hAnsi = "Wingdings";
                fonts.hint  = ST_Hint.@default;

                list.Add(lvl);
            }
            ctAbstractNum.lvl = list;
        }
예제 #2
0
        /**
         * Specifies the fonts which shall be used to display the text contents of
         * this run. Specifies a font which shall be used to format all characters
         * in the ASCII range (0 - 127) within the parent run
         *
         * @param fontFamily
         */
        public void SetFontFamily(String fontFamily)
        {
            CT_RPr   pr    = run.IsSetRPr() ? run.rPr : run.AddNewRPr();
            CT_Fonts fonts = pr.IsSetRFonts() ? pr.rFonts : pr.AddNewRFonts();

            fonts.ascii = (fontFamily);
        }
예제 #3
0
        /**
         * Sets the default font on ctStyles DocDefaults parameter
         * @param fonts
         */
        public void SetDefaultFonts(CT_Fonts fonts)
        {
            CT_DocDefaults docDefaults = null;
            CT_RPr         RunProps    = null;

            // Just making sure we use the members that have already been defined
            if (ctStyles.IsSetDocDefaults())
            {
                docDefaults = ctStyles.docDefaults;
                if (docDefaults.IsSetRPrDefault())
                {
                    CT_RPrDefault RPrDefault = docDefaults.rPrDefault;
                    if (RPrDefault.IsSetRPr())
                    {
                        RunProps = RPrDefault.rPr;
                    }
                }
            }

            if (docDefaults == null)
            {
                docDefaults = ctStyles.AddNewDocDefaults();
            }
            if (RunProps == null)
            {
                RunProps = docDefaults.AddNewRPrDefault().AddNewRPr();
            }

            RunProps.rFonts = (fonts);
        }
예제 #4
0
파일: XWPFRun.cs 프로젝트: Yvees/npoi
        /**
         * Gets the font family for the specified font char range.
         * If fcr is null, the font char range "ascii" is used
         *
         * @param fcr the font char range, defaults to "ansi"
         * @return  a string representing the font famil
         */
        public String GetFontFamily(FontCharRange fcr)
        {
            CT_RPr pr = run.rPr;

            if (pr == null || !pr.IsSetRFonts())
            {
                return(null);
            }

            CT_Fonts fonts = pr.rFonts;

            switch (fcr == FontCharRange.None ? FontCharRange.Ascii : fcr)
            {
            default:
            case FontCharRange.Ascii:
                return(fonts.ascii);

            case FontCharRange.CS:
                return(fonts.cs);

            case FontCharRange.EastAsia:
                return(fonts.eastAsia);

            case FontCharRange.HAnsi:
                return(fonts.hAnsi);
            }
        }
예제 #5
0
        public TOC(CT_SdtBlock block)
        {
            this.block = block;
            CT_SdtPr         sdtPr = block.AddNewSdtPr();
            CT_DecimalNumber id    = sdtPr.AddNewId();

            id.val = ("4844945");
            sdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = ("Table of contents");
            CT_SdtEndPr sdtEndPr = block.AddNewSdtEndPr();
            CT_RPr      rPr      = sdtEndPr.AddNewRPr();
            CT_Fonts    fonts    = rPr.AddNewRFonts();

            fonts.asciiTheme    = (ST_Theme.minorHAnsi);
            fonts.eastAsiaTheme = (ST_Theme.minorHAnsi);
            fonts.hAnsiTheme    = (ST_Theme.minorHAnsi);
            fonts.cstheme       = (ST_Theme.minorBidi);
            rPr.AddNewB().val          = false;
            rPr.AddNewBCs().val        = false;
            rPr.AddNewColor().val      = ("auto");
            rPr.AddNewSz().val         = 24;
            rPr.AddNewSzCs().val       = 24;
            CT_SdtContentBlock content = block.AddNewSdtContent();
            CT_P p = content.AddNewP();

            byte[] b = Encoding.Unicode.GetBytes("00EF7E24");
            p.rsidR        = b;
            p.rsidRDefault = b;
            p.AddNewPPr().AddNewPStyle().val = ("TOCHeading");
            p.AddNewR().AddNewT().Value      = ("Table of Contents");
        }
예제 #6
0
        public TOC(CT_SdtBlock block)
        {
            this.block = block;
            CT_SdtPr ctSdtPr = block.AddNewSdtPr();

            ctSdtPr.AddNewId().val = "4844945";
            ctSdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = "Table of contents";
            CT_RPr   ctRpr   = block.AddNewSdtEndPr().AddNewRPr();
            CT_Fonts ctFonts = ctRpr.AddNewRFonts();

            ctFonts.asciiTheme    = ST_Theme.minorHAnsi;
            ctFonts.eastAsiaTheme = ST_Theme.minorHAnsi;
            ctFonts.hAnsiTheme    = ST_Theme.minorHAnsi;
            ctFonts.cstheme       = ST_Theme.minorBidi;
            ctRpr.AddNewB().val     = ST_OnOff.off;
            ctRpr.AddNewBCs().val   = ST_OnOff.off;
            ctRpr.AddNewColor().val = "auto";
            ctRpr.AddNewSz().val    = 24UL;
            ctRpr.AddNewSzCs().val  = 24UL;
            CT_P ctP = block.AddNewSdtContent().AddNewP();

            byte[] bytes = Encoding.Unicode.GetBytes("00EF7E24");
            ctP.rsidR        = bytes;
            ctP.rsidRDefault = bytes;
            ctP.AddNewPPr().AddNewPStyle().val = "TOCHeading";
            ctP.AddNewR().AddNewT().Value      = "Table of Contents";
        }
예제 #7
0
        /**
         * Sets the default font on ctStyles DocDefaults parameter
         * TODO Replace this with specific Setters for each type, possibly
         *  on XWPFDefaultRunStyle
         */
        public void SetDefaultFonts(CT_Fonts fonts)
        {
            EnsureDocDefaults();

            CT_RPr RunProps = defaultRunStyle.GetRPr();

            RunProps.rFonts = (/*setter*/ fonts);
        }
예제 #8
0
        public void TestLanguages()
        {
            XWPFDocument docOut = new XWPFDocument();
            XWPFStyles   styles = docOut.CreateStyles();

            styles.SetEastAsia("Chinese");

            styles.SetSpellingLanguage("English");

            CT_Fonts def = new CT_Fonts();

            styles.SetDefaultFonts(def);
        }
예제 #9
0
        public TOC(CT_SdtBlock block)
        {
            this.block = block;
            CT_SdtPr         sdtPr = block.AddNewSdtPr();
            CT_DecimalNumber id    = sdtPr.AddNewId();

            id.val = ("4844945");
            sdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = ("Table of Contents");
            CT_SdtEndPr sdtEndPr = block.AddNewSdtEndPr();
            CT_RPr      rPr      = sdtEndPr.AddNewRPr();
            CT_Fonts    fonts    = rPr.AddNewRFonts();

            fonts.asciiTheme    = (ST_Theme.minorHAnsi);
            fonts.eastAsiaTheme = (ST_Theme.minorHAnsi);
            fonts.hAnsiTheme    = (ST_Theme.minorHAnsi);
            fonts.cstheme       = (ST_Theme.minorBidi);
            CT_SdtContentBlock content = block.AddNewSdtContent();
            CT_P p = content.AddNewP();

            byte[] b = Encoding.Unicode.GetBytes("00EF7E24");
            p.rsidR        = b;
            p.rsidRDefault = b;
            CT_PPr pPr = p.AddNewPPr();

            pPr.AddNewPStyle().val = ("TOCHeading");
            pPr.AddNewJc().val     = ST_Jc.center;
            CT_R run = p.AddNewR();

            run.AddNewRPr().AddNewSz().val = 48;
            run.AddNewT().Value            = ("Table of Contents");
            run.AddNewBr().type            = ST_BrType.textWrapping; // line break

            // TOC Field
            p   = content.AddNewP();
            pPr = p.AddNewPPr();
            pPr.AddNewPStyle().val = "TOC1";
            pPr.AddNewRPr().AddNewNoProof();

            run = p.AddNewR();
            run.AddNewFldChar().fldCharType = ST_FldCharType.begin;

            run = p.AddNewR();
            CT_Text text = run.AddNewInstrText();

            text.space = "preserve";
            text.Value = (" TOC \\h \\z ");

            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
        }
예제 #10
0
파일: XWPFRun.cs 프로젝트: Yvees/npoi
        /**
         * Specifies the fonts which shall be used to display the text contents of
         * this run. The default handling for fcr == null is to overwrite the
         * ascii font char range with the given font family and also set all not
         * specified font ranges
         *
         * @param fontFamily
         * @param fcr FontCharRange or null for default handling
         */
        public void SetFontFamily(String fontFamily, FontCharRange fcr)
        {
            CT_RPr   pr    = run.IsSetRPr() ? run.rPr : run.AddNewRPr();
            CT_Fonts fonts = pr.IsSetRFonts() ? pr.rFonts : pr.AddNewRFonts();

            if (fcr == FontCharRange.None)
            {
                fonts.ascii = (fontFamily);
                if (!fonts.IsSetHAnsi())
                {
                    fonts.hAnsi = (fontFamily);
                }
                if (!fonts.IsSetCs())
                {
                    fonts.cs = (fontFamily);
                }
                if (!fonts.IsSetEastAsia())
                {
                    fonts.eastAsia = (fontFamily);
                }
            }
            else
            {
                switch (fcr)
                {
                case FontCharRange.Ascii:
                    fonts.ascii = (fontFamily);
                    break;

                case FontCharRange.CS:
                    fonts.cs = (fontFamily);
                    break;

                case FontCharRange.EastAsia:
                    fonts.eastAsia = (fontFamily);
                    break;

                case FontCharRange.HAnsi:
                    fonts.hAnsi = (fontFamily);
                    break;
                }
            }
        }
예제 #11
0
 public void SetDefaultFonts(CT_Fonts fonts)
 {
   CT_DocDefaults ctDocDefaults = (CT_DocDefaults) null;
   CT_RPr ctRpr = (CT_RPr) null;
   if (this.ctStyles.IsSetDocDefaults())
   {
     ctDocDefaults = this.ctStyles.docDefaults;
     if (ctDocDefaults.IsSetRPrDefault())
     {
       CT_RPrDefault rPrDefault = ctDocDefaults.rPrDefault;
       if (rPrDefault.IsSetRPr())
         ctRpr = rPrDefault.rPr;
     }
   }
   if (ctDocDefaults == null)
     ctDocDefaults = this.ctStyles.AddNewDocDefaults();
   if (ctRpr == null)
     ctRpr = ctDocDefaults.AddNewRPrDefault().AddNewRPr();
   ctRpr.rFonts = fonts;
 }
예제 #12
0
        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="run"></param>
        /// <param name="nameType"></param>
        public static void SetFontName(this XWPFRun run, FontNameType nameType)
        {
            string   fontName = GetFontName(nameType);
            CT_R     r        = run.GetCTR();
            CT_RPr   rpr      = r.IsSetRPr() ? r.rPr : r.AddNewRPr();
            CT_Fonts fonts    = rpr.IsSetRFonts() ? rpr.rFonts : rpr.AddNewRFonts();

            fonts.ascii = fontName;
            if (string.IsNullOrEmpty(fonts.eastAsia))
            {
                fonts.eastAsia = fontName;
            }
            if (string.IsNullOrEmpty(fonts.cs))
            {
                fonts.cs = fontName;
            }
            if (string.IsNullOrEmpty(fonts.hAnsi))
            {
                fonts.hAnsi = fontName;
            }
        }
예제 #13
0
        internal void InitLvl()
        {
            List <CT_Lvl> ctLvlList = new List <CT_Lvl>();

            for (int index = 0; index < 9; ++index)
            {
                CT_Lvl ctLvl = new CT_Lvl();
                ctLvl.start.val   = "1";
                ctLvl.tentative   = index == 0 ? ST_OnOff.Value0 : ST_OnOff.Value1;
                ctLvl.ilvl        = index.ToString();
                ctLvl.lvlJc.val   = ST_Jc.left;
                ctLvl.numFmt.val  = ST_NumberFormat.bullet;
                ctLvl.lvlText.val = this.lvlText[index % 3].ToString();
                CT_Ind ctInd = ctLvl.pPr.AddNewInd();
                ctInd.left    = (420 * (index + 1)).ToString();
                ctInd.hanging = 420UL;
                CT_Fonts ctFonts = ctLvl.rPr.AddNewRFonts();
                ctFonts.ascii = "Wingdings";
                ctFonts.hAnsi = "Wingdings";
                ctFonts.hint  = ST_Hint.@default;
                ctLvlList.Add(ctLvl);
            }
            this.ctAbstractNum.lvl = ctLvlList.ToArray();
        }
예제 #14
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            //图片位置
            String       m_PicPath = @"C:\ttt.jpeg";
            FileStream   gfs       = null;
            MemoryStream ms        = new MemoryStream();
            XWPFDocument m_Docx    = new XWPFDocument();

            //页面设置
            //A4:W=11906,h=16838
            //CT_SectPr m_SectPr = m_Docx.Document.body.AddNewSectPr();
            m_Docx.Document.body.sectPr = new CT_SectPr();
            CT_SectPr m_SectPr = m_Docx.Document.body.sectPr;

            //页面设置A4纵向
            m_SectPr.pgSz.h = (ulong)16838;
            m_SectPr.pgSz.w = (ulong)11906;
            XWPFParagraph gp = m_Docx.CreateParagraph();
            // gp.GetCTPPr().AddNewJc().val = ST_Jc.center; //水平居中
            XWPFRun gr = gp.CreateRun();

            gr.GetCTR().AddNewRPr().AddNewRFonts().ascii    = "黑体";
            gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑体";
            gr.GetCTR().AddNewRPr().AddNewRFonts().hint     = ST_Hint.eastAsia;
            gr.GetCTR().AddNewRPr().AddNewSz().val          = (ulong)44; //2号字体
            gr.GetCTR().AddNewRPr().AddNewSzCs().val        = (ulong)44;
            gr.GetCTR().AddNewRPr().AddNewB().val           = true;      //加粗
            gr.GetCTR().AddNewRPr().AddNewColor().val       = "red";     //字体颜色
            gr.SetText("NPOI创建Word2007Docx");
            gp = m_Docx.CreateParagraph();
            //gp.GetCTPPr().AddNewJc().val = ST_Jc.both;
            // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符
            gp.IndentationFirstLine = 15;
            gr = gp.CreateRun();
            CT_RPr   rpr    = gr.GetCTR().AddNewRPr();
            CT_Fonts rfonts = rpr.AddNewRFonts();

            rfonts.ascii    = "宋体";
            rfonts.eastAsia = "宋体";
            rpr.AddNewSz().val   = (ulong)21;//5号字体
            rpr.AddNewSzCs().val = (ulong)21;
            gr.SetText("NPOI,顾名思义,就是POI的.NET版本。那POI又是什么呢?POI是一套用Java写成的库,能够帮助开 发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等 。目前POI的稳定版本中支持Excel文件格式(xls和xlsx),其他的都属于不稳定版本(放在poi的scrachpad目录 中)。");
            //创建表
            XWPFTable table     = m_Docx.CreateTable(1, 4);              //创建一行4列表
            CT_Tbl    m_CTTbl   = m_Docx.Document.body.GetTblArray()[0]; //获得文档第一张表
            CT_TblPr  m_CTTblPr = m_CTTbl.AddNewTblPr();

            m_CTTblPr.AddNewTblW().w    = "2000";              //表宽
            m_CTTblPr.AddNewTblW().type = ST_TblWidth.dxa;
            m_CTTblPr.tblpPr               = new CT_TblPPr();  //表定位
            m_CTTblPr.tblpPr.tblpX         = "4003";           //表左上角坐标
            m_CTTblPr.tblpPr.tblpY         = "365";
            m_CTTblPr.tblpPr.tblpXSpec     = ST_XAlign.center; //若不为“Null”,则优先tblpX,即表由tblpXSpec定位
            m_CTTblPr.tblpPr.tblpYSpec     = ST_YAlign.center; //若不为“Null”,则优先tblpY,即表由tblpYSpec定位
            m_CTTblPr.tblpPr.leftFromText  = (ulong)180;
            m_CTTblPr.tblpPr.rightFromText = (ulong)180;
            m_CTTblPr.tblpPr.vertAnchor    = ST_VAnchor.text;
            m_CTTblPr.tblpPr.horzAnchor    = ST_HAnchor.page;


            //表1行4列充值:a,b,c,d
            table.GetRow(0).GetCell(0).SetText("a");
            table.GetRow(0).GetCell(1).SetText("b");
            table.GetRow(0).GetCell(2).SetText("c");
            table.GetRow(0).GetCell(3).SetText("d");
            CT_Row       m_NewRow = new CT_Row();//创建1行
            XWPFTableRow m_Row    = new XWPFTableRow(m_NewRow, table);

            table.AddRow(m_Row);                     //必须要!!!
            XWPFTableCell cell = m_Row.CreateCell(); //创建单元格,也创建了一个CT_P
            CT_Tc         cttc = cell.GetCTTc();
            CT_TcPr       ctPr = cttc.AddNewTcPr();

            //ctPr.gridSpan.val = "3";//合并3列
            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
            cttc.GetPList()[0].AddNewR().AddNewT().Value  = "666";
            cell = m_Row.CreateCell();//创建单元格,也创建了一个CT_P
            cell.GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
            cell.GetCTTc().GetPList()[0].AddNewR().AddNewT().Value  = "e";
            //合并3列,合并2行
            //1行
            m_NewRow = new CT_Row();
            m_Row    = new XWPFTableRow(m_NewRow, table);
            table.AddRow(m_Row);
            cell = m_Row.CreateCell(); //第1单元格
            cell.SetText("f");
            cell = m_Row.CreateCell(); //从第2单元格开始合并
            cttc = cell.GetCTTc();
            ctPr = cttc.AddNewTcPr();
            // ctPr.gridSpan.val = "3";//合并3列
            ctPr.AddNewVMerge().val = ST_Merge.restart;     //开始合并行
            ctPr.AddNewVAlign().val = ST_VerticalJc.center; //垂直居中
            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
            cttc.GetPList()[0].AddNewR().AddNewT().Value  = "777";
            //2行
            m_NewRow = new CT_Row();
            m_Row    = new XWPFTableRow(m_NewRow, table);
            table.AddRow(m_Row);
            cell = m_Row.CreateCell(); //第1单元格
            cell.SetText("g");
            cell = m_Row.CreateCell(); //第2单元格
            cttc = cell.GetCTTc();
            ctPr = cttc.AddNewTcPr();
            // ctPr.gridSpan.val = "3";//合并3列
            ctPr.AddNewVMerge().val = ST_Merge.@continue;//继续合并行


            //表插入图片
            m_NewRow = new CT_Row();
            m_Row    = new XWPFTableRow(m_NewRow, table);
            table.AddRow(m_Row);
            cell = m_Row.CreateCell();//第1单元格
            //inline方式插入图片
            //gp = table.GetRow(table.Rows.Count - 1).GetCell(0).GetParagraph(table.GetRow(table.Rows.Count - 1).GetCell(0).GetCTTc().GetPList()[0]);//获得指定表单元格的段
            gp  = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr  = gp.CreateRun();                                              //创建run
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);   //读取图片文件
            gr.AddPicture(gfs, (int)PictureType.PNG, "1.jpg", 500000, 500000); //插入图片
            gfs.Close();
            //Anchor方式插入图片
            CT_Anchor an = new CT_Anchor();

            an.distB          = (uint)(0);
            an.distL          = 114300u;
            an.distR          = 114300U;
            an.distT          = 0U;
            an.relativeHeight = 251658240u;
            an.behindDoc      = false; //"0"
            an.locked         = false; //"0"
            an.layoutInCell   = true;  //"1"
            an.allowOverlap   = true;  //"1"


            NPOI.OpenXmlFormats.Dml.CT_Point2D simplePos = new NPOI.OpenXmlFormats.Dml.CT_Point2D();
            simplePos.x = (long)0;
            simplePos.y = (long)0;
            CT_EffectExtent effectExtent = new CT_EffectExtent();

            effectExtent.b = 0L;
            effectExtent.l = 0L;
            effectExtent.r = 0L;
            effectExtent.t = 0L;
            //wrapSquare(四周)
            cell = m_Row.CreateCell(); //第2单元格
            gp   = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr   = gp.CreateRun();     //创建run
            CT_WrapSquare wrapSquare = new CT_WrapSquare();

            wrapSquare.wrapText = ST_WrapText.bothSides;
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件
            // gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();
            //wrapTight(紧密)
            cell = m_Row.CreateCell(); //第3单元格
            gp   = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr   = gp.CreateRun();     //创建run
            CT_WrapTight wrapTight = new CT_WrapTight();

            wrapTight.wrapText            = ST_WrapText.bothSides;
            wrapTight.wrapPolygon         = new CT_WrapPath();
            wrapTight.wrapPolygon.edited  = false;
            wrapTight.wrapPolygon.start   = new CT_Point2D();
            wrapTight.wrapPolygon.start.x = 0;
            wrapTight.wrapPolygon.start.y = 0;
            CT_Point2D lineTo = new CT_Point2D();

            wrapTight.wrapPolygon.lineTo = new List <CT_Point2D>();
            lineTo   = new CT_Point2D();
            lineTo.x = 0;
            lineTo.y = 1343;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 1343;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 0;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            lineTo.x = 0;
            lineTo.y = 0;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件
            //gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapTight, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();
            //wrapThrough(穿越)
            cell = m_Row.CreateCell();                                        //第4单元格
            gp   = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr   = gp.CreateRun();                                            //创建run
            gfs  = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read); //读取图片文件
            CT_WrapThrough wrapThrough = new CT_WrapThrough();

            wrapThrough.wrapText            = ST_WrapText.bothSides;
            wrapThrough.wrapPolygon         = new CT_WrapPath();
            wrapThrough.wrapPolygon.edited  = false;
            wrapThrough.wrapPolygon.start   = new CT_Point2D();
            wrapThrough.wrapPolygon.start.x = 0;
            wrapThrough.wrapPolygon.start.y = 0;
            lineTo = new CT_Point2D();
            wrapThrough.wrapPolygon.lineTo = new List <CT_Point2D>();
            lineTo   = new CT_Point2D();
            lineTo.x = 0;
            lineTo.y = 1343;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 1343;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 0;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            lineTo.x = 0;
            lineTo.y = 0;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 0, 0, wrapThrough, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();


            gp = m_Docx.CreateParagraph();
            //gp.GetCTPPr().AddNewJc().val = ST_Jc.both;
            // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符
            gp.IndentationFirstLine = 15;
            gr = gp.CreateRun();
            gr.SetText("NPOI是POI项目的.NET版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。使用NPOI你就可以在没有安装Office或者相应环境的机器上对WORD/EXCEL文档进行读写。NPOI是构建在POI3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。");
            gp = m_Docx.CreateParagraph();
            // gp.GetCTPPr().AddNewJc().val = ST_Jc.both;
            //gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符
            gp.IndentationFirstLine = 15;
            gr = gp.CreateRun();
            gr.SetText("NPOI之所以强大,并不是因为它支持导出Excel,而是因为它支持导入Excel,并能“理解”OLE2文档结构,这也是其他一些Excel读写库比较弱的方面。通常,读入并理解结构远比导出来得复杂,因为导入你必须假设一切情况都是可能的,而生成你只要保证满足你自己需求就可以了,如果把导入需求和生成需求比做两个集合,那么生成需求通常都是导入需求的子集。");
            //在本段中插图-wrapSquare
            //gr = gp.CreateRun();//创建run
            wrapSquare          = new CT_WrapSquare();
            wrapSquare.wrapText = ST_WrapText.bothSides;
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件
            // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 900000, 200000, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();


            m_Docx.Write(ms);
            ms.Flush();
            SaveToFile(ms, Path.GetPathRoot(Directory.GetCurrentDirectory()) + "\\NPOI.docx");
        }
예제 #15
0
파일: StylesTable.cs 프로젝트: zbl960/npoi
        /**
         * Write this table out as XML.
         *
         * @param out The stream to write to.
         * @throws IOException if an error occurs while writing.
         */
        public void WriteTo(Stream out1)
        {
            // Work on the current one
            // Need to do this, as we don't handle
            //  all the possible entries yet
            CT_Stylesheet styleSheet = doc.GetStyleSheet();

            // Formats
            CT_NumFmts ctFormats = new CT_NumFmts();

            ctFormats.count = (uint)numberFormats.Count;
            if (ctFormats.count > 0)
            {
                ctFormats.countSpecified = true;
            }
            foreach (KeyValuePair <int, String> fmt in numberFormats)
            {
                CT_NumFmt ctFmt = ctFormats.AddNewNumFmt();
                ctFmt.numFmtId   = (uint)fmt.Key;
                ctFmt.formatCode = fmt.Value;
            }
            if (ctFormats.count > 0)
            {
                styleSheet.numFmts = ctFormats;
            }

            // Fonts
            CT_Fonts ctFonts = new CT_Fonts();

            ctFonts.count = (uint)fonts.Count;
            if (ctFonts.count > 0)
            {
                ctFonts.countSpecified = true;
            }
            List <CT_Font> ctfnt = new List <CT_Font>(fonts.Count);

            foreach (XSSFFont f in fonts)
            {
                ctfnt.Add(f.GetCTFont());
            }
            ctFonts.SetFontArray(ctfnt);
            styleSheet.fonts = (ctFonts);

            // Fills
            List <CT_Fill> ctf = new List <CT_Fill>(fills.Count);

            foreach (XSSFCellFill f in fills)
            {
                ctf.Add(f.GetCTFill());
            }
            CT_Fills ctFills = new CT_Fills();

            ctFills.SetFillArray(ctf);
            ctFills.count = (uint)fills.Count;
            if (ctFills.count > 0)
            {
                ctFills.countSpecified = true;
            }
            styleSheet.fills = ctFills;

            // Borders
            List <CT_Border> ctb = new List <CT_Border>(borders.Count);

            foreach (XSSFCellBorder b in borders)
            {
                ctb.Add(b.GetCTBorder());
            }
            CT_Borders ctBorders = new CT_Borders();

            ctBorders.SetBorderArray(ctb);
            ctBorders.count    = (uint)ctb.Count;
            styleSheet.borders = ctBorders;

            // Xfs
            if (xfs.Count > 0)
            {
                CT_CellXfs ctXfs = new CT_CellXfs();
                ctXfs.count = (uint)xfs.Count;
                if (ctXfs.count > 0)
                {
                    ctXfs.countSpecified = true;
                }
                ctXfs.xf = xfs;

                styleSheet.cellXfs = (ctXfs);
            }

            // Style xfs
            if (styleXfs.Count > 0)
            {
                CT_CellStyleXfs ctSXfs = new CT_CellStyleXfs();
                ctSXfs.count = (uint)(styleXfs.Count);
                if (ctSXfs.count > 0)
                {
                    ctSXfs.countSpecified = true;
                }
                ctSXfs.xf = styleXfs;

                styleSheet.cellStyleXfs = (ctSXfs);
            }

            // Style dxfs
            if (dxfs.Count > 0)
            {
                CT_Dxfs ctDxfs = new CT_Dxfs();
                ctDxfs.count = (uint)dxfs.Count;
                if (ctDxfs.count > 0)
                {
                    ctDxfs.countSpecified = true;
                }
                ctDxfs.dxf = dxfs;

                styleSheet.dxfs = (ctDxfs);
            }

            // Save
            doc.Save(out1);
        }
예제 #16
0
파일: StylesTable.cs 프로젝트: zbl960/npoi
        /**
         * Read this shared styles table from an XML file.
         *
         * @param is The input stream Containing the XML document.
         * @throws IOException if an error occurs while Reading.
         */

        protected void ReadFrom(XmlDocument xmldoc)
        {
            try
            {
                doc = StyleSheetDocument.Parse(xmldoc, NamespaceManager);

                CT_Stylesheet styleSheet = doc.GetStyleSheet();

                // Grab all the different bits we care about
                CT_NumFmts ctfmts = styleSheet.numFmts;
                if (ctfmts != null)
                {
                    foreach (CT_NumFmt nfmt in ctfmts.numFmt)
                    {
                        numberFormats.Add((int)nfmt.numFmtId, nfmt.formatCode);
                    }
                }

                CT_Fonts ctfonts = styleSheet.fonts;
                if (ctfonts != null)
                {
                    int idx = 0;
                    foreach (CT_Font font in ctfonts.font)
                    {
                        // Create the font and save it. Themes Table supplied later
                        XSSFFont f = new XSSFFont(font, idx);
                        fonts.Add(f);
                        idx++;
                    }
                }
                CT_Fills ctFills = styleSheet.fills;
                if (ctFills != null)
                {
                    foreach (CT_Fill fill in ctFills.fill)
                    {
                        fills.Add(new XSSFCellFill(fill));
                    }
                }

                CT_Borders ctborders = styleSheet.borders;
                if (ctborders != null)
                {
                    foreach (CT_Border border in ctborders.border)
                    {
                        borders.Add(new XSSFCellBorder(border));
                    }
                }

                CT_CellXfs cellXfs = styleSheet.cellXfs;
                if (cellXfs != null)
                {
                    xfs.AddRange(cellXfs.xf);
                }

                CT_CellStyleXfs cellStyleXfs = styleSheet.cellStyleXfs;
                if (cellStyleXfs != null)
                {
                    styleXfs.AddRange(cellStyleXfs.xf);
                }

                CT_Dxfs styleDxfs = styleSheet.dxfs;
                if (styleDxfs != null)
                {
                    dxfs.AddRange(styleDxfs.dxf);
                }
            }
            catch (XmlException e)
            {
                throw new IOException(e.Message);
            }
        }
예제 #17
0
        public void WriteTo(Stream out1)
        {
            CT_Stylesheet styleSheet = this.doc.GetStyleSheet();
            CT_NumFmts    ctNumFmts  = new CT_NumFmts();

            ctNumFmts.count = (uint)this.numberFormats.Count;
            if (ctNumFmts.count > 0U)
            {
                ctNumFmts.countSpecified = true;
            }
            foreach (KeyValuePair <int, string> numberFormat in this.numberFormats)
            {
                CT_NumFmt ctNumFmt = ctNumFmts.AddNewNumFmt();
                ctNumFmt.numFmtId   = (uint)numberFormat.Key;
                ctNumFmt.formatCode = numberFormat.Value;
            }
            styleSheet.numFmts = ctNumFmts;
            CT_Fonts ctFonts = new CT_Fonts();

            ctFonts.count = (uint)this.fonts.Count;
            if (ctFonts.count > 0U)
            {
                ctFonts.countSpecified = true;
            }
            CT_Font[] array1 = new CT_Font[this.fonts.Count];
            int       num    = 0;

            foreach (XSSFFont font in this.fonts)
            {
                array1[num++] = font.GetCTFont();
            }
            ctFonts.SetFontArray(array1);
            styleSheet.fonts = ctFonts;
            List <CT_Fill> array2 = new List <CT_Fill>(this.fills.Count);

            foreach (XSSFCellFill fill in this.fills)
            {
                array2.Add(fill.GetCTFill());
            }
            CT_Fills ctFills = new CT_Fills();

            ctFills.SetFillArray(array2);
            ctFills.count = (uint)this.fills.Count;
            if (ctFills.count > 0U)
            {
                ctFills.countSpecified = true;
            }
            styleSheet.fills = ctFills;
            List <CT_Border> array3 = new List <CT_Border>(this.borders.Count);

            foreach (XSSFCellBorder border in this.borders)
            {
                array3.Add(border.GetCTBorder());
            }
            CT_Borders ctBorders = new CT_Borders();

            ctBorders.SetBorderArray(array3);
            ctBorders.count = (uint)array3.Count;
            if (ctBorders.count > 0U)
            {
                ctBorders.countSpecified = true;
            }
            styleSheet.borders = ctBorders;
            if (this.xfs.Count > 0)
            {
                CT_CellXfs ctCellXfs = new CT_CellXfs();
                ctCellXfs.count = (uint)this.xfs.Count;
                if (ctCellXfs.count > 0U)
                {
                    ctCellXfs.countSpecified = true;
                }
                ctCellXfs.xf       = this.xfs;
                styleSheet.cellXfs = ctCellXfs;
            }
            if (this.styleXfs.Count > 0)
            {
                CT_CellStyleXfs ctCellStyleXfs = new CT_CellStyleXfs();
                ctCellStyleXfs.count = (uint)this.styleXfs.Count;
                if (ctCellStyleXfs.count > 0U)
                {
                    ctCellStyleXfs.countSpecified = true;
                }
                ctCellStyleXfs.xf       = this.styleXfs;
                styleSheet.cellStyleXfs = ctCellStyleXfs;
            }
            if (this.dxfs.Count > 0)
            {
                CT_Dxfs ctDxfs = new CT_Dxfs();
                ctDxfs.count = (uint)this.dxfs.Count;
                if (ctDxfs.count > 0U)
                {
                    ctDxfs.countSpecified = true;
                }
                ctDxfs.dxf      = this.dxfs;
                styleSheet.dxfs = ctDxfs;
            }
            this.doc.Save(out1);
        }
예제 #18
0
    /// <summary>
    /// 创建文档
    /// </summary>
    /// <param name="setting"></param>
    public static void ExportDocument(DocumentSetting setting)
    {
        XWPFDocument docx = new XWPFDocument();
        MemoryStream ms   = new MemoryStream();

        //设置文档
        docx.Document.body.sectPr = new CT_SectPr();
        CT_SectPr setPr = docx.Document.body.sectPr;
        //获取页面大小
        Tuple <int, int> size = GetPaperSize(setting.PaperType);

        setPr.pgSz.w = (ulong)size.Item1;
        setPr.pgSz.h = (ulong)size.Item2;
        //创建一个段落
        CT_P p = docx.Document.body.AddNewP();

        //段落水平居中
        p.AddNewPPr().AddNewJc().val = ST_Jc.center;
        XWPFParagraph gp             = new XWPFParagraph(p, docx);

        XWPFRun gr = gp.CreateRun();

        //创建标题
        if (!string.IsNullOrEmpty(setting.TitleSetting.Title))
        {
            gr.GetCTR().AddNewRPr().AddNewRFonts().ascii    = setting.TitleSetting.FontName;
            gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = setting.TitleSetting.FontName;
            gr.GetCTR().AddNewRPr().AddNewRFonts().hint     = ST_Hint.eastAsia;
            gr.GetCTR().AddNewRPr().AddNewSz().val          = (ulong)setting.TitleSetting.FontSize; //2号字体
            gr.GetCTR().AddNewRPr().AddNewSzCs().val        = (ulong)setting.TitleSetting.FontSize;
            gr.GetCTR().AddNewRPr().AddNewB().val           = setting.TitleSetting.HasBold;         //加粗
            gr.GetCTR().AddNewRPr().AddNewColor().val       = "black";                              //字体颜色
            gr.SetText(setting.TitleSetting.Title);
        }

        //创建文档主要内容
        if (!string.IsNullOrEmpty(setting.MainContentSetting.MainContent))
        {
            p = docx.Document.body.AddNewP();
            p.AddNewPPr().AddNewJc().val = ST_Jc.both;
            gp = new XWPFParagraph(p, docx)
            {
                IndentationFirstLine = 2
            };

            //单倍为默认值(240)不需设置,1.5倍=240X1.5=360,2倍=240X2=480
            p.AddNewPPr().AddNewSpacing().line     = "400";//固定20磅
            p.AddNewPPr().AddNewSpacing().lineRule = ST_LineSpacingRule.exact;

            gr = gp.CreateRun();
            CT_RPr   rpr    = gr.GetCTR().AddNewRPr();
            CT_Fonts rfonts = rpr.AddNewRFonts();
            rfonts.ascii    = setting.MainContentSetting.FontName;
            rfonts.eastAsia = setting.MainContentSetting.FontName;
            rpr.AddNewSz().val   = (ulong)setting.MainContentSetting.FontSize;//5号字体-21
            rpr.AddNewSzCs().val = (ulong)setting.MainContentSetting.FontSize;
            rpr.AddNewB().val    = setting.MainContentSetting.HasBold;

            gr.SetText(setting.MainContentSetting.MainContent);
        }

        //开始写入
        docx.Write(ms);

        using (FileStream fs = new FileStream(setting.SavePath, FileMode.Create, FileAccess.Write))
        {
            byte[] data = ms.ToArray();
            fs.Write(data, 0, data.Length);
            fs.Flush();
        }
        ms.Close();
    }
예제 #19
0
 protected void ReadFrom(Stream is1)
 {
     try
     {
         this.doc = StyleSheetDocument.Parse(is1);
         CT_Stylesheet styleSheet = this.doc.GetStyleSheet();
         CT_NumFmts    numFmts    = styleSheet.numFmts;
         if (numFmts != null)
         {
             foreach (CT_NumFmt ctNumFmt in numFmts.numFmt)
             {
                 this.numberFormats.Add((int)ctNumFmt.numFmtId, ctNumFmt.formatCode);
             }
         }
         CT_Fonts fonts = styleSheet.fonts;
         if (fonts != null)
         {
             int index = 0;
             foreach (CT_Font font in fonts.font)
             {
                 this.fonts.Add(new XSSFFont(font, index));
                 ++index;
             }
         }
         CT_Fills fills = styleSheet.fills;
         if (fills != null)
         {
             foreach (CT_Fill Fill in fills.fill)
             {
                 this.fills.Add(new XSSFCellFill(Fill));
             }
         }
         CT_Borders borders = styleSheet.borders;
         if (borders != null)
         {
             foreach (CT_Border border in borders.border)
             {
                 this.borders.Add(new XSSFCellBorder(border));
             }
         }
         CT_CellXfs cellXfs = styleSheet.cellXfs;
         if (cellXfs != null)
         {
             this.xfs.AddRange((IEnumerable <CT_Xf>)cellXfs.xf);
         }
         CT_CellStyleXfs cellStyleXfs = styleSheet.cellStyleXfs;
         if (cellStyleXfs != null)
         {
             this.styleXfs.AddRange((IEnumerable <CT_Xf>)cellStyleXfs.xf);
         }
         CT_Dxfs dxfs = styleSheet.dxfs;
         if (dxfs == null)
         {
             return;
         }
         this.dxfs.AddRange((IEnumerable <CT_Dxf>)dxfs.dxf);
     }
     catch (XmlException ex)
     {
         throw new IOException(ex.Message);
     }
 }