公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C# 動態生成word文檔

admin
2025年5月19日 8:59 本文熱度 148

本文以一個簡單的小例子,簡述利用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();                 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);                 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.Font.Bold = 1;                     //oPara0.Format.SpaceAfter = 5;                     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))                 {                 }             }         }     } }


關于word開發中字體大小

滿足中文出版中使用字號作為字體大小的單位的需要,它允許用戶同時使用“號”和 “磅”作為字體大小的單位。

磅值與字號之間的換算關系如下:

word中設置寬度時,采用磅為單位,磅與厘米之間的換算關系如下:

 

備注

  1.  插入多個表格時,表格容易嵌套,主要是由于往下移動的行數不對,后來通過選中表格右下角的單元格,將光標移動到表格右下角,然后再往下移動兩行,即可解決表格嵌套的問題。

  2. 單元格合并問題,當單元格合并時,單元格的位置也隨之改變,如:水平方向第二,三兩個單元格合并,則原來的第四個單元格的坐標就會變成第三個單元格。

  3. 開發運行需要在電腦上安裝office組件,或者也可以安裝wps。

關于源碼下載鏈接https://files.cnblogs.com/files/hsiang/ETWord.rar?


閱讀原文:原文鏈接


該文章在 2025/5/19 10:05:24 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产在线一区二区三区av | 国产精品无码久久av不卡 | 国产aⅴ精品一区二区三区色成熟 | 精品无码人妻av一区二区pro | a级国产乱午夜理论片在线观看 | 国产成人高清亚洲一区久久 | 国产日韩精品视频一区二区三区 | av无码亚州不卡播放网点 | 国产99久久九九精品无码动漫 | av色伊人久久综合一区二区 | 国产成人8x人网站视频下载 | 国产91在线播放边 | 国产成人精品电影在线观看 | 国产激情一区二区三区在线hd | 国产aⅴ无码精品一区二区三区 | 国产精品亚洲五月天 | 国产精品国产亚洲精品不卡 | av鲁丝一区二区三区 | 国产亚洲综合日韩 | 成人一区二区三区漫画 | 成人免费午夜无码 | 国产精品艺校美女校花在线 | 91精品国产在热久久下载 | 高清不卡亚洲日韩av在线 | 精品人妻av无码一区二区三区 | 国产在线精品成人一区二区三区 | 91久久久久精品无嫩草影院 | 国产av熟女一 | 91福利视频免费 | 国产午夜精品自在自线专区 | 国产成人亚洲精品无码 | 国产精品男男视频一区二区三区 | 国产三级高清午夜羞羞视频 | 精品丝袜国产自在线拍小草 | 国产在线观看av直播 | 国产成人啪精品视频免费网站 | 国产三级在线播放不 | 加勒比人妻av无码不卡 | 国产在线精品一区二区三区直 | 3d动漫精品专区久久电影 | 国产精品兄妹在线观看麻豆 |