Esempio 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();
            }
        }
Esempio n. 2
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]);
            }
        }
        public static void Save(ExcelSheetModel[] sheetItems)
        {
            #region 读取模板中样式

            // 默认新创建word没有Heading1,Heading2...这些样式,需要手动创建
            // 采用建一个word样式模板方法
            // 参考(第2个答案):
            // https://stackoverflow.com/questions/2643822/how-can-i-use-predefined-formats-in-docx-with-poi

            /*
             * XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));
             *
             * XWPFDocument doc = new XWPFDocument();
             * // let's copy styles from template to new doc
             * XWPFStyles newStyles = doc.createStyles();
             * newStyles.setStyles(template.getStyle());   // 复制模板样式到当前文件
             *
             * XWPFParagraph para = doc.createParagraph();
             * para.setStyle("Heading1");
             *
             * XWPFRun run = para.createRun();
             * run.setText("Heading 1");
             *
             * return doc;
             */

            NPOI.OpenXmlFormats.Wordprocessing.CT_Styles templateStyle = null;

            if (File.Exists("template.docx"))
            {
                using (var stream = new System.IO.FileStream("template.docx", System.IO.FileMode.Open))
                {
                    XWPFDocument doc = new XWPFDocument(stream);
                    templateStyle = doc.GetCTStyle();
                }
            }
            else
            {
                System.Console.WriteLine("模板文件不存在, 生成word样式会有问题!");
            }

            #endregion

            var downloadDir = StaticVariables.GetDownloadDir();

            if (!Directory.Exists(downloadDir))
            {
                Directory.CreateDirectory(downloadDir);
            }

            #region 每个专辑一个歌词文件

            /*
             * foreach (var item in sheetItems)
             * {
             *  // 每个专辑一个唱词文件
             *  foreach (var mediaItem in item.MediaItems)
             *  {
             *      var subDir = Path.Combine(downloadDir, item.Name, "唱词");
             *      if (!Directory.Exists(subDir))
             *      {
             *          Directory.CreateDirectory(subDir);
             *      }
             *
             *      var fileName = Path.Combine(subDir, mediaItem.Title + ".docx");
             *
             *      using (FileStream fs = new FileStream(fileName, FileMode.Create))
             *      {
             *          var doc = new XWPFDocument();
             *          doc.GetStyles().SetStyles(templateStyle);
             *
             *          WriteMedia(doc, mediaItem);
             *          doc.Write(fs);
             *      }
             *  }
             * }
             */
            #endregion

            #region 所有专辑合并成一个文件

            // 全部的歌词
            var allLyricDoc = new XWPFDocument();
            allLyricDoc.GetStyles().SetStyles(templateStyle);

            foreach (var item in sheetItems)
            {
                // TODO : 前面已经创建过文件
                var subDir = Path.Combine(downloadDir, item.Name);

                if (!Directory.Exists(subDir))
                {
                    Directory.CreateDirectory(subDir);
                }

                try
                {
                    // 每个分类(2018年出品-京剧, 2019年出品-京剧)一个歌词文件
                    var fileName = Path.Combine(subDir, "_所有唱词_" + item.Name + ".docx");

                    using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
                    {
                        var doc = new XWPFDocument();
                        doc.GetStyles().SetStyles(templateStyle);

                        // 当前分类下歌词
                        WriteToDoc(doc, item);

                        // 全部歌词
                        WriteToDoc(allLyricDoc, item);

                        doc.Write(fs);
                    }
                }
                catch (System.Exception)
                {
                    continue;
                }
            }

            // 保存全部歌词
            var allLyricFilePath = Path.Combine(downloadDir, "所有唱词.docx");
            using (FileStream fs = new FileStream(allLyricFilePath, FileMode.OpenOrCreate))
            {
                allLyricDoc.Write(fs);
            }
            #endregion
        }