Example #1
0
        private static void WriteByReadTemplate()
        {
            using (var dotStream = new FileStream("read.docx", FileMode.Open, FileAccess.Read))
            {
                XWPFDocument template = new XWPFDocument(dotStream);

                using (var fileStream = new FileStream("test.docx", FileMode.Create, FileAccess.Write))
                {
                    XWPFDocument document  = new XWPFDocument();
                    XWPFStyles   newStyles = document.CreateStyles();
                    newStyles.SetStyles(template.GetCTStyle());

                    XWPFParagraph paragraph = document.CreateParagraph();
                    paragraph.Style = "a3";
                    XWPFRun xwpfRun = paragraph.CreateRun();
                    xwpfRun.SetText("标题内容");

                    XWPFParagraph paragraph1 = document.CreateParagraph();
                    paragraph1.Style = "1";
                    XWPFRun xwpfRun1 = paragraph1.CreateRun();
                    xwpfRun1.SetText("标题1内容");

                    XWPFParagraph paragraph2 = document.CreateParagraph();
                    paragraph2.Style = "2";
                    XWPFRun xwpfRun2 = paragraph2.CreateRun();
                    xwpfRun2.SetText("标题2内容");

                    document.Write(fileStream);

                    document.Close();
                }
                template.Close();
            }
        }
Example #2
0
        public void TestEasyAccessToStyles()
        {
            XWPFDocument doc    = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");
            XWPFStyles   styles = doc.GetStyles();

            Assert.IsNotNull(styles);

            // Has 3 paragraphs on page one, a break, and 3 on page 2
            Assert.AreEqual(7, doc.Paragraphs.Count);

            // Check the first three have no run styles, just default paragraph style
            for (int i = 0; i < 3; i++)
            {
                XWPFParagraph p = doc.Paragraphs[(i)];
                Assert.AreEqual(null, p.Style);
                Assert.AreEqual(null, p.StyleID);
                Assert.AreEqual(1, p.Runs.Count);

                XWPFRun r = p.Runs[(0)];
                Assert.AreEqual(null, r.GetColor());
                Assert.AreEqual(null, r.FontFamily);
                Assert.AreEqual(null, r.FontName);
                Assert.AreEqual(-1, r.FontSize);
            }

            // On page two, has explicit styles, but on Runs not on
            //  the paragraph itself
            for (int i = 4; i < 7; i++)
            {
                XWPFParagraph p = doc.Paragraphs[(i)];
                Assert.AreEqual(null, p.Style);
                Assert.AreEqual(null, p.StyleID);
                Assert.AreEqual(1, p.Runs.Count);

                XWPFRun r = p.Runs[(0)];
                Assert.AreEqual("Arial Black", r.FontFamily);
                Assert.AreEqual("Arial Black", r.FontName);
                Assert.AreEqual(16, r.FontSize);
                Assert.AreEqual("548DD4", r.GetColor());
            }

            // Check the document styles
            // Should have a style defined for each type
            Assert.AreEqual(4, styles.NumberOfStyles);
            Assert.IsNotNull(styles.GetStyle("Normal"));
            Assert.IsNotNull(styles.GetStyle("DefaultParagraphFont"));
            Assert.IsNotNull(styles.GetStyle("TableNormal"));
            Assert.IsNotNull(styles.GetStyle("NoList"));

            // We can't do much yet with latent styles
            Assert.AreEqual(137, styles.LatentStyles.NumberOfStyles);

            // Check the default styles
            Assert.IsNotNull(styles.DefaultRunStyle);
            Assert.IsNotNull(styles.DefaultParagraphStyle);

            Assert.AreEqual(11, styles.DefaultRunStyle.FontSize);
            Assert.AreEqual(200, styles.DefaultParagraphStyle.SpacingAfter);
        }
Example #3
0
        public void Test52449()
        {
            XWPFDocument doc    = XWPFTestDataSamples.OpenSampleDocument("52449.docx");
            XWPFStyles   styles = doc.GetStyles();

            Assert.IsNotNull(styles);

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(doc);

            styles = docIn.GetStyles();
            Assert.IsNotNull(styles);
        }
Example #4
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);
        }
