在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 写入Word模版数据(需安装office)

C# 写入Word模版数据(需安装office)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.16M
  • 下载次数:72
  • 浏览次数:385
  • 发布时间:2019-07-08
  • 实例类别:C#语言基础
  • 发 布 人:wfdsfafsd
  • 文件格式:.zip
  • 所需积分:1
 相关标签: Word 模版

实例介绍

第一步,制作模板
1.新建一个文档,设置文档内容。
2.在相应位置插入书签;将鼠标定位到要插入书签的位置,点击“插入”>“书签”,弹出对话框,输入书签名,点击“添加”按钮。
3.保存模板,命名为“模板1.dot”或者“模板1.doc”
第二步,设置项目中的引用
1.右击“解决方案资源管理器”中的项目目录下的“引用”,选择“添加引用”,打开“添加引用”对话框
2.在“添加引用”对话框中,选择“COM”>“Microsoft Word 11.0 Object Library”,点击“确定”按钮
3.相同操作打开“添加引用”对话框中,选择“浏览”项,查找到”Microsoft.Office.Interop.Word.dll”文件,选中它,点击“确定”按钮
注意:此处要查找的“Microsoft.Office.Interop.Word.dll”版本必须为“11.*.*.*”,“*”代表数字
第三步,编码
这一步分成两个部分
第一部分,Report类的编码
这部分我已经封装好,为文件“Report.cs”,可以直接使用



第二部分,具体生成文档的编码
代码见下文:
 
1.首先需要载入模板
Report report =new Report();
report.CreateNewDocument(TemPath); //模板路径
 
2.插入一个值
report.InsertValue("Bookmark_value","世界杯");//在书签“Bookmark_value”处插入值
 
3.创建一个表格
Table table =report.InsertTable("Bookmark_table", 2, 3, 0); //在书签“Bookmark_table”处插入2行3列行宽最大的表
 
4.合并单元格
report.MergeCell(table, 1, 1, 1, 3); //表名,开始行号,开始列号,结束行号,结束列号
 
5.表格添加一行
report.AddRow(table); //表名
 
6.在单元格中插入值
report.InsertCell(table, 2, 1,"R2C1");//表名,行号,列号,值
 
7.设置表格中文字的对齐方式
report.SetParagraph_Table(table, -1, 0);//水平方向左对齐,垂直方向居中对齐
 
8.设置表格字体
report.SetFont_Table(table,"宋体", 9);//宋体9磅
 
9.给现有的表格添加一行
report.AddRow(1);//给模板中第一个表格添加一行
 
10.确定现有的表格是否使用边框
report.UseBorder(1,true); //模板中第一个表格使用实线边框
 
11.给现有的表格添加多行
report.AddRow(1, 2);//给模板中第一个表格插入2行
 
12.给现有的表格插入一行数据
string[] values={"英超", "意甲", "德甲","西甲", "法甲" };
report.InsertCell(1, 2, 5,values); //给模板中第一个表格的第二行的5列分别插入数据
 
13.插入图片
string picturePath = @"C:\Documents and Settings\Administrator\桌面\1.jpg";
report.InsertPicture("Bookmark_picture",picturePath, 150, 150); //书签位置,图片路径,图片宽度,图片高度
 
14.插入一段文字
string text = "长期从事电脑操作者,应多吃一些新鲜的蔬菜和水果,同时增加维生素A、B1、C、E的摄入。为预防角膜干燥、眼干涩、视力下降、甚至出现夜盲等,电 脑操作者应多吃富含维生素A的食物,如豆制品、鱼、牛奶、核桃、青菜、大白菜、空心菜、西红柿及新鲜水果等。";
report.InsertText("Bookmark_text",text);
 
15.最后保存文档
report.SaveDocument(RepPath); //文档路径
 

第四步,运行程序生成文档,并查看生成的文档


【实例截图】

from clipboard


【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSWord = Microsoft.Office.Interop.Word;

