public OpResult <Stream> SwaggerHtmlConvers(string html, string type, out string memi) { string fileName = Guid.NewGuid().ToString() + type; string webRootPath = _hostingEnvironment.WebRootPath; string path = webRootPath + @"\Files\TempFiles\"; var addrUrl = path + $"{fileName}"; FileStream fileStream = null; var provider = new FileExtensionContentTypeProvider(); memi = provider.Mappings[type]; try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var data = System.Text.Encoding.Default.GetBytes(html); //byte[] data = Encoding.Default.getbyte(ByteHelpe.html); var stream = ByteHelper.BytesToStream(data); //创建Document实例 Document document = new Document(); //加载HTML文档 //document.LoadFromFile("APIDocument.html", FileFormat.Html, XHTMLValidationType.None); document.LoadFromStream(stream, FileFormat.Html, XHTMLValidationType.None); //document.LoadText(stream, Encoding.Default); //保存为Word switch (type) { case ".docx": //Word document.SaveToFile(addrUrl, FileFormat.Docx); break; case ".pdf": //PDF document.SaveToFile(addrUrl, FileFormat.PDF); break; case ".html": //Html FileStream fs = new FileStream(addrUrl, FileMode.Append, FileAccess.Write, FileShare.None); //html直接写入不用spire.doc StreamWriter sw = new StreamWriter(fs); // 创建写入流 sw.WriteLine(html); // 写入Hello World sw.Close(); //关闭文件 fs.Close(); break; case ".xml": //PDF document.SaveToFile(addrUrl, FileFormat.WordXml); break; case ".svg": //PDF document.SaveToFile(addrUrl, FileFormat.SVG); break; } document.Close(); fileStream = System.IO.File.Open(addrUrl, FileMode.OpenOrCreate); var filedata = ByteHelper.StreamToBytes(fileStream); var outdata = ByteHelper.BytesToStream(filedata); return(new OpResult <Stream>(OpResultType.Success, "转换成功!", outdata)); } catch (Exception e) { return(new OpResult <Stream>(OpResultType.Error, $"转换失败,{e.Message}", null)); } finally { if (fileStream != null) { fileStream.Close(); } if (System.IO.File.Exists(addrUrl)) { System.IO.File.Delete(addrUrl);//删掉文件 } } }
/// <summary> /// 静态页面转文件 /// </summary> /// <param name="html">静态页面html</param> /// <param name="type">文件类型</param> /// <param name="contenttype">上下文类型</param> /// <returns></returns> public Stream SwaggerConversHtml(string html, string type, out string contenttype) { string fileName = Guid.NewGuid().ToString() + type; //文件存放路径 string webRootPath = _hostingEnvironment.WebRootPath; string path = webRootPath + @"\Files\TempFiles\"; var addrUrl = path + $"{fileName}"; FileStream fileStream = null; var provider = new FileExtensionContentTypeProvider(); contenttype = provider.Mappings[type]; try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var data = Encoding.Default.GetBytes(html); var stream = ByteHelper.BytesToStream(data); //创建Document实例 Document document = new Document(); //加载HTML文档 document.LoadFromStream(stream, FileFormat.Html, XHTMLValidationType.None); switch (type) { case ".docx": document.SaveToFile(addrUrl, FileFormat.Docx); break; case ".pdf": document.SaveToFile(addrUrl, FileFormat.PDF); break; case ".html": //document.SaveToFile(addrUrl, FileFormat.Html); //当然了,html 如果不用spire,也可以直接生成 FileStream fs = new FileStream(addrUrl, FileMode.Append, FileAccess.Write, FileShare.None); //html直接写入不用spire.doc StreamWriter sw = new StreamWriter(fs); // 创建写入流 sw.WriteLine(html); // 写入Hello World sw.Close(); //关闭文件 fs.Close(); break; case ".xml": document.SaveToFile(addrUrl, FileFormat.Xml); break; case ".svg": document.SaveToFile(addrUrl, FileFormat.SVG); break; default: //保存为Word document.SaveToFile(addrUrl, FileFormat.Docx); break; } document.Close(); fileStream = File.Open(addrUrl, FileMode.OpenOrCreate); var filedata = ByteHelper.StreamToBytes(fileStream); var outdata = ByteHelper.BytesToStream(filedata); return(outdata); } catch (Exception) { throw; } finally { if (fileStream != null) { fileStream.Close(); } if (File.Exists(addrUrl)) { File.Delete(addrUrl);//删掉文件 } } }