Example #5
0
        public void TestSetStyles_Bug57254()
        {
            XWPFDocument docOut = new XWPFDocument();
            XWPFStyles   styles = docOut.CreateStyles();

            CT_Styles ctStyles   = new CT_Styles();
            String    strStyleId = "headline1";
            CT_Style  ctStyle    = ctStyles.AddNewStyle();

            ctStyle.styleId = (/*setter*/ strStyleId);
            styles.SetStyles(ctStyles);

            Assert.IsTrue(styles.StyleExist(strStyleId));

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            styles = docIn.GetStyles();
            Assert.IsTrue(styles.StyleExist(strStyleId));
        }
Example #6
0
        /// <summary>
        /// 加入标题定义
        /// </summary>
        /// <param name="docxDocument"></param>
        /// <param name="strStyleId"></param>
        /// <param name="headingLevel"></param>
        private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel)
        {
            CT_Style ctStyle = new CT_Style();

            ctStyle.styleId = strStyleId;

            CT_String styleName = new CT_String();

            styleName.val = strStyleId;
            ctStyle.name  = styleName;

            CT_DecimalNumber indentNumber = new CT_DecimalNumber();

            indentNumber.val = BigInteger.ValueOf(headingLevel).ToString();

            // lower number > style is more prominent in the formats bar
            ctStyle.uiPriority = indentNumber;

            CT_OnOff onoffnull = new CT_OnOff();

            ctStyle.unhideWhenUsed = onoffnull;

            // style shows up in the formats bar
            ctStyle.qFormat = onoffnull;

            // style defines a heading of the given level
            CT_PPr ppr = new CT_PPr();

            ppr.outlineLvl = indentNumber;
            ctStyle.pPr    = ppr;

            XWPFStyle style = new XWPFStyle(ctStyle);

            // is a null op if already defined
            XWPFStyles styles = docxDocument.CreateStyles();

            style.StyleType = ST_StyleType.paragraph;

            if (styles.GetStyle(strStyleId) == null)
            {
                styles.AddStyle(style);
            }
        }
Example #7
0
        public void TestAddStylesToDocument()
        {
            XWPFDocument docOut = new XWPFDocument();
            XWPFStyles   styles = docOut.CreateStyles();

            String   strStyleId = "headline1";
            CT_Style ctStyle    = new CT_Style();

            ctStyle.styleId = (strStyleId);
            XWPFStyle s = new XWPFStyle(ctStyle);

            styles.AddStyle(s);

            Assert.IsTrue(styles.StyleExist(strStyleId));

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            styles = docIn.GetStyles();
            Assert.IsTrue(styles.StyleExist(strStyleId));
        }
Example #8
0
        public void TestGetUsedStyles()
        {
            XWPFDocument     sampleDoc         = XWPFTestDataSamples.OpenSampleDocument("Styles.docx");
            List <XWPFStyle> testUsedStyleList = new List <XWPFStyle>();
            XWPFStyles       styles            = sampleDoc.GetStyles();
            XWPFStyle        style             = styles.GetStyle("berschrift1");

            testUsedStyleList.Add(style);
            testUsedStyleList.Add(styles.GetStyle("Standard"));
            testUsedStyleList.Add(styles.GetStyle("berschrift1Zchn"));
            testUsedStyleList.Add(styles.GetStyle("Absatz-Standardschriftart"));
            style.HasSameName(style);

            List <XWPFStyle> usedStyleList = styles.GetUsedStyleList(style);

            //Assert.AreEqual(usedStyleList, testUsedStyleList);
            Assert.AreEqual(usedStyleList.Count, testUsedStyleList.Count);
            for (int i = 0; i < usedStyleList.Count; i++)
            {
                Assert.AreEqual(usedStyleList[i], testUsedStyleList[i]);
            }
        }
Example #9
0
        public MemoryStream CreateTableDoc(List <StaffViewModel> staffs, int countRecord = 0)
        {
            MemoryStream  result = new MemoryStream();
            XWPFDocument  doc    = new XWPFDocument();
            XWPFStyles    styles = doc.CreateStyles();
            XWPFParagraph pCha   = doc.CreateParagraph();
            XWPFTable     table  = doc.CreateTable(countRecord + 1, 7);

            table.Width = 3500;
            AddCell(table.GetRow(0).GetCell(0), "Фамилия");
            AddCell(table.GetRow(0).GetCell(1), "Имя");
            AddCell(table.GetRow(0).GetCell(2), "Отчество");
            AddCell(table.GetRow(0).GetCell(3), "Должность");
            AddCell(table.GetRow(0).GetCell(4), "Звание");
            AddCell(table.GetRow(0).GetCell(5), "Подразделение");
            AddCell(table.GetRow(0).GetCell(6), "Уволен");
            int i = 1;

            if (staffs != null)
            {
                foreach (var item in staffs)
                {
                    AddCell(table.GetRow(i).GetCell(0), item.Second);
                    AddCell(table.GetRow(i).GetCell(1), item.First);
                    AddCell(table.GetRow(i).GetCell(2), item.MiddleName);
                    AddCell(table.GetRow(i).GetCell(3), item.Position.Name);
                    AddCell(table.GetRow(i).GetCell(4), item.Rank.Name);
                    AddCell(table.GetRow(i).GetCell(5), item.SubDepartmen.Name);
                    AddCell(table.GetRow(i).GetCell(6), item.Fired == true ? "Уволен" : "Работает");
                    // table.GetRow(i).GetCell(0).SetText(item.Second);
                    ++i;
                }
            }
            table.ColBandSize = 14;

            doc.Write(result);
            return(result);
        }
