コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create Word document.
            Document document = new Document();

            //Add a new section.
            Section section = document.AddSection();

            //Add a table with rows and columns and set the text for the table.
            Table table = section.AddTable();

            table.ResetCells(1, 1);
            TableCell cell = table.Rows[0].Cells[0];

            table.Rows[0].Height = 150;
            cell.AddParagraph().AppendText("Draft copy in vertical style");

            //Set the TextDirection for the table to RightToLeftRotated.
            cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;

            //Set the table format.
            table.TableFormat.WrapTextAround              = true;
            table.TableFormat.Positioning.VertRelationTo  = VerticalRelation.Page;
            table.TableFormat.Positioning.HorizRelationTo = HorizontalRelation.Page;
            table.TableFormat.Positioning.HorizPosition   = section.PageSetup.PageSize.Width - table.Width;
            table.TableFormat.Positioning.VertPosition    = 200;

            String result = "Result-CreateVerticalTable.docx";

            //Save to file.
            document.SaveToFile(result, FileFormat.Docx2013);

            //Launch the MS Word file.
            WordDocViewer(result);
        }
コード例 #2
0
        /// <summary>
        /// Создает файл формата .doc
        /// </summary>
        /// <param name="path">Путь к Excel файлу, из которого берется таблица.</param>
        private void CreateWordFile(string path)
        {
            Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
            workbook.LoadFromFile(path);
            Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
            Document            doc   = new Document();
            Table table = doc.AddSection().AddTable(true);

            table.ResetCells(sheet.LastRow, sheet.LastColumn);
            for (int r = 1; r <= sheet.LastRow; r++)
            {
                for (int c = 1; c <= sheet.LastColumn; c++)
                {
                    CellRange xCell     = sheet.Range[r, c];
                    TableCell wCell     = table.Rows[r - 1].Cells[c - 1];
                    TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                }
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                {
                    table.Rows[i].Cells[j].Width = 100f;
                }
            }
            doc.SaveToFile("result.doc", Spire.Doc.FileFormat.Docx);
            System.Diagnostics.Process.Start("result.doc");
        }
コード例 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a document
            Document document = new Document();

            //Load file
            document.LoadFromFile(@"..\..\..\..\..\..\Data\TableSample.docx");
            Section section = document.Sections[0];
            Table   table   = section.Tables[0] as Table;

            //Delete the seventh row
            table.Rows.RemoveAt(7);

            //Add a row and insert it into specific position
            TableRow row = new TableRow(document);

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                TableCell tc        = row.AddCell();
                Paragraph paragraph = tc.AddParagraph();
                paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                paragraph.AppendText("Added");
            }
            table.Rows.Insert(2, row);
            //Add a row at the end of table
            table.AddRow();

            //Save to file and launch it
            document.SaveToFile("AddDeleteRow.docx", FileFormat.Docx);
            FileViewer("AddDeleteRow.docx");
        }
コード例 #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a Word document
            Document doc = new Document();

            //Add a section
            Section section = doc.AddSection();

            //Create a table
            Table table = new Table(doc);

            //Set the width of table
            table.PreferredWidth = new PreferredWidth(WidthType.Percentage, (short)100);
            //Set the border of table
            table.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;

            //Create a table row
            TableRow row = new TableRow(doc);

            row.Height = 50.0f;
            table.Rows.Add(row);

            //Create a table cell
            TableCell cell1 = new TableCell(doc);
            //Add a paragraph
            Paragraph para1 = cell1.AddParagraph();

            //Append text in the paragraph
            para1.AppendText("Row 1, Cell 1");
            //Set the horizontal alignment of paragrah
            para1.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
            //Set the background color of cell
            cell1.CellFormat.BackColor = Color.CadetBlue;
            //Set the vertical alignment of paragraph
            cell1.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            row.Cells.Add(cell1);

            //Create a table cell
            TableCell cell2 = new TableCell(doc);
            Paragraph para2 = cell2.AddParagraph();

            para2.AppendText("Row 1, Cell 2");
            para2.Format.HorizontalAlignment   = Spire.Doc.Documents.HorizontalAlignment.Center;
            cell2.CellFormat.BackColor         = Color.CadetBlue;
            cell2.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            row.Cells.Add(cell2);

            //Add the table in the section
            section.Tables.Add(table);

            //Save the document
            string output = "CreateTableDirectly_out.docx";

            doc.SaveToFile(output, FileFormat.Docx2013);

            //Launch the document
            WordDocViewer(output);
        }
