Beispiel #1
0
        /// <summary>
        /// 创建题目文件
        /// </summary>
        /// <param name="queryItems">
        /// The query items.
        /// </param>
        /// <returns>
        /// System.String.
        /// </returns>
        private string CreateQueryDocFile(QueryItem[] queryItems)
        {
            this.ShowLog("生成 word 文件");
            var docFile = this.CreateQueryDocFile();

            this.ShowLog("更新 word 文件");
            var columnNum = WinFormExtension.GetControlValue(
                this.columnNumNumericUpDown,
                ctrl => Convert.ToInt32(ctrl.Value));
            this.InsertQuerys(queryItems, columnNum, docFile);

            return docFile;
        }
Beispiel #2
0
        /// <summary>
        /// 将题目插入到 Doc 文件中
        /// </summary>
        /// <param name="queryItems">
        /// The query items.
        /// </param>
        /// <param name="columnNumber">
        /// 多少列.
        /// </param>
        /// <param name="docFile">
        /// The document file.
        /// </param>
        private void InsertQuerys(QueryItem[] queryItems, int columnNumber, string docFile)
        {
            // todo: 以后脱离 office
            Application application = new ApplicationClass();
            application.Visible = false;
            var document = application.Documents.Open(docFile);

            try
            {
                // 插入日期 手工写好了
                /*object dateBookmark = @"date";
                var bookmark = document.Bookmarks[dateBookmark];
                var bookmarkRang = bookmark.Range;
                var length = bookmarkRang.Text?.Length;
                var date = $"{DateTime.Now:yyyy年M月d日 H时m分}".PadRight(length.GetValueOrDefault(0));
                bookmarkRang.Text = date;
                bookmarkRang.Font.Underline = WdUnderline.wdUnderlineSingle;*/

                // 插入表格
                var rowNumber = queryItems.Length / columnNumber + (queryItems.Length % columnNumber != 0 ? 1 : 0);
                var paragraph = document.Content.Paragraphs.Add(Missing.Value);
                var table = document.Tables.Add(paragraph.Range, rowNumber, columnNumber);
                table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                table.Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5;// WdLineSpacing.wdLineSpaceDouble;
                table.Range.Font.Size = 9;
                table.Range.Font.Spacing = 1.5f;

                // 设置单元格式
                for (var i = 0; i < rowNumber; i++)
                {
                    for (var j = 0; j < columnNumber; j++)
                    {
                        var content = string.Empty;
                        var index = i * columnNumber + j;
                        if (index < queryItems.Length)
                        {
                            content = queryItems[index].ToString();
                        }

                        var cell = table.Cell(i + 1, j + 1);
            //                        this.SetBoards(cell.Borders);
                        cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                        cell.Range.Text = content;
                    }
                }

                document.Save();
            }
            finally
            {
                document.Close();
                application.Quit();
                ComObjectDisposeHelper.DisposeObject(application);

                application = null;
            }
        }