C# 在Word表格中插入新行(表格含合并行)

摘要:
PublicstringCreateWordFile(stringCheckedInfo){stringmessage=“”;try{ObjectNothing=System.Reflection.Missing.Value;Directory.CreateDirectory(“C:\/CNSI”);//创建文件的目录stringname=“CNSI_”+DateTime.Now.ToLongDa
public string CreateWordFile(string CheckedInfo)
        {
            string message = "";
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                Directory.CreateDirectory("C:/CNSI");  //创建文件所在目录
                string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc";
                object filename = "C://CNSI//" + name;  //文件保存路径
                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                //添加页眉
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

                //移动焦点并换行
                object count = 14;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落

                //文档中创建表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
                //设置表格样式
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTable.Columns[1].Width = 100f;
                newTable.Columns[2].Width = 220f;
                newTable.Columns[3].Width = 105f;

                //填充表格内容
                newTable.Cell(1, 1).Range.Text = "产品详细信息表";
                newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                //合并单元格
                newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

                //填充表格内容
                newTable.Cell(2, 1).Range.Text = "产品基本信息";
                newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
                //合并单元格
                newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                //填充表格内容
                newTable.Cell(3, 1).Range.Text = "品牌名称:";
                newTable.Cell(3, 2).Range.Text = "BrandName";
                //纵向合并单元格
                newTable.Cell(3, 3).Select();//选中一行
                object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object moveCount = 5;
                object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                WordApp.Selection.Cells.Merge();
                //插入图片
                string FileName = @"J:/C#/WebApplication1/images/login_3(3).gif";//图片所在路径
                object LinkToFile = false;
                object SaveWithDocument = true;
                object Anchor = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
                WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
                //将图片设置为四周环绕型
                Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

                newTable.Cell(12, 1).Range.Text = "产品特殊属性";
                newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                //在表格中增加行
                WordDoc.Content.Tables[1].Rows.Add(ref Nothing);

                WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                //文件保存
                WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                message = name + "文档生成成功,以保存到C:CNSI下";
            }
            catch
            {
                message = "文件导出异常!";
            }
            return message;
        }

以上内容转自   https://blog.csdn.net/lj102800/article/details/6106451

免责声明:文章转载自《C# 在Word表格中插入新行(表格含合并行)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python:Python 自动化测试框架 unittest 和 pytest 对比深入浅出全面解析RDMA下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Excel VBA 根据下拉框单元格的值来改变另一个下拉框单元格的值

在很多的报表开发中,需要用到VBA去设置Excel的一些规则。  以下是一个根据下拉框单元格的值来给特定单元格进行赋值的代码: Private Sub Worksheet_SelectionChange(ByVal Target As Range) On Error Resume Next: Application.ScreenUpdati...

Java中使用poi导入、导出Excel

来自:http://blog.csdn.net/jerehedu/article/details/45195359 一、介绍    当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统、银行系统)。或者是:我们已经习惯用Excel打印。这样在我们实际的开发中,很多时候需要实现导入、...

Spread for Windows Forms快速入门(10)绑定到数据库

下面的教程将带你创建一个工程, 并将Spread控件绑定到一个数据库。 在这个教程中,主要的步骤为: 1. 将Spread添加到一个数据绑定工程中 2. 设置数据库连接 3. 指定要使用的数据 4. 创建数据集 5. 把Spread控件绑定到数据库 6. 通过改变单元格类型改善显示效果 将Spread添加到一个数据绑定工程中 打开一个新的Visual St...

Java 简单的excel读写操作

描述:简单的对excel的第一个Sheet表的读写操作 依赖包:apache.poi/poi-3.14、apache.poi/poi-ooxml-3.14 package excel; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel....

jqgrid 行内修改单元格内容

由于字段较多,但可供用户修改的 字段仅有一个,所有不想通过弹出对话框的方式让用户进行修改。还好jqgrid支持行内数据修改,首先要将cellEdit设为true以支持对单元格的 修改操作。这里讲cellsubmit设为clientArray,当修改完成后并不直接发送到服务器进行保存,而是先保存在table中,当用户点击保 存按钮是才向数据库发送请求更新数据...

EXCEL中,如何引用一个单元格中的数据,作为另一个单元格内容中的一部分?

https://zhidao.baidu.com/question/230715654.html 假设单元格A1值是8(该值由函数计算得出),我要在单元格B1中引用A1的值,但只是作为B1单元格内容中的一部分,即显示“该计算结果为8%”,其中的“8”就是A1的值。 B1输入公式内容="该计算结果为"&A1&"%" 更多追问追答� ...