コード例 #5
0
    private static void smethod_1(Class581 A_0, RowFormat A_1)
    {
        int      num    = 8;
        Class394 class2 = A_0.imethod_1();

        if (A_0.vmethod_1())
        {
            Class9 class3 = Class402.smethod_0(A_0, MarkupLevel.Cell);
            A_0.method_3(class3);
            while (class2.method_9(class3.method_22()))
            {
                smethod_1(A_0, A_1);
            }
            A_0.method_5(DocumentObjectType.CustomXml);
        }
        else
        {
            string str = class2.method_1();
            if (str != null)
            {
                if (str == BookmarkStart.b("娭刯帱搳䐵紷䈹", num))
                {
                    Class829.smethod_2(A_1, A_0);
                    return;
                }
                if (str == BookmarkStart.b("娭䈯戱䘳", num))
                {
                    Class1131.smethod_0(A_1, A_0);
                    return;
                }
                if (str == BookmarkStart.b("娭匯", num))
                {
                    Class251.smethod_0(A_0);
                    return;
                }
                if (str == BookmarkStart.b("伭帯就嬳䈵夷丹唻儽⸿", num))
                {
                    TableRow  lastRow   = A_0.Interface50.imethod_0().LastSection.Tables[A_0.Interface50.imethod_0().LastSection.Tables.Count - 1].LastRow;
                    TableCell cell      = lastRow.Cells[lastRow.Cells.Count - 1];
                    Paragraph paragraph = cell.Items[cell.Items.Count - 1] as Paragraph;
                    if (paragraph == null)
                    {
                        paragraph = cell.AddParagraph();
                    }
                    Class519.smethod_1(A_0, paragraph);
                    return;
                }
            }
            string str2 = class2.method_1();
            while (class2.method_9(str2))
            {
                smethod_1(A_0, A_1);
            }
        }
    }
コード例 #6
0
ファイル: Utils.cs プロジェクト: IamZY/TxtToWordTable
        public static void addTable(List <WordEntity> words)
        {
            //创建一个Document类实例,并添加section
            Document doc     = new Document();
            Section  section = doc.AddSection();

            //添加表格
            Table table = section.AddTable(true);

            //添加表格第1行
            TableRow row1 = table.AddRow();

            //添加第1个单元格到第1行
            TableCell cell1 = row1.AddCell();

            cell1.AddParagraph().AppendText("编码");

            //添加第2个单元格到第1行
            TableCell cell2 = row1.AddCell();

            cell2.AddParagraph().AppendText("英文描述");

            //添加第3个单元格到第1行
            TableCell cell3 = row1.AddCell();

            cell3.AddParagraph().AppendText("中文描述");



            foreach (WordEntity w in words)
            {
                //添加表格第2行
                TableRow row2 = table.AddRow(true, false);

                //添加第6个单元格到第2行
                TableCell cell6 = row2.AddCell();
                cell6.AddParagraph().AppendText(w.origin);

                //添加第7个单元格到第2行
                TableCell cell7 = row2.AddCell();
                cell7.AddParagraph().AppendText(w.en_origin);

                //添加第8个单元格到第2行
                TableCell cell8 = row2.AddCell();
                cell8.AddParagraph().AppendText(w.fanyi_res);
            }

            //table.AutoFit()

            table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);

            //保存文档
            doc.SaveToFile(@"d:\Table.docx");
        }
コード例 #7
0
    internal static void smethod_1(Class581 A_0, RowFormat A_1)
    {
        int      num    = 10;
        Class394 class2 = A_0.imethod_1();

        class2.method_18();
        if (A_0.vmethod_1())
        {
            Class9 class3 = Class402.smethod_0(A_0, MarkupLevel.Row);
            A_0.method_3(class3);
            while (class2.method_9(class3.method_22()))
            {
                smethod_1(A_0, A_1);
            }
            A_0.method_5(DocumentObjectType.CustomXml);
        }
        else
        {
            string str = class2.method_1();
            if (str != null)
            {
                if (str == BookmarkStart.b("䐯倱堳昵䨷", num))
                {
                    Class829.smethod_0(A_1, A_0, false);
                    ((Table)A_0.method_13()).TableFormat.ImportContainer(A_1);
                    return;
                }
                if (str == BookmarkStart.b("䐯倱堳焵䨷匹堻", num))
                {
                    Class532.smethod_1(class2, ((Table)A_0.method_13()).TableFormat);
                    return;
                }
                if (str == BookmarkStart.b("䐯䀱", num))
                {
                    Class912.smethod_0(A_0, A_1);
                    return;
                }
                if (str == BookmarkStart.b("儯就娳夵䰷嬹䠻圽⼿ⱁ", num))
                {
                    TableRow  lastRow   = A_0.Interface50.imethod_0().LastSection.Tables[A_0.Interface50.imethod_0().LastSection.Tables.Count - 1].LastRow;
                    TableCell cell      = lastRow.Cells[lastRow.Cells.Count - 1];
                    Paragraph paragraph = cell.Items[cell.Items.Count - 1] as Paragraph;
                    if (paragraph == null)
                    {
                        paragraph = cell.AddParagraph();
                    }
                    Class519.smethod_1(A_0, paragraph);
                    return;
                }
            }
            smethod_3(A_0, A_1);
        }
    }
コード例 #8
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a new document
            Document doc = new Document();

            //Add the first section
            Section section1 = doc.AddSection();

            //Set text direction for all text in a section
            section1.TextDirection = TextDirection.RightToLeft;

            //Set Font Style and Size
            ParagraphStyle style = new ParagraphStyle(doc);

            style.Name = "FontStyle";
            style.CharacterFormat.FontName = "Arial";
            style.CharacterFormat.FontSize = 15;
            doc.Styles.Add(style);

            //Add two paragraphs and apply the font style
            Paragraph p = section1.AddParagraph();

            p.AppendText("Only Spire.Doc, no Microsoft Office automation");
            p.ApplyStyle(style.Name);
            p = section1.AddParagraph();
            p.AppendText("Convert file documents with high quality");
            p.ApplyStyle(style.Name);

            //Set text direction for a part of text
            //Add the second section
            Section section2 = doc.AddSection();
            //Add a table
            Table table = section2.AddTable();

            table.ResetCells(1, 1);
            TableCell cell = table.Rows[0].Cells[0];

            table.Rows[0].Height         = 150;
            table.Rows[0].Cells[0].Width = 10;
            //Set vertical text direction of table
            cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;
            cell.AddParagraph().AppendText("This is vertical style");
            //Add a paragraph and set horizontal text direction
            p = section2.AddParagraph();
            p.AppendText("This is horizontal style");
            p.ApplyStyle(style.Name);

            //Save and launch document
            string output = "SetTextDirection.docx";

            doc.SaveToFile(output, FileFormat.Docx);
            Viewer(output);
        }
コード例 #9
0
        private void Replace_Grid(int Vi_tri_bang, string Ten_file1, string Ten_file2, DataGridView DGV)
        {
            var wordApp = new Microsoft.Office.Interop.Word.Application();
            int col     = DGV.Columns.Count;
            int row     = DGV.Rows.Count;
            //Creat a new document and load the document form file
            Document doc = new Document();

            doc.LoadFromFile(Ten_file2);

            //Get the first table from the word document
            Table table = doc.Sections[0].Tables[Vi_tri_bang] as Spire.Doc.Table;

            //Insert a new row as the THIRD row

            for (int i = 1; i < row; i++)
            {
                table.AddRow();
            }
            for (int i = 1; i < row; i++)
            {
                TableRow oRow = table.Rows[i];

                //format Row
                oRow.Height = 30;

                for (int j = 0; j < DGV.Columns.Count; j++)
                {
                    ////Viết nội dung cho Cell[i][j]
                    TableCell cell = oRow.Cells[j];

                    //Align Cell
                    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;

                    //Fill data in row
                    cell.Paragraphs.Clear();
                    Paragraph p2  = cell.AddParagraph();
                    string    str = DGV.Rows[i - 1].Cells[j].Value == null ? "null" : DGV.Rows[i - 1].Cells[j].Value.ToString();

                    TextRange TR2 = p2.AppendText(str);//Cai nay de modify code chu khong phai de set Text

                    //Format Cell
                    p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
                    TR2.CharacterFormat.FontName  = "Time New Roman";
                    TR2.CharacterFormat.FontSize  = 14;
                    TR2.CharacterFormat.TextColor = Color.Black;
                }
            }

            //Save the document to file and set its file format
            doc.SaveToFile(Ten_file1, FileFormat.Docx2010);
        }
コード例 #10
0
        static void Main(string[] args)
        {
            //Initialize an instance of Workbook class and load excel from file
            Workbook workbook = new Workbook();

            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Create an object of Document class,add table to Word and set the rows and columns number based on the cells range that contains data
            Document doc   = new Document();
            Table    table = doc.AddSection().AddTable(true);

            table.ResetCells(sheet.LastRow, sheet.LastColumn);

            //Traverse the rows and columns of table in worksheet and get the cells,call a custom function CopyStype() to copy the the font style and cell style from Excel to Word table.
            for (int r = 1; r <= sheet.LastRow; r++)
            {
                for (int c = 1; c <= sheet.LastColumn; c++)
                {
                    CellRange xCell = sheet.Range[r, c];
                    TableCell wCell = table.Rows[r - 1].Cells[c - 1];
                    //Fill data to Word table
                    TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                    //Copy the formating of table to Word
                    CopyStyle(textRange, xCell, wCell);
                }
            }
            //Set column width of Word table in Word
            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                {
                    table.Rows[i].Cells[j].Width = 100f;
                }
            }
            //Save document
            doc.SaveToFile("result.doc", Spire.Doc.FileFormat.Docx);
            System.Diagnostics.Process.Start("result.doc");
        }
コード例 #11
0
        private static void FillTableUsingDataTable(Table table, DataTable dataTable)
        {
            int columnCount = dataTable.Columns.Count;

            foreach (DataRow dataRow in dataTable.Rows)
            {
                TableRow row = table.AddRow(columnCount);
                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    int       columnIndex = dataTable.Columns.IndexOf(dataColumn);
                    string    value       = dataRow[dataColumn].ToString();
                    TableCell cell        = row.Cells[columnIndex];
                    //Add paragraph for cell
                    Paragraph para = cell.AddParagraph();
                    //Append text from datatable
                    para.AppendText(value);
                    //Set the alignment of cell
                    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    para.Format.HorizontalAlignment   = Spire.Doc.Documents.HorizontalAlignment.Center;
                }
            }
        }
コード例 #12
0
        private void UpdateCell(TableCell tableCell, string Message, bool isHeader = false, float fontSize = 9)
        {
            Paragraph para = tableCell.AddParagraph();

            if (isHeader)
            {
                tableCell.CellFormat.BackColor  = Color.FromArgb(217, 217, 217);
                para.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
            }
            TextRange TR = para.AppendText(Message);

            //Font name
            TR.CharacterFormat.FontName = "Microsoft YaHei";

            //Font size
            TR.CharacterFormat.FontSize = fontSize;

            ////Underline
            //TR.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot;

            ////Change text color
            //TR.CharacterFormat.TextColor = Color.Blue;
        }
コード例 #13
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create word document
            Document document = new Document();

            //Create a new section
            Section section = document.AddSection();

            //Create a table width default borders
            Table table = section.AddTable(true);
            //Set table with to 100%
            PreferredWidth width = new PreferredWidth(WidthType.Percentage, 100);

            table.PreferredWidth = width;

            //Add a new row
            TableRow row = table.AddRow();

            //Set the row as a table header
            row.IsHeader = true;
            //Set the backcolor of row
            row.RowFormat.BackColor = Color.LightGray;
            //Add a new cell for row
            TableCell cell = row.AddCell();

            cell.SetCellWidth(100, CellWidthType.Percentage);
            //Add a paragraph for cell to put some data
            Paragraph parapraph = cell.AddParagraph();

            //Add text
            parapraph.AppendText("Row Header 1");
            //Set paragraph horizontal center alignment
            parapraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;

            row                     = table.AddRow(false, 1);
            row.IsHeader            = true;
            row.RowFormat.BackColor = Color.Ivory;
            //Set row height
            row.Height = 30;
            cell       = row.Cells[0];
            cell.SetCellWidth(100, CellWidthType.Percentage);
            //Set cell vertical middle alignment
            cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            //Add a paragraph for cell to put some data
            parapraph = cell.AddParagraph();
            //Add text
            parapraph.AppendText("Row Header 2");
            parapraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;

            //Add many common rows
            for (int i = 0; i < 70; i++)
            {
                row  = table.AddRow(false, 2);
                cell = row.Cells[0];
                //Set cell width to 50% of table width
                cell.SetCellWidth(50, CellWidthType.Percentage);
                cell.AddParagraph().AppendText("Column 1 Text");
                cell = row.Cells[1];
                cell.SetCellWidth(50, CellWidthType.Percentage);
                cell.AddParagraph().AppendText("Column 2 Text");
            }
            //Set cell backcolor
            for (int j = 1; j < table.Rows.Count; j++)
            {
                if (j % 2 == 0)
                {
                    TableRow row2 = table.Rows[j];
                    for (int f = 0; f < row2.Cells.Count; f++)
                    {
                        row2.Cells[f].CellFormat.BackColor = Color.LightBlue;
                    }
                }
            }

            String result = "RepeatRowOnEachPage_out.docx";

            //Save file.
            document.SaveToFile(result, FileFormat.Docx);

            //Launching the Word file.
            WordDocViewer(result);
        }
コード例 #14
0
        // Add table to word
        #region converting tables
        public void Add(string filepath, List <string> names, bool boolResult, bool boolSteel)
        {
            //Initialize an instance of Workbook class and load excel from file
            Workbook workbook = new Workbook();

            workbook.LoadFromFile(filepath);

            //Get all worksheets
            WorksheetsCollection worksheets = workbook.Worksheets;
            Section    section1             = doc.Sections[0];
            Worksheet  sheet;
            Paragraph  title;
            Table      table;
            List <int> ColumnList = new List <int>();

            // Fit result's column in one table
            if (boolResult)
            {
                numcol = 99;
                names.Clear();
                foreach (var item in worksheets)
                {
                    names.Add(item.Name);
                }
            }

            foreach (var name in names)
            {
                int iter = 0;
                foreach (Worksheet item in worksheets)
                {
                    if (name == item.Name)
                    {
                        //Get the worksheet
                        sheet = workbook.Worksheets[iter];
                        // Get which column to export
                        ColumnList.Clear();
                        if (!boolResult)
                        {
                            ColumnList = GetColumnList(name, boolSteel);
                        }
                        else
                        {
                            for (int i = 1; i <= sheet.LastColumn; i++)
                            {
                                ColumnList.Add(i);
                            }
                        }

                        if (ColumnList.Count <= numcol) // 1 part
                        {
                            //Write Name of Worksheet
                            title      = section1.AddParagraph();
                            title.Text = item.Name;
                            title.ApplyStyle(style.Name);

                            //Write Table
                            table = section1.AddTable(true);
                            table.ResetCells(sheet.LastRow, ColumnList.Count);
                            if (!boolResult)
                            {
                                table.ApplyHorizontalMerge(0, 0, (ColumnList.Count - 1));
                            }

                            //Traverse the rows and columns of table in worksheet and get the cells, call a custom function CopyStyle() to copy the font style and cell style from Excel to Word table.
                            for (int r = 1; r <= sheet.LastRow; r++)
                            {
                                for (int c = 1; c <= ColumnList.Count; c++)
                                {
                                    int       c2    = ColumnList[c - 1];
                                    CellRange xCell = sheet.Range[r, c2];
                                    TableCell wCell = table.Rows[r - 1].Cells[c - 1];
                                    //Fill data to Word table
                                    TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                    //Copy the formatting of table to Word
                                    CopyStyle(textRange, xCell, wCell);
                                }
                            }
                            //Set column width of Word table in Word
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                                {
                                    table.Rows[i].Cells[j].Width = 100f;
                                }
                            }

                            //NoBreakTable(table);
                        }
                        else if (ColumnList.Count < 2 * numcol - 1) // 2 part
                        {
                            //Part 1 of 2
                            title      = section1.AddParagraph();
                            title.Text = item.Name + " Part 1 of 2";
                            title.ApplyStyle(style.Name);

                            //Write Table
                            table = section1.AddTable(true);
                            table.ResetCells(sheet.LastRow, numcol);
                            if (!boolResult)
                            {
                                table.ApplyHorizontalMerge(0, 0, numcol - 1);
                            }
                            //Traverse the rows and columns of table in worksheet and get the cells, call a custom function CopyStyle() to copy the font style and cell style from Excel to Word table.
                            for (int r = 1; r <= sheet.LastRow; r++)
                            {
                                for (int c = 1; c <= numcol; c++)
                                {
                                    int       c2    = ColumnList[c - 1];
                                    CellRange xCell = sheet.Range[r, c2];
                                    TableCell wCell = table.Rows[r - 1].Cells[c - 1];
                                    //Fill data to Word table
                                    TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                    //Copy the formatting of table to Word
                                    CopyStyle(textRange, xCell, wCell);
                                }
                            }
                            //Set column width of Word table in Word
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                                {
                                    table.Rows[i].Cells[j].Width = 100f;
                                }
                            }

                            //NoBreakTable(table);

                            //Part 2 of 2
                            title      = section1.AddParagraph();
                            title.Text = item.Name + " Part 2 of 2";
                            title.ApplyStyle(style.Name);

                            //Write Table
                            table = section1.AddTable(true);
                            table.ResetCells(sheet.LastRow, ColumnList.Count - numcol + 1); // first column and the rest
                            if (!boolResult)
                            {
                                table.ApplyHorizontalMerge(0, 0, ColumnList.Count - numcol);
                            }
                            //Traverse the rows and columns of table in worksheet and get the cells, call a custom function CopyStyle() to copy the font style and cell style from Excel to Word table.
                            for (int r = 1; r <= sheet.LastRow; r++)
                            {
                                //for c = 1
                                CellRange xCell = sheet.Range[r, 1];
                                TableCell wCell = table.Rows[r - 1].Cells[0];
                                //Fill data to Word table
                                TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                //Copy the formatting of table to Word
                                CopyStyle(textRange, xCell, wCell);
                                for (int c = 2; c <= ColumnList.Count - numcol + 1; c++)
                                {
                                    int c2 = ColumnList[numcol + c - 2];
                                    xCell = sheet.Range[r, c2];
                                    wCell = table.Rows[r - 1].Cells[c - 1];
                                    //Fill data to Word table
                                    textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                    //Copy the formatting of table to Word
                                    CopyStyle(textRange, xCell, wCell);
                                }
                            }
                            //Set column width of Word table in Word
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                                {
                                    table.Rows[i].Cells[j].Width = 100f;
                                }
                            }

                            //NoBreakTable(table);
                        }
                        else // more than 2 part
                        {
                            double value = (double)(ColumnList.Count - numcol) / (numcol - 1);
                            double nf    = Math.Ceiling(value) + 1;
                            int    n     = (int)nf;

                            //Part 1 of n
                            title      = section1.AddParagraph();
                            title.Text = item.Name + $" Part 1 of {n}";
                            title.ApplyStyle(style.Name);

                            //Write Table
                            table = section1.AddTable(true);
                            table.ResetCells(sheet.LastRow, numcol);
                            if (!boolResult)
                            {
                                table.ApplyHorizontalMerge(0, 0, (numcol - 1));
                            }
                            //Traverse the rows and columns of table in worksheet and get the cells, call a custom function CopyStyle() to copy the font style and cell style from Excel to Word table.
                            for (int r = 1; r <= sheet.LastRow; r++)
                            {
                                for (int c = 1; c <= numcol; c++)
                                {
                                    int       c2    = ColumnList[c - 1];
                                    CellRange xCell = sheet.Range[r, c2];
                                    TableCell wCell = table.Rows[r - 1].Cells[c - 1];
                                    //Fill data to Word table
                                    TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                    //Copy the formatting of table to Word
                                    CopyStyle(textRange, xCell, wCell);
                                }
                            }
                            //Set column width of Word table in Word
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                                {
                                    table.Rows[i].Cells[j].Width = 100f;
                                }
                            }

                            //NoBreakTable(table);

                            //Part x of n (2 <= x <= n-1)
                            for (int i = 2; i < n; i++)
                            {
                                title      = section1.AddParagraph();
                                title.Text = item.Name + $" Part {i} of {n}";
                                title.ApplyStyle(style.Name);

                                //Write Table
                                table = section1.AddTable(true);
                                table.ResetCells(sheet.LastRow, numcol); // first column and the rest
                                if (!boolResult)
                                {
                                    table.ApplyHorizontalMerge(0, 0, (numcol - 1));
                                }
                                //Traverse the rows and columns of table in worksheet and get the cells, call a custom function CopyStyle() to copy the font style and cell style from Excel to Word table.
                                for (int r = 1; r <= sheet.LastRow; r++)
                                {
                                    //for c = 1
                                    CellRange xCell = sheet.Range[r, 1];
                                    TableCell wCell = table.Rows[r - 1].Cells[0];
                                    //Fill data to Word table
                                    TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                    //Copy the formatting of table to Word
                                    CopyStyle(textRange, xCell, wCell);
                                    for (int c = 2; c <= numcol; c++)
                                    {
                                        int c2 = ColumnList[numcol + (i - 2) * (numcol - 1) + c - 2];
                                        xCell = sheet.Range[r, c2];
                                        wCell = table.Rows[r - 1].Cells[c - 1];
                                        //Fill data to Word table
                                        textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                        //Copy the formatting of table to Word
                                        CopyStyle(textRange, xCell, wCell);
                                    }
                                }

                                //Set column width of Word table in Word
                                for (int it = 0; it < table.Rows.Count; it++)
                                {
                                    for (int j = 0; j < table.Rows[it].Cells.Count; j++)
                                    {
                                        table.Rows[it].Cells[j].Width = 100f;
                                    }
                                }

                                //NoBreakTable(table);
                            }


                            //Part n of n
                            title      = section1.AddParagraph();
                            title.Text = item.Name + $" Part {n} of {n}";
                            title.ApplyStyle(style.Name);

                            //Write Table
                            table = section1.AddTable(true);
                            int lastcol = ColumnList.Count - (numcol + (n - 2) * (numcol - 1));
                            table.ResetCells(sheet.LastRow, lastcol + 1); // first column and the rest
                            if (!boolResult)
                            {
                                table.ApplyHorizontalMerge(0, 0, lastcol);
                            }
                            //Traverse the rows and columns of table in worksheet and get the cells, call a custom function CopyStyle() to copy the font style and cell style from Excel to Word table.
                            for (int r = 1; r <= sheet.LastRow; r++)
                            {
                                //for c = 1
                                CellRange xCell = sheet.Range[r, 1];
                                TableCell wCell = table.Rows[r - 1].Cells[0];
                                //Fill data to Word table
                                TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                //Copy the formatting of table to Word
                                CopyStyle(textRange, xCell, wCell);
                                for (int c = 2; c <= lastcol + 1; c++)
                                {
                                    int c2 = ColumnList[numcol + (n - 2) * (numcol - 1) + c - 2];
                                    xCell = sheet.Range[r, c2];
                                    wCell = table.Rows[r - 1].Cells[c - 1];
                                    //Fill data to Word table
                                    textRange = wCell.AddParagraph().AppendText(xCell.NumberText);
                                    //Copy the formatting of table to Word
                                    CopyStyle(textRange, xCell, wCell);
                                }
                            }
                            //Set column width of Word table in Word
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                                {
                                    table.Rows[i].Cells[j].Width = 100f;
                                }
                            }

                            //NoBreakTable(table);
                        }
                        section1.AddParagraph().Text = ""; //Buffer
                        //continue;
                        break;
                    }
                    iter++;
                }
            }
        }
コード例 #15
0
        //导出按钮点击事件
        private void button2_Click(object sender, EventArgs e)
        {
            int num = 0;

            if (textBox2.Text != "")
            {
                num = Convert.ToInt32(textBox2.Text);
            }

            //创建Word文档
            Document doc = new Document();
            //添加section
            Section section = doc.AddSection();

            //添加表格
            Table table = section.AddTable(true);

            //添加第1行
            TableRow  row1  = table.AddRow();
            TableCell cell1 = row1.AddCell();

            cell1.AddParagraph().AppendText("题 号");
            cell1.CellFormat.BackColor = Color.Gold;
            TableCell cell2 = row1.AddCell();

            cell2.AddParagraph().AppendText("题 目");

            TableCell cell3 = row1.AddCell();

            cell3.AddParagraph().AppendText("题 号");
            cell3.CellFormat.BackColor = Color.Gold;
            TableCell cell4 = row1.AddCell();

            cell4.AddParagraph().AppendText("题 目");

            TableCell cell5 = row1.AddCell();

            cell5.AddParagraph().AppendText("题 号");
            cell5.CellFormat.BackColor = Color.Gold;
            TableCell cell6 = row1.AddCell();

            cell6.AddParagraph().AppendText("题 目");

            TableCell cell7 = row1.AddCell();

            cell7.AddParagraph().AppendText("题 号");
            cell7.CellFormat.BackColor = Color.Gold;
            TableCell cell8 = row1.AddCell();

            cell8.AddParagraph().AppendText("题 目");

            int j = 0;

            for (int i = 0; i < num / 4 + 1; i++)
            {
                TableRow row = table.AddRow();
                j++;
                if (j < num + 1)
                {
                    table.Rows[i + 1].Cells[0].AddParagraph().AppendText(j.ToString());
                    table.Rows[i + 1].Cells[0].CellFormat.BackColor = Color.Gold;
                    table.Rows[i + 1].Cells[1].AddParagraph().AppendText(shizi[j - 1]);
                }
                j++;
                if (j < num + 1)
                {
                    table.Rows[i + 1].Cells[2].AddParagraph().AppendText(j.ToString());
                    table.Rows[i + 1].Cells[2].CellFormat.BackColor = Color.Gold;
                    table.Rows[i + 1].Cells[3].AddParagraph().AppendText(shizi[j - 1]);
                }

                j++;
                if (j < num + 1)
                {
                    table.Rows[i + 1].Cells[4].AddParagraph().AppendText(j.ToString());
                    table.Rows[i + 1].Cells[4].CellFormat.BackColor = Color.Gold;
                    table.Rows[i + 1].Cells[5].AddParagraph().AppendText(shizi[j - 1]);
                }
                j++;
                if (j < num + 1)
                {
                    table.Rows[i + 1].Cells[6].AddParagraph().AppendText(j.ToString());
                    table.Rows[i + 1].Cells[6].CellFormat.BackColor = Color.Gold;
                    table.Rows[i + 1].Cells[7].AddParagraph().AppendText(shizi[j - 1]);
                }
            }
            //保存文档
            table.AutoFit(AutoFitBehaviorType.FixedColumnWidths);
            doc.SaveToFile("Table.docx");
            MessageBox.Show("导出成功!");
        }