Program.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using DocumentFormat.OpenXml.Packaging;
  2. using DocumentFormat.OpenXml.Wordprocessing;
  3. using OpenXmlPowerTools;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Xml;
  12. using System.Xml.Linq;
  13. using System.Xml.Serialization;
  14. using System.Xml.Xsl;
  15. using TEAMModelOS.SDK.Helper.Common.StringHelper;
  16. using TEAMModelOS.SDK.Helper.Network.HttpHelper;
  17. namespace TEAMModelOS.Test.OfficeDoc
  18. {
  19. class Program
  20. {
  21. /// <summary>
  22. /// 对数组进行组合操作,选取selectCount个元素进行组合
  23. /// </summary>
  24. /// <param name="lsArray">即将进行组合操作的数组</param>
  25. /// <param name="selectCount">选取的元素的个数</param>
  26. static void C(List<string> lsArray, int selectCount)
  27. {
  28. int totolcount = lsArray.Count;
  29. int[] currectselect = new int[selectCount];
  30. int last = selectCount - 1;
  31. for (int i = 0; i < selectCount; i++)
  32. {
  33. currectselect[i] = i;
  34. }
  35. while (true)
  36. {
  37. for (int i = 0; i < selectCount; i++)
  38. {
  39. Console.Write(" {0} ", lsArray[currectselect[i]]);
  40. }
  41. Console.WriteLine();
  42. if (currectselect[last] < totolcount - 1)
  43. {
  44. currectselect[last]++;
  45. }
  46. else
  47. {
  48. int pos = last;
  49. while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1)
  50. {
  51. pos--;
  52. }
  53. if (pos == 0) return;
  54. currectselect[pos - 1]++;
  55. for (int i = pos; i < selectCount; i++)
  56. {
  57. currectselect[i] = currectselect[i - 1] + 1;
  58. }
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// 对数组进行全排列
  64. /// </summary>
  65. /// <param name="lsArray">要进行全排列的数组</param>
  66. /// <param name="begin">进行全排列的开始下标</param>
  67. /// <param name="end">进行全排列的结束下标</param>
  68. static void A(List<string> lsArray, int begin, int end)
  69. {
  70. if (begin == end)
  71. {
  72. for (int i = 0; i <= end; i++)
  73. Console.Write(" {0} ", lsArray[i]);
  74. Console.WriteLine();
  75. }
  76. for (int i = begin; i <= end; i++)
  77. {
  78. Swap(lsArray, begin, i);
  79. A(lsArray, begin + 1, end);
  80. Swap(lsArray, begin, i);
  81. }
  82. }
  83. /// <summary>
  84. /// 交换数组中的下标为x,y的值
  85. /// </summary>
  86. /// <param name="lsArray">该数组</param>
  87. /// <param name="x"></param>
  88. /// <param name="y"></param>
  89. static void Swap(List<string> lsArray, int x, int y)
  90. {
  91. string t = lsArray[x];
  92. lsArray[x] = lsArray[y];
  93. lsArray[y] = t;
  94. }
  95.  
  96. static void Main(string[] args)
  97. {
  98. List<string> list = new List<string>();
  99. for (int i = 0; i < 6; i++)
  100. {
  101. list.Add(i.ToString());
  102. }
  103. //C(list, 4);
  104. Console.WriteLine("---------------------");
  105. A(list, 0, 4);
  106. // wordPathStr = @"E:\document\题目模板.docx";
  107. //string s = HttpHelper.HttpGet("https://teammodelstorage.blob.core.chinacloudapi.cn/teammodelos/common/20190620/index_143520-2550.html");
  108. //for (int i = 0; i < 1000; i++) {
  109. // Console.WriteLine(DateTime.Now.ToString("yyyyMMddHHmmssfffffff"));
  110. //}
  111. //ConvertDocxToHtml(wordPathStr);
  112. //byte[] byteArray = File.ReadAllBytes(wordPathStr);
  113. //using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
  114. //{
  115. // Document document = new Document();
  116. // document.Load(doc.MainDocumentPart);
  117. // var stream = new MemoryStream();
  118. // stream.Write(byteArray, 0, (int)byteArray.Length);
  119. //doc.SaveAs(@"E:\document\1026-Weekend Acitivities-刘海荣.xml");
  120. // File.WriteAllBytes(@"E:\document\1026-Weekend Acitivities-刘海荣.xml", stream.ToArray());
  121. //}
  122. // string a = GetWordDocumentAsMathML(wordPathStr);
  123. }
  124. public static string ConvertXML(XmlDocument InputXMLDocument, string XSLTFilePath, XsltArgumentList XSLTArgs)
  125. {
  126. System.IO.StringWriter sw = new System.IO.StringWriter();
  127. XslCompiledTransform xslTrans = new XslCompiledTransform();
  128. xslTrans.Load(XSLTFilePath);
  129. xslTrans.Transform(InputXMLDocument.CreateNavigator(), XSLTArgs, sw);
  130. return sw.ToString();
  131. }
  132. public static string GetWordDocumentAsMathML(string fileName) {
  133. string officeML = string.Empty;
  134. using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, false))
  135. {
  136. string wordDocXml = doc.MainDocumentPart.Document.OuterXml;
  137. XslCompiledTransform xslTransform = new XslCompiledTransform();
  138. // The OMML2MML.xsl file is located under
  139. // %ProgramFiles%\Microsoft Office\Office15\
  140. xslTransform.Load(@"E:\document\OMML2MML.XSL");
  141. using (TextReader tr = new StringReader(wordDocXml))
  142. {
  143. // Load the xml of your main document part.
  144. using (XmlReader reader = XmlReader.Create(tr))
  145. {
  146. using (MemoryStream ms = new MemoryStream())
  147. {
  148. XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
  149. // Configure xml writer to omit xml declaration.
  150. settings.ConformanceLevel = ConformanceLevel.Fragment;
  151. settings.OmitXmlDeclaration = true;
  152. XmlWriter xw = XmlWriter.Create(ms, settings);
  153. // Transform our OfficeMathML to MathML.
  154. xslTransform.Transform(reader, xw);
  155. ms.Seek(0, SeekOrigin.Begin);
  156. using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
  157. {
  158. officeML = sr.ReadToEnd();
  159. // Console.Out.WriteLine(officeML);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. return officeML;
  166. }
  167. public static void ConvertDocxToHtml(string fileName)
  168. {
  169. byte[] byteArray = File.ReadAllBytes(fileName);
  170. using (MemoryStream memoryStream = new MemoryStream())
  171. {
  172. memoryStream.Write(byteArray, 0, byteArray.Length);
  173. using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
  174. {
  175. //XslCompiledTransform xslTransform = new XslCompiledTransform();
  176. //xslTransform.Load(@"E:\document\OMML2MML.XSL");
  177. //XmlWriterSettings xmlsettings = xslTransform.OutputSettings.Clone();
  178. //xmlsettings.ConformanceLevel = ConformanceLevel.Fragment;
  179. //xmlsettings.OmitXmlDeclaration = true;
  180. //XmlWriter xw = XmlWriter.Create(memoryStream, xmlsettings);
  181. // XmlDocument xdoc = doc.ToFlatOpcDocument().GetXmlDocument();
  182. // xdoc.Save(@"E:\document\" + Guid.NewGuid().ToString() + ".xml");
  183. // string path = @"E:\document\" + Guid.NewGuid().ToString() + ".xml";
  184. // xdoc.Save(path);
  185. // xdoc.LoadXml(path);
  186. // string returnhtml = ConvertXML(xdoc, @"E:\document\1111" + ".xml", new XsltArgumentList());
  187. int imageCounter = 0;
  188. WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings()
  189. {
  190. PageTitle = "My Page Title",
  191. AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }",
  192. TableInnerCss = true,
  193. FabricateCssClasses = true,
  194. CssClassPrefix = "pt-",
  195. RestrictToSupportedLanguages = false,
  196. RestrictToSupportedNumberingFormats = false,
  197. ImageHandler = imageInfo =>
  198. {
  199. ++imageCounter;
  200. string extension = imageInfo.ContentType.Split('/')[1].ToLower();
  201. ImageFormat imageFormat = null;
  202. if (extension == "png") imageFormat = ImageFormat.Png;
  203. else if (extension == "gif") imageFormat = ImageFormat.Gif;
  204. else if (extension == "bmp") imageFormat = ImageFormat.Bmp;
  205. else if (extension == "jpeg") imageFormat = ImageFormat.Jpeg;
  206. else if (extension == "tiff")
  207. {
  208. extension = "gif";
  209. imageFormat = ImageFormat.Gif;
  210. }
  211. else if (extension == "x-wmf")
  212. {
  213. extension = "wmf";
  214. imageFormat = ImageFormat.Wmf;
  215. }
  216. if (imageFormat == null) return null;
  217. string base64 = null;
  218. try
  219. {
  220. using (MemoryStream ms = new MemoryStream())
  221. {
  222. imageInfo.Bitmap.Save(ms, imageFormat);
  223. var ba = ms.ToArray();
  224. base64 = System.Convert.ToBase64String(ba);
  225. }
  226. }
  227. catch (System.Runtime.InteropServices.ExternalException)
  228. { return null; }
  229. ImageFormat format = imageInfo.Bitmap.RawFormat;
  230. ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders()
  231. .First(c => c.FormatID == format.Guid);
  232. string mimeType = codec.MimeType;
  233. string imageSource =
  234. string.Format("data:{0};base64,{1}", mimeType, base64);
  235. XElement img = new XElement(Xhtml.img,
  236. new XAttribute(NoNamespace.src, imageSource),
  237. imageInfo.ImgStyleAttribute,
  238. imageInfo.AltText != null ?
  239. new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
  240. return img;
  241. }
  242. };
  243. // XElement html = HtmlConverter.ConvertToHtml(doc, settings);
  244. // File.WriteAllText(@"E:\document\kk.html", html.ToStringNewLineOnAttributes());
  245. XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(doc, settings);
  246. var htmls = new XDocument(new XDocumentType("html", null, null, null), htmlElement);
  247. var htmlString = htmls.ToString(SaveOptions.DisableFormatting);
  248. //引入MathJax插件
  249. htmlString = htmlString + "<script type=\"text/javascript\" src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>";
  250. File.WriteAllText(@"E:\document\kk.html", htmlString);
  251. };
  252. }
  253. }
  254. }
  255. }