1. 程式人生 > 其它 >NET Core匯出word文件(表格迴圈插入)

NET Core匯出word文件(表格迴圈插入)

本文以一個簡單的小例子,簡述利用C#語言開發word表格相關的知識,僅供學習分享使用,如有不足之處,還請指正。

在工程中引用word的動態庫

在專案中,點選專案名稱右鍵-->管理NuGet程式包,開啟NuGet包管理器視窗,進行搜尋下載即可,如下圖所示:

涉及的知識點

  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);//移動焦點

生成文件效果圖

核心程式碼:

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

操作資料庫,模板的方法類:

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
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