你好,游客 登录 注册 搜索
背景:
阅读新闻

利用Aspose.Word控件实现Word文档的操作

[日期:2013-10-25] 来源:Linux社区  作者:wuhuacong [字体:大 中 小]

2、单元格合并的操作

常见的Word文件或者Excel文件中,都经常看到合并单元格的内容,因此这个部分也是非常常见的操作,必须掌握。

我们先看一个例子代码及效果。

 
                try
                {
                    Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);
                    Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

                    builder.InsertCell();
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                    builder.CellFormat.VerticalMerge = CellMerge.First;
                    builder.Write("Text in merged cells.");

                    builder.InsertCell();
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                    builder.CellFormat.VerticalMerge = CellMerge.None;
                    builder.Write("Text in one cell");
                    builder.EndRow();

                    builder.InsertCell();
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                    // This cell is vertically merged to the cell above and should be empty.
                    builder.CellFormat.VerticalMerge = CellMerge.Previous;

                    builder.InsertCell();
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                    builder.CellFormat.VerticalMerge = CellMerge.None;
                    builder.Write("Text in another cell");
                    builder.EndRow();

                    doc.Save(saveDocFile);
                    if (MessageUtil.ShowYesNoAndTips("保存成功,是否打开文件?") == System.Windows.Forms.DialogResult.Yes)
                    {
                        System.Diagnostics.Process.Start(saveDocFile);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                    return;
                }
 

他的效果如下

关于合并单元格的介绍,你还可以参考下这篇官方介绍:http://www.aspose.com/docs/display/wordsnet/Working+with+Merged+Cells

如果上面的例子还不够明白,OK,我在介绍一个实际的例子,来说明合并单元格的操作模式。

实际文档生成如下所示:

文档的模板如下所示:

其实这个里面的“测试”内容是使用代码写入的,其实就是一行业务数据,用两行来展示,其中有些合并的单元格,这是一个实际项目的表格形式。我们注意到,每行有13个单元格,其中第一、第二、第十三列是合并列。和并列有一个特点,就是它的两个索引都有效,不过只是能使用第一个索引来对它进行操作复制,利用第二个没有用处的。

如第一个列是和并列,它应该有0、13这样的索引,第二列也是和并列,它也有1、14的索引,其他的类推。

了解这样的逻辑关系后,我们看实际操作的代码如下所示。

 
                try
                {
                    Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);
                    Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
                    
                    List<double> widthList = new List<double>();
                    for (int i = 0; i < 13; i++)
                    {
                        builder.MoveToCell(0, 2, i, 0); //移动单元格
                        double width = builder.CellFormat.Width;//获取单元格宽度
                        widthList.Add(width);
                    }

                    builder.MoveToBookmark("table");        //开始添加值

                    Table table = builder.StartTable();
                    builder.RowFormat.HeadingFormat = true;
                    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

                    for (int j = 0; j < 26; j++)
                    {
                        builder.InsertCell();// 添加一个单元格                    
                        builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                        builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                        int cellIndex = (j > 12) ? (j-13) : j; //位于第几个单元格
                        builder.CellFormat.Width = widthList[cellIndex];
                        builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
                        builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐

                        builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
                        if (cellIndex == 0 || cellIndex == 1 || cellIndex == 12)
                        {
                            if (j > 12)
                            {
                                builder.CellFormat.VerticalMerge = CellMerge.Previous;
                            }
                            else
                            {
                                builder.CellFormat.VerticalMerge = CellMerge.First;
                            }
                        }
 
                        builder.Write("测试" + j.ToString());
                        if (cellIndex == 12 )
                        {
                            builder.EndRow();
                        }
                    }
                    builder.EndTable();

                    doc.Save(saveDocFile);
                    if (MessageUtil.ShowYesNoAndTips("保存成功,是否打开文件?") == System.Windows.Forms.DialogResult.Yes)
                    {
                        System.Diagnostics.Process.Start(saveDocFile);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                    return;
                }
linux
相关资讯       Aspose.Word 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款