namespace ExportWordTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        MSWord.Application wordApp = null;
        MSWord.Document wordDoc = null;
        object nothing = System.Reflection.Missing.Value;

        private void button1_Click(object sender, EventArgs e)
        {
            doExport();
        }

        private void doExport()
        {
            try
            {
                wordApp = new MSWord.ApplicationClass();
                wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);

                doPrintTable("shili.jpg");
                doPrintTable("shili1.jpg");
                //doPrintTable();

                //wordApp.Visible = true;

                object filename = System.IO.Directory.GetCurrentDirectory()   @"\测试.docx";
                wordDoc.SaveAs2(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, ref nothing);
                wordDoc.Close(ref nothing, ref nothing, ref nothing);
                wordApp.Quit(ref nothing, ref nothing, ref nothing);

                MessageBox.Show("导出Word成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出Word错误"   ex.ToString());
            }
        }

        private void doPrintTable(string t)
        {
            try
            {
                wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                //wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//设置字体居中
                wordApp.Selection.Font.Size = 20;//设置字体大小
                wordApp.Selection.Font.Bold = 2;//设置字体为粗体
                wordApp.Selection.TypeText("设备(设施)标识牌");
                wordDoc.Paragraphs.Add(ref nothing);
                wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight;
                wordApp.Selection.Font.Size = 15;
                wordApp.Selection.Font.Bold = 0;
                wordApp.Selection.TypeText("南通帝人");
                
                wordApp.Selection.TypeParagraph();//换行
                wordApp.Selection.Font.Size = 12;
                wordApp.Selection.Font.Bold = 0;

                //8表示8行,3表示3列
                MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, 8, 3, ref nothing, ref nothing);
                table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;//设置表格边框样式
                table.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;

                table.Columns[1].Width = 80f;
                table.Columns[2].Width = 200f;
                table.Columns[3].Width = 120f;
                table.Rows.Alignment = MSWord.WdRowAlignment.wdAlignRowCenter;//表格整体居中
                wordApp.Selection.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //第一行
                table.Cell(1, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(1, 1).Range.Text = "设备名称";
                table.Cell(1, 2).Merge(table.Cell(1, 3));//合并单元格
                table.Cell(1, 2).Range.Text = "ABC干粉灭火器SB03324";
                table.Cell(1, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;


                //第二行
                table.Cell(2, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(2, 1).Range.Text = "使用位置";
                table.Cell(2, 2).Merge(table.Cell(2, 3));
                table.Cell(2, 2).Range.Text = "帝人宿舍区六楼楼梯口";
                table.Cell(2, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;

                //第三行
                table.Cell(3, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(3, 1).Range.Text = "型号规格";
                table.Cell(3, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                table.Cell(3, 2).Range.Text = "";
                table.Cell(3, 3).Merge(table.Cell(8, 3));
                table.Cell(3, 3).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(3, 3).Select();//纵向合并单元格
                object LinkOfFile = false;
                object SaveDocument = true;
                object range = wordApp.Selection.Range;
                string pictureFileName = System.IO.Directory.GetCurrentDirectory()   @"\"   t;
                table.Cell(3, 3).Range.InlineShapes.AddPicture(pictureFileName, ref LinkOfFile, ref SaveDocument, ref range);


                table.Cell(3, 3).Range.InlineShapes[1].Width = 100f;
                table.Cell(3, 3).Range.InlineShapes[1].Height = 100f;
                //MSWord.Shape s = table.Cell(3, 3).Range.InlineShapes[1].ConvertToShape();
                //s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare;//四周环绕

                //第四行
                table.Cell(4, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(4, 1).Range.Text = "启用日期";
                table.Cell(4, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                table.Cell(4, 2).Range.Text = "";

                //第四行
                table.Cell(5, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(5, 1).Range.Text = "物理编码";
                table.Cell(5, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                table.Cell(5, 2).Range.Text = "";

                //第四行
                table.Cell(6, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(6, 1).Range.Text = "产权单位";
                table.Cell(6, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                table.Cell(6, 2).Range.Text = "帝人";

                //第四行
                table.Cell(7, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(7, 1).Range.Text = "维保单位";
                table.Cell(7, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                table.Cell(7, 2).Range.Text = "江苏万友消防工程有限公司";

                //第四行
                table.Cell(8, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                table.Cell(8, 1).Range.Text = "维修电话";
                table.Cell(8, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                table.Cell(8, 2).Range.Text = "400-0820-119";


                wordApp.Selection.EndKey(MSWord.WdUnits.wdStory);
                wordApp.Selection.TypeParagraph();//换行
            }
            catch (Exception ex)
            {
                MessageBox.Show("在word文档中生成表格异常:"   ex.ToString());
            }
        }


    }
}



标签: Word 模版

网友评论

第 1 楼 a147qq123 发表于: 2022-12-27 15:15 44
兄弟你实际的代码和实例介绍不一致啊。。。

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警