Example #10
0
        /// <summary>
        /// 创建文档
        /// </summary>
        /// <param name="setting"></param>
        public static void ExportDocument(List <Table> listTable, List <TableMapping> listMapping, string fileName)
        {
            XWPFDocument doc = new XWPFDocument(NPOI.OpenXml4Net.OPC.OPCPackage.Open("12345.docx"));

            MemoryStream    ms      = new MemoryStream();
            DocumentSetting setting = new DocumentSetting();

            int docItemsCount = doc.BodyElements.Count;

            for (int i = 0; i < docItemsCount; i++)
            {
                doc.RemoveBodyElement(0);
            }

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

            setPr.pgSz.w = (ulong)size.Item1;
            setPr.pgSz.h = (ulong)size.Item2;

            int tableNum = 0;

            foreach (var table in listTable)
            {
                tableNum++;

                string tableTitle = table.TableName;
                if (!string.IsNullOrEmpty(table.TableComment))
                {
                    tableTitle = $"{table.TableComment} {tableTitle}";
                }

                Console.WriteLine($"正在处理 {tableNum}.{tableTitle}");

                //创建一个段落
                XWPFParagraph gp = doc.CreateParagraph();

                XWPFStyles styles = doc.CreateStyles();

                //表格段落标题
                gp.SetNumID($"2.1");
                gp.Style = "Heading2";
                XWPFRun gr = gp.CreateRun();
                gr.SetText($"{tableTitle}");

                //获取表字段信息
                var listTableSchema = GetTableSchema <TableSchema>(MysqlGetTableSchemaSQL(table.TableName));

                //创建表格
                XWPFTable docTable = doc.CreateTable(listTableSchema.Count + 1, listMapping.Count);
                int       i        = 0;
                foreach (var tableMapping in listMapping)
                {
                    var currentCell = docTable.GetRow(0).GetCell(i);
                    currentCell = SetCell(currentCell, tableMapping.DocumentColumn, tableMapping.ColumnWidth, "CCCCCC", true, 10, true);
                    //var ppr = currentCell.GetCTTc().AddNewP().AddNewPPr();
                    //space.line="0";
                    //space.lineRule = ST_LineSpacingRule.auto;
                    i++;
                }
                int rowIndex = 1;
                foreach (var tableSchema in listTableSchema)
                {
                    int cellIndex = 0;
                    foreach (var tableMapping in listMapping)
                    {
                        var    currentCell = docTable.GetRow(rowIndex).GetCell(cellIndex);
                        string color       = string.Empty;
                        string text        = string.Empty;
                        if (rowIndex % 2 == 0)
                        {
                            color = "DDDDDD";
                        }

                        if (string.IsNullOrEmpty(tableMapping.DBColumn))
                        {
                            text = rowIndex.ToString();
                        }
                        else
                        {
                            text = GetPropValue(tableSchema, tableMapping.DBColumn);
                        }

                        currentCell = SetCell(currentCell, text, tableMapping.ColumnWidth, color, tableMapping.IsCenter, 10);

                        cellIndex++;
                    }
                    rowIndex++;
                }
            }

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

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                byte[] data = ms.ToArray();
                fs.Write(data, 0, data.Length);
                fs.Flush();
            }
            ms.Close();
        }
Example #11
0
 public DocumentStylesBuilder(XWPFDocument docxDocument)
 {
     documentStyles = docxDocument.CreateStyles();
     ctStyles       = new CT_Styles();
 }
Example #12
0
 public DocumentStylesBuilder()
 {
     documentStyles = new XWPFStyles();
     ctStyles       = new CT_Styles();
 }