phpexcel 导出循环增加列数_基于.NetCore3.1搭建项目系列 —— 使用Swagger导出文档 (补充篇)...
生活随笔
收集整理的這篇文章主要介紹了
phpexcel 导出循环增加列数_基于.NetCore3.1搭建项目系列 —— 使用Swagger导出文档 (补充篇)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
? 在上一篇導出文檔番外篇中,我們已經熟悉了怎樣根據json數據導出word的文檔,生成接口文檔,而在這一篇,將對上一篇進行完善補充,增加多種導出方式,實現更加完善的導出功能。
回顧
? 1. 獲取Swagger接口文檔的Json文件
? 2. 解析Json文件數據填充到Html的表格中
? 3.根據生成的html轉work文檔
功能
開始
?根據生成的html轉work文檔
/// /// 靜態頁面轉文件/// /// 靜態頁面html/// 文件類型/// 上下文類型/// 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);//刪掉文件 }
}
增加導出按鈕
function LoadExportApiWordBtn() {$(".information-container").height(240);
$(".topbar").height(35);var btnExport = "" +
"導出離線文檔" +
"" +
"" +
"" +
"導出 Word" +
"" +
"" +
"導出 PDF" +
"" +
"" +
"導出 Html" +
"" +
"" +
"導出 Xml" +
"" +
"" +
"導出 Svg" +
"" +
"" +
"" +
"";//information-container這個元素是swagger后期動態渲染出來的,所有這里要加個循環判斷。//第一次進來如果有這個class直接加載按鈕退出if ($("*").hasClass("information-container")) {
$(".information-container").append(btnExport);return;
}//沒有元素等待元素出現在加載按鈕
timerLoadExportBtn = setInterval(function () {if ($("*").hasClass("information-container")) {
$(".information-container").append(btnExport);
console.log("load ok");
window.clearInterval(timerLoadExportBtn);return;
}
console.log("loading");
}, 788);
}
效果
總結
?1. 通過Swagger 導出各類的說明文檔,可以根據自己的html模板生成各式的word樣式文檔說明。
?2 .注:搜索關注公眾號【DotNet技術谷】--回復【文檔生成器】,可獲取本篇Swagger生成文檔文件。
?3. 參考資料:Spire.Doc文件?、Swagger開源地址
總結
以上是生活随笔為你收集整理的phpexcel 导出循环增加列数_基于.NetCore3.1搭建项目系列 —— 使用Swagger导出文档 (补充篇)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 助听器需要配吗
- 下一篇: 动态规划算法php,php算法学习之动态