NET Core导出word文档(表格循环插入)

本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正。

在工程中引用word的动态库

在项目中,点击项目名称右键-->管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可,如下图所示:

NET Core导出word文档(表格循环插入)

 

 

涉及的知识点

 

  1. _Application: 表示word应用程序的接口,对应的实现类是Application类。
  2. _Document:表示一个word文档,通过_Application对应的文档接口进行创建。
  3. Paragraph:表示一个段落,通过_Document对象的相关方法进行创建。
  4. Table:表示一个表格,通过_Document对象的相关方法进行创建。
  5. Range:表示一个区域,可以是一个段落,也可以是一个表格,也可以是一个单元格,可以Range.select()将光标移动到当前区域。
  6. 移动焦点:wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);//移动焦点

生成文档效果图

NET Core导出word文档(表格循环插入)

 

 

核心代码:

NET Core导出word文档(表格循环插入)
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ETWord
{
    public class WordHelper
    {
        public static void CreateWordFile(string filePath)
        {
            
            try
            {
                CreateFile(filePath);
                //
                MessageFilter.Register();
                object wdLine = WdUnits.wdLine;//一个线段
                object oMissing = Missing.Value;//表示这个参数可以传入缺省值
                object fileName = filePath;
                object heading2 = WdBuiltinStyle.wdStyleHeading2;//标题一
                object heading3 = WdBuiltinStyle.wdStyleHeading3;//标题二
                
                _Application wordApp = new Application();//创建一个word进程
                wordApp.Visible = true;//word进程是否可见
                _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);//打开word进程
                System.Data.DataTable dtDepts = DatabaseHelper.getDept();//获取大标题
                int ii = 0;
                foreach (DataRow dr in dtDepts.Rows)
                {
                    string dept = dr["dept"].ToString();//标题一
                    Paragraph oPara0 = wordDoc.Content.Paragraphs.Add(ref oMissing);//创建前三行表格
                    oPara0.Range.Text = string.Format("{0}-{1}", ii + 1, dept);//设置第二标题的序号
                    oPara0.Range.Select();
                    oPara0.set_Style(ref heading2);//设置样式
                    oPara0.Range.InsertParagraphAfter();//在范围之后插入段落标记
                    System.Data.DataTable dtTemplate = DatabaseHelper.getTemplateByDept(dept);//循环前三行
                    int jj = 0;
                    foreach (DataRow dr1 in dtTemplate.Rows)
                    {
                        string template = dr1["template"].ToString();//创建模板时设置的
                        string user1 = dr1["user1"].ToString();//创建模板时设置的
                        string remark = dr1["remark"].ToString();//创建模板时设置的
                        System.Data.DataTable dtData = DatabaseHelper.getDataByDeptAndTemplate(dept, template);//获取数据
                        int count = dtData.Rows.Count;//获取集合中总数量
                        int row = count + 4;//行数
                        int column = 5;
                        object ncount = 1;

                        wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);
                        wordApp.Selection.TypeParagraph();//插入一个新空白段落
                        Paragraph oPara1 = wordDoc.Content.Paragraphs.Add(ref oMissing);//创建第二个表格
                        oPara1.Range.Select();
                        oPara1.Range.Text = string.Format("{0}-{1}、{2}", ii + 1, jj + 1, template);//标题二
                        //oPara1.Range.Font.Bold = 1;
                        //oPara1.Format.SpaceAfter = 5;
                        oPara1.set_Style(ref heading3);//设置样式
                        oPara1.Range.InsertParagraphAfter();//在范围之后插入段落标记
                        wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);
                        wordApp.Selection.TypeParagraph();//插入一个新空白段落
                        //设置表格
                        Table table = wordDoc.Tables.Add(wordApp.Selection.Range, row, column, ref oMissing, ref oMissing);//创建新表格
                       
                        table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//外边框单实线
                        table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;//内边框单实线
                        table.Range.Font.Bold = 0;//字体粗细
                        table.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthAuto;//设置表格宽度,基于当前所选内容自动选择要使用的度量单位。
                        table.Columns[1].Width = 60f;
                        table.Columns[2].Width = 100f;
                        table.Columns[3].Width = 100f;
                        table.Columns[4].Width = 60f;
                        table.Columns[5].Width = 100f;
                        //列的合并
                        Cell cell = table.Cell(1, 2);
                        cell.Merge(table.Cell(1, 5));
                        Cell cell2 = table.Cell(2, 2);
                        cell2.Merge(table.Cell(2, 5));
                        Cell cell3 = table.Cell(3, 2);
                        cell3.Merge(table.Cell(3, 5));
                        //赋值
                        table.Cell(1, 1).Range.Text = "流程名称:";
                        table.Cell(2, 1).Range.Text = "使用人:";
                        table.Cell(3, 1).Range.Text = "流程说明:";
                        table.Cell(4, 1).Range.Text = "节点";
                        table.Cell(4, 2).Range.Text = "节点名";
                        table.Cell(4, 3).Range.Text = "处理人员";
                        table.Cell(4, 4).Range.Text = "处理方式";
                        table.Cell(4, 5).Range.Text = "跳转信息";
                        table.Cell(1, 2).Range.Text = template;//把值赋给第一行第二列表格
                        table.Cell(2, 2).Range.Text = user1;//把值赋给第二行第二列表格
                        table.Cell(3, 2).Range.Text = remark;//把值赋给第三行第二列表格
                        int kk = 5;
                        foreach (DataRow dr2 in dtData.Rows)
                        {
                            table.Cell(kk, 1).Range.Text = (kk - 4).ToString();
                            table.Cell(kk, 2).Range.Text = dr2["NodeName"].ToString();
                            table.Cell(kk, 3).Range.Text = dr2["DoName"].ToString();
                            table.Cell(kk, 4).Range.Text = dr2["DoType"].ToString();
                            table.Cell(kk, 5).Range.Text = string.Empty;
                            kk++;
                        }
                        table.Cell(kk - 1, 5).Range.Select();

                        wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);//移动焦点
                        wordApp.Selection.TypeParagraph();//插入段落

                        jj++;
                    }
                    ii++;
                }

                //保存文档
                wordDoc.Save();
                wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                MessageFilter.Revoke();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);

            }
        }

        private static void CreateFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                using (FileStream fs = File.Create(filePath))
                {

                }
            }
        }
    }
}
View Code

操作数据库,模板的方法类:

NET Core导出word文档(表格循环插入)
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETWord
{
    public class DatabaseHelper
    {
        /// <summary>
        /// 获取部门
        /// </summary>
        /// <returns></returns>
        public static DataTable getDept()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("dept");
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dt.NewRow();
                dr["dept"] = string.Format("部门_{0}_T", i + 1);
                dt.Rows.Add(dr);
            }
            return dt;
        }

        /// <summary>
        /// 获取模板
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        public static DataTable getTemplateByDept(string dept)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("template");
            dt.Columns.Add("user1");
            dt.Columns.Add("remark");
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dt.NewRow();
                dr["template"] = string.Format("小组_{0}_A_{1}", i + 1,dept);
                dr["user1"] = string.Format("B_{0}_B_{1}", i + 1, dept);
                dr["remark"] = string.Format("C_{0}_C_{1}", i + 1, dept);
                dt.Rows.Add(dr);
            }
            return dt;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public static DataTable getDataByDeptAndTemplate(string dept, string template)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("NodeName");
            dt.Columns.Add("DoName");
            dt.Columns.Add("DoType");
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dt.NewRow();
                dr["NodeName"] = string.Format("AA_{0}_{1}", i,template);
                dr["DoName"] = string.Format("BB_{0}", i);
                dr["DoType"] = string.Format("CC_{0}",  i);
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
}
View Code NET Core导出word文档(表格循环插入)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ETWord
{
    public class MessageFilter : IOleMessageFilter
    {
        //
        // Class containing the IOleMessageFilter
        // thread error-handling functions.

        // Start the filter.
        public static void Register()
        {
            IOleMessageFilter newFilter = new MessageFilter();
            IOleMessageFilter oldFilter = null;
            CoRegisterMessageFilter(newFilter, out oldFilter);
        }

        // Done with the filter, close it.
        public static void Revoke()
        {
            IOleMessageFilter oldFilter = null;
            CoRegisterMessageFilter(null, out oldFilter);
        }

        //
        // IOleMessageFilter functions.
        // Handle incoming thread requests.
        int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
        {
            //Return the flag SERVERCALL_ISHANDLED.
            return 0;
        }

        // Thread call was rejected, so try again.
        int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
        {
            if (dwRejectType == 2)
            // flag = SERVERCALL_RETRYLATER.
            {
                // Retry the thread call immediately if return >=0 & 
                // <100.
                return 99;
            }
            // Too busy; cancel call.
            return -1;
        }

        int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
        {
            //Return the flag PENDINGMSG_WAITDEFPROCESS.
            return 2;
        }

        // Implement the IOleMessageFilter interface.
        [DllImport("Ole32.dll")]
        private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
    }

    [ComImport(), Guid("00000016-0000-0000-C000-000000000046"),
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    interface IOleMessageFilter
    {
        [PreserveSig]
        int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);

        [PreserveSig]
        int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);

        [PreserveSig]
        int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
    }
}
View Code

 

上一篇:关于STATUS_FLOAT_UNDERFLOW(0xC0000093)异常


下一篇:100 个 Python 小例子(练习题)