123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Wordprocessing;
- using OpenXmlPowerTools;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.Serialization;
- using System.Xml.Xsl;
- using TEAMModelOS.SDK.Helper.Common.StringHelper;
- using TEAMModelOS.SDK.Helper.Network.HttpHelper;
- namespace TEAMModelOS.Test.OfficeDoc
- {
- class Program
- {
- /// <summary>
- /// 对数组进行组合操作,选取selectCount个元素进行组合
- /// </summary>
- /// <param name="lsArray">即将进行组合操作的数组</param>
- /// <param name="selectCount">选取的元素的个数</param>
- static void C(List<string> lsArray, int selectCount)
- {
- int totolcount = lsArray.Count;
- int[] currectselect = new int[selectCount];
- int last = selectCount - 1;
- for (int i = 0; i < selectCount; i++)
- {
- currectselect[i] = i;
- }
- while (true)
- {
- for (int i = 0; i < selectCount; i++)
- {
- Console.Write(" {0} ", lsArray[currectselect[i]]);
- }
- Console.WriteLine();
- if (currectselect[last] < totolcount - 1)
- {
- currectselect[last]++;
- }
- else
- {
- int pos = last;
- while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1)
- {
- pos--;
- }
- if (pos == 0) return;
- currectselect[pos - 1]++;
- for (int i = pos; i < selectCount; i++)
- {
- currectselect[i] = currectselect[i - 1] + 1;
- }
- }
- }
- }
- /// <summary>
- /// 对数组进行全排列
- /// </summary>
- /// <param name="lsArray">要进行全排列的数组</param>
- /// <param name="begin">进行全排列的开始下标</param>
- /// <param name="end">进行全排列的结束下标</param>
- static void A(List<string> lsArray, int begin, int end)
- {
- if (begin == end)
- {
- for (int i = 0; i <= end; i++)
- Console.Write(" {0} ", lsArray[i]);
- Console.WriteLine();
- }
- for (int i = begin; i <= end; i++)
- {
- Swap(lsArray, begin, i);
- A(lsArray, begin + 1, end);
- Swap(lsArray, begin, i);
- }
- }
- /// <summary>
- /// 交换数组中的下标为x,y的值
- /// </summary>
- /// <param name="lsArray">该数组</param>
- /// <param name="x"></param>
- /// <param name="y"></param>
- static void Swap(List<string> lsArray, int x, int y)
- {
- string t = lsArray[x];
- lsArray[x] = lsArray[y];
- lsArray[y] = t;
- }
-
- static void Main(string[] args)
- {
- List<string> list = new List<string>();
- for (int i = 0; i < 6; i++)
- {
- list.Add(i.ToString());
- }
- //C(list, 4);
- Console.WriteLine("---------------------");
- A(list, 0, 4);
-
-
-
- // wordPathStr = @"E:\document\题目模板.docx";
- //string s = HttpHelper.HttpGet("https://teammodelstorage.blob.core.chinacloudapi.cn/teammodelos/common/20190620/index_143520-2550.html");
- //for (int i = 0; i < 1000; i++) {
- // Console.WriteLine(DateTime.Now.ToString("yyyyMMddHHmmssfffffff"));
- //}
- //ConvertDocxToHtml(wordPathStr);
- //byte[] byteArray = File.ReadAllBytes(wordPathStr);
- //using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
- //{
- // Document document = new Document();
- // document.Load(doc.MainDocumentPart);
- // var stream = new MemoryStream();
- // stream.Write(byteArray, 0, (int)byteArray.Length);
- //doc.SaveAs(@"E:\document\1026-Weekend Acitivities-刘海荣.xml");
- // File.WriteAllBytes(@"E:\document\1026-Weekend Acitivities-刘海荣.xml", stream.ToArray());
- //}
- // string a = GetWordDocumentAsMathML(wordPathStr);
- }
- public static string ConvertXML(XmlDocument InputXMLDocument, string XSLTFilePath, XsltArgumentList XSLTArgs)
- {
- System.IO.StringWriter sw = new System.IO.StringWriter();
- XslCompiledTransform xslTrans = new XslCompiledTransform();
- xslTrans.Load(XSLTFilePath);
- xslTrans.Transform(InputXMLDocument.CreateNavigator(), XSLTArgs, sw);
- return sw.ToString();
- }
- public static string GetWordDocumentAsMathML(string fileName) {
- string officeML = string.Empty;
- using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, false))
- {
- string wordDocXml = doc.MainDocumentPart.Document.OuterXml;
- XslCompiledTransform xslTransform = new XslCompiledTransform();
- // The OMML2MML.xsl file is located under
- // %ProgramFiles%\Microsoft Office\Office15\
- xslTransform.Load(@"E:\document\OMML2MML.XSL");
- using (TextReader tr = new StringReader(wordDocXml))
- {
- // Load the xml of your main document part.
- using (XmlReader reader = XmlReader.Create(tr))
- {
- using (MemoryStream ms = new MemoryStream())
- {
- XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
- // Configure xml writer to omit xml declaration.
- settings.ConformanceLevel = ConformanceLevel.Fragment;
- settings.OmitXmlDeclaration = true;
- XmlWriter xw = XmlWriter.Create(ms, settings);
- // Transform our OfficeMathML to MathML.
- xslTransform.Transform(reader, xw);
- ms.Seek(0, SeekOrigin.Begin);
- using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
- {
- officeML = sr.ReadToEnd();
- // Console.Out.WriteLine(officeML);
- }
- }
- }
- }
- }
- return officeML;
- }
- public static void ConvertDocxToHtml(string fileName)
- {
- byte[] byteArray = File.ReadAllBytes(fileName);
- using (MemoryStream memoryStream = new MemoryStream())
- {
- memoryStream.Write(byteArray, 0, byteArray.Length);
-
- using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
- {
- //XslCompiledTransform xslTransform = new XslCompiledTransform();
- //xslTransform.Load(@"E:\document\OMML2MML.XSL");
- //XmlWriterSettings xmlsettings = xslTransform.OutputSettings.Clone();
- //xmlsettings.ConformanceLevel = ConformanceLevel.Fragment;
- //xmlsettings.OmitXmlDeclaration = true;
- //XmlWriter xw = XmlWriter.Create(memoryStream, xmlsettings);
-
- // XmlDocument xdoc = doc.ToFlatOpcDocument().GetXmlDocument();
- // xdoc.Save(@"E:\document\" + Guid.NewGuid().ToString() + ".xml");
- // string path = @"E:\document\" + Guid.NewGuid().ToString() + ".xml";
- // xdoc.Save(path);
- // xdoc.LoadXml(path);
- // string returnhtml = ConvertXML(xdoc, @"E:\document\1111" + ".xml", new XsltArgumentList());
- int imageCounter = 0;
- WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings()
- {
- PageTitle = "My Page Title",
- AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }",
- TableInnerCss = true,
- FabricateCssClasses = true,
- CssClassPrefix = "pt-",
- RestrictToSupportedLanguages = false,
- RestrictToSupportedNumberingFormats = false,
- ImageHandler = imageInfo =>
- {
- ++imageCounter;
- string extension = imageInfo.ContentType.Split('/')[1].ToLower();
- ImageFormat imageFormat = null;
- if (extension == "png") imageFormat = ImageFormat.Png;
- else if (extension == "gif") imageFormat = ImageFormat.Gif;
- else if (extension == "bmp") imageFormat = ImageFormat.Bmp;
- else if (extension == "jpeg") imageFormat = ImageFormat.Jpeg;
- else if (extension == "tiff")
- {
- extension = "gif";
- imageFormat = ImageFormat.Gif;
- }
- else if (extension == "x-wmf")
- {
- extension = "wmf";
- imageFormat = ImageFormat.Wmf;
- }
- if (imageFormat == null) return null;
- string base64 = null;
- try
- {
- using (MemoryStream ms = new MemoryStream())
- {
- imageInfo.Bitmap.Save(ms, imageFormat);
- var ba = ms.ToArray();
- base64 = System.Convert.ToBase64String(ba);
- }
- }
- catch (System.Runtime.InteropServices.ExternalException)
- { return null; }
- ImageFormat format = imageInfo.Bitmap.RawFormat;
- ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders()
- .First(c => c.FormatID == format.Guid);
- string mimeType = codec.MimeType;
- string imageSource =
- string.Format("data:{0};base64,{1}", mimeType, base64);
- XElement img = new XElement(Xhtml.img,
- new XAttribute(NoNamespace.src, imageSource),
- imageInfo.ImgStyleAttribute,
- imageInfo.AltText != null ?
- new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
- return img;
- }
- };
- // XElement html = HtmlConverter.ConvertToHtml(doc, settings);
- // File.WriteAllText(@"E:\document\kk.html", html.ToStringNewLineOnAttributes());
- XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(doc, settings);
- var htmls = new XDocument(new XDocumentType("html", null, null, null), htmlElement);
-
- var htmlString = htmls.ToString(SaveOptions.DisableFormatting);
- //引入MathJax插件
- htmlString = htmlString + "<script type=\"text/javascript\" src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>";
- File.WriteAllText(@"E:\document\kk.html", htmlString);
- };
- }
- }
- }
- }
|