Winform创建doc文档(一)

这篇博客介绍了如何在Winform应用中通过C#创建doc文档。在前台按钮点击事件中,实现了保存文件的功能。关键步骤包括创建WordHelper类以及正确引入和配置Microsoft.Office.Interop.Word库,解决嵌入互操作类型的报错问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台点击按钮事件,保存文件地址,如下:

private void button1_Click(object sender, EventArgs e)
{
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = string.Format("T{0}.doc", 
             DateTime.Now.ToString("yyyyMMddHHmmss"));
            sfd.Filter = "word文档|*.doc";
            if (DialogResult.OK == sfd.ShowDialog())
            {
                string filePath = sfd.FileName;
                WordHelper.CreateWordFile(filePath);
            }
}

建立类WordHelper,代码:

using MSWord=Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WindowsFormsApp1
{
    public class WordHelper
    {
        public static void CreateWordFile(string filePath)
        {
            try
            {
                CreateFile(filePath);
                object wdLine = WdUnits.wdLine;
                object oMissing = Missing.Value;
                object fileName = filePath;
                object heading1 = WdBuiltinStyle.wdStyleHeading1;
                object heading2 = WdBuiltinStyle.wdStyleHeading2;
                object heading3 = WdBuiltinStyle.wdStyleHeading3;
                object heading4 = WdBuiltinStyle.wdStyleHeading4;
                object heading5 = WdBuiltinStyle.wdStyleHeading5;
                object heading6 = WdBuiltinStyle.wdStyleHeading6;
                object heading7 = WdBuiltinStyle.wdStyleHeading7;
                object heading8 = WdBuiltinStyle.wdStyleHeading8;
                //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
                object missing = Missing.Value;
                string strContent = "";
                _Application wordApp = new Application();
                wordApp.Visible = true;
                _Document wordDoc = wordApp.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                List<data> datas = new List<data>() {
                        new data(){ADDVCDName="孙悟空",Avg="6.0",Maxname="桀王舍",Max="60.4"},
                        new data(){ADDVCDName="孙悟空",Avg="6.0",Maxname="桀王舍",Max="60.4"},
                         new data(){ADDVCDName="孙悟空",Avg="6.0",Maxname="桀王舍",Max="60.4"},
                         new data(){ADDVCDName="孙悟空",Avg="6.0",Maxname="桀王舍",Max="60.4"},
                         new data(){ADDVCDName="孙悟空",Avg="6.0",Maxname="桀王舍",Max="60.4"},
                         new data(){ADDVCDName="孙悟空",Avg="6.0",Maxname="桀王舍",Max="60.4"},
                         new data(){ADDVCDName=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值