当前位置:   article > 正文

C# NPOI Word操作_npoi操作word

npoi操作word

简述

本文章中NPOI版本为2.5.3.0。

页面设置


  1. void main()
  2. {
  3. XWPFDocument document = new XWPFDocument();
  4. document.Document.body.sectPr = new CT_SectPr();
  5. var sectPr = document.Document.body.sectPr;
  6. SetPageSz(sectPr.pgSz,"A4", ST_PageOrientation.portrait);//A4纵向
  7. SetPageMar(sectPr.pgMar, "Medium");//中等边框
  8. }
  9. void SetPageSz(CT_PageSz size, string pageSz, ST_PageOrientation orientation)
  10. {
  11. //这里的单位是缇,1英寸=1440缇,1英寸=2.54cm,1cm=1440÷2.54≈567缇
  12. //A4纸的物理尺寸是210×297mm,
  13. //210mm=210×144÷2.54≈11905.51181102362
  14. //297mm=297×144÷2.54≈16837.79527559055
  15. size.orient = orientation;
  16. switch (pageSz)//默认A4
  17. {
  18. case "A3": size.w = 16838; size.h = 23811; break;//297x420mm
  19. case "A4": size.w = 11906; size.h = 16838; break;//210x297mm
  20. case "A5": size.w = 8391; size.h = 11906; break;//148x210mm
  21. }
  22. if (orientation != ST_PageOrientation.portrait)
  23. {
  24. (size.h, size.w) = (size.w, size.h);
  25. }
  26. }
  27. void SetPageMar(CT_PageMar margin, string pageMar)
  28. {
  29. switch (type)
  30. {
  31. case "Normal": m.left = m.right = 1800; m.top = m.bottom = "1440"; break;
  32. case "Narrow": m.left = m.right = 720; m.top = m.bottom = "720"; break;
  33. case "Medium": m.left = m.right = 1080; m.top = m.bottom = "1440"; break;
  34. case "Broad": m.left = m.right = 2880; m.top = m.bottom = "1440"; break;
  35. case "Symmetrical": m.left = 1800; m.right = 1440; m.top = m.bottom = "1440"; break;
  36. }
  37. m.header = 851;//页面设置-布局-页眉和页脚-页眉
  38. m.footer = 992;//页面设置-布局-页眉和页脚-页脚
  39. m.gutter = 0;//页面设置-页边距-页边距-装订线
  40. }

插入一段文本

  1. //初号 42,小初 36
  2. //一号 26,小一 24
  3. //二号 22,小二 18
  4. //三号 16,小三 15
  5. //四号 14,小四 12
  6. //五号 10.5,小五9
  7. //六号 7.5,小六6.5
  8. //七号 5.5,八号 5
  9. XWPFDocument document = new XWPFDocument();
  10. var paragraph = document.CreateParagraph();
  11. var run = paragraph.CreateRun();
  12. run.IsItalic = true;
  13. run.IsBold = true;
  14. run.FontSize = 12;//小四
  15. run.FontFamily = “Arial”;
  16. run.SetColor(“#FFFFFF”);
  17. run.SetText(“Email:”);
  18. run.SetUnderline(UnderlinePatterns.Single);
  19. //FontSize这破玩意儿是int类型,如果要想使用五号、六号、小六、七号字体则必须用下面的办法
  20. decimal size = 10.5;
  21. if (size== decimal.Truncate(size))
  22. run.FontSize = (int)size;
  23. else
  24. {
  25. var ctr = run.GetCTR();
  26. CT_RPr cT_RPr = ctr.IsSetRPr() ? ctr.rPr : ctr.AddNewRPr();
  27. (cT_RPr.IsSetSz() ? cT_RPr.sz : cT_RPr.AddNewSz()).val = (ulong)(size * 2uL);
  28. }

写入文件

  1. FileStream fs = new FileStream("1.docx", FileMode.Create);
  2. document.Write(fs);
  3. fs.Close();

颜色值表

  1. public static class WordColors
  2. {
  3. //标准色
  4. public const string DarkRed = "#C00000";
  5. public const string Red = "#FF0000";
  6. public const string Orange = "#FFC000";
  7. public const string Yellow = "#FFFF00";
  8. public const string LightGreen = "#90D050";
  9. public const string Green = "#00B050";
  10. public const string LightBlue = "#00B0F0";
  11. public const string Blue = "#0070C0";
  12. public const string DarkBlue = "#002060";
  13. public const string Purple = "#7030A0";
  14. //主题色-白色
  15. public const string Back1 = "#FFFFFF";
  16. public const string Back1Sub1 = "#F2F2F2";
  17. public const string Back1Sub2 = "#D9D9D9";
  18. public const string Back1Sub3 = "#BFBFBF";
  19. public const string Back1Sub4 = "#A6A6A6";
  20. public const string Back1Sub5 = "#808080";
  21. //主题色-黑色
  22. public const string Text1 = "#000000";
  23. public const string Text1Sub1 = "#7F7F7F";
  24. public const string Text1Sub2 = "#595959";
  25. public const string Text1Sub3 = "#404040";
  26. public const string Text1Sub4 = "#262626";
  27. public const string Text1Sub5 = "#0D0D0D";
  28. //主题色-浅灰
  29. public const string Back2 = "#E7E6E6";
  30. public const string Back2Sub1 = "#D0CECE";
  31. public const string Back2Sub2 = "#AEAAAA";
  32. public const string Back2Sub3 = "#767171";
  33. public const string Back2Sub4 = "#3B3838";
  34. public const string Back2Sub5 = "#171717";
  35. //主题色-蓝灰
  36. public const string Text2 = "#44546A";
  37. public const string Text2Sub1 = "#D5DCE4";
  38. public const string Text2Sub2 = "#ACB9CA";
  39. public const string Text2Sub3 = "#B496B0";
  40. public const string Text2Sub4 = "#323E4F";
  41. public const string Text2Sub5 = "#222A35";
  42. //主题色-蓝色
  43. public const string Accent1 = "#4472C4";
  44. public const string Accent1Sub1 = "#D9E2F3";
  45. public const string Accent1Sub2 = "#B4C6E7";
  46. public const string Accent1Sub3 = "#8EAADB";
  47. public const string Accent1Sub4 = "#2F5496";
  48. public const string Accent1Sub5 = "#1F3864";
  49. //主题色-橙色
  50. public const string Accent2 = "#ED7D31";
  51. public const string Accent2Sub1 = "#FBE4D5";
  52. public const string Accent2Sub2 = "#F7CAAC";
  53. public const string Accent2Sub3 = "#F4B083";
  54. public const string Accent2Sub4 = "#C45911";
  55. public const string Accent2Sub5 = "#833C0B";
  56. //主题色-灰色
  57. public const string Accent3 = "#A5A5A5";
  58. public const string Accent3Sub1 = "#EDEDED";
  59. public const string Accent3Sub2 = "#DBDBDB";
  60. public const string Accent3Sub3 = "#C9C9C9";
  61. public const string Accent3Sub4 = "#7B7B7B";
  62. public const string Accent3Sub5 = "#525252";
  63. //主题色-金色
  64. public const string Accent4 = "#FFC000";
  65. public const string Accent4Sub1 = "#FFF2CC";
  66. public const string Accent4Sub2 = "#FFE599";
  67. public const string Accent4Sub3 = "#FFD955";
  68. public const string Accent4Sub4 = "#BF8F00";
  69. public const string Accent4Sub5 = "#806000";
  70. //主题色-蓝色
  71. public const string Accent5 = "#5B9BD5";
  72. public const string Accent5Sub1 = "#DEEAF6";
  73. public const string Accent5Sub2 = "#BDD6EE";
  74. public const string Accent5Sub3 = "#9CC2E5";
  75. public const string Accent5Sub4 = "#2E74B5";
  76. public const string Accent5Sub5 = "#1F4E79";
  77. //主题色-绿色
  78. public const string Accent6 = "#70AD47";
  79. public const string Accent6Sub1 = "#E2EFD9";
  80. public const string Accent6Sub2 = "#C5E0B3";
  81. public const string Accent6Sub3 = "#A8D08D";
  82. public const string Accent6Sub4 = "#538135";
  83. public const string Accent6Sub5 = "#385623";
  84. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/709513?site
推荐阅读
相关标签
  

闽ICP备14008679号