Exemplo n.º 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();
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        private static void CopyStyle(this XWPFDocument srcDoc, XWPFDocument destDoc, XWPFStyle style)
        {
            if (destDoc == null || style == null)
            {
                return;
            }

            if (destDoc.GetStyles() == null)
            {
                destDoc.CreateStyles();
            }

            foreach (var xwpfStyle in srcDoc.GetStyles().GetUsedStyleList(style))
            {
                destDoc.GetStyles().AddStyle(xwpfStyle);
            }
        }
Exemplo n.º 4
0
        private static void CopyStyle(XWPFDocument srcDoc, XWPFDocument destDoc, XWPFStyle style)
        {
            if (destDoc == null || style == null)
            {
                return;
            }

            if (destDoc.GetCTStyle() == null)
            {
                destDoc.CreateStyles();
            }

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

            for (int i = 0; i < usedStyleList.Count; i++)
            {
                destDoc.GetStyles().AddStyle(usedStyleList[i]);
            }
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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));
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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();
        }
Exemplo n.º 10
0
 public DocumentStylesBuilder(XWPFDocument docxDocument)
 {
     documentStyles = docxDocument.CreateStyles();
     ctStyles       = new CT_Styles();
 }