s
o
u
l
s
j
i
e

C#使用Aspose.Words将Spread表格插入到Word中

注:Word中每个表格只有一行,固定第一行为表头,先复制表头N行追加到表格后,保证每行的格式与表头一致。
封装方法:

        private void SheetToTable(FarPoint.Win.Spread.SheetView sheet, Document doc, Table table, int beginRow)
        {
            //先复制出所有行,保证每行的格式都是一致的
            for (int i = beginRow; i < sheet.RowCount; i++)
            {
                Row oneRow = (Row)table.Rows[table.Rows.Count - 1].Clone(true);
                table.Rows.Insert(table.Rows.Count, oneRow);
            }
            //从第二行开始进行数据填充
            for (int i = beginRow; i < table.Rows.Count; i++)
            {
                //先统一设置一下行高,后续给单元格赋值时会自动撑开
                table.Rows[i].RowFormat.Height = 20;
                for (int j = 0; j < sheet.ColumnCount; j++)
                {
                    table.Rows[i].Cells[j].ChildNodes.Clear();
                    Paragraph p = new Paragraph(doc);
                    p.AppendChild(new Run(doc, sheet.Cells[i, j].Text));
                    table.Rows[i].Cells[j].AppendChild(p);
                }
            }
        }

  

        /// <summary>
        /// 设置指定单元格的文本
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="cell"></param>
        /// <param name="text"></param>
        public static void SetCellText(Document doc,Cell cell,string text) 
        {
            cell.RemoveAllChildren();
            cell.Paragraphs.Add(new Paragraph(doc));
            Run run = new Run(doc, text);
            run.Font.Name = "宋体";
            run.Font.Size = 8;
            cell.LastParagraph.Runs.Add(run);
        }

  

posted @ 2023-02-23 10:57  soulsjie  阅读(197)  评论(0编辑  收藏  举报
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43