asp.net core實現(xiàn)在線生成多個文件將多個文件打包為zip返回的操作
using Aspose.Words; using Aspose.Words.Saving; using System.IO.Compression; namespace ConsoleApp4 { internal class Program { static void Main(string[] args) { var html = GetHtml(); using var memoryStream = new MemoryStream(); using var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true); var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"); for (int i = 0; i < 3; i++) { var docPath = now + "_" + i + ".docx"; var entry = zipArchive.CreateEntry(docPath, System.IO.Compression.CompressionLevel.Fastest); using var entryStream = entry.Open(); var bytes = Html2Word(html); var stream = new MemoryStream(bytes); stream.CopyTo(entryStream); } memoryStream.Position = 0; // 創(chuàng)建一個FileStream,并將MemoryStream的內(nèi)容寫入該文件 string filePath = now + ".zip"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { memoryStream.CopyTo(fileStream); } //如果是asp.net core接口返回,代碼如下 //return File(memoryStream, "application/zip", filePath); Console.WriteLine("壓縮完成"); Console.ReadKey(); } /// <summary> /// 獲取html代碼 /// </summary> /// <returns></returns> static string GetHtml() { var htmlData = @" <!DOCTYPE html> <html lang=""zh""> <head> <meta charset=""UTF-8""> <meta name=""viewport"" content=""width=device-width, initial-scale=1.0""> <title>Aspose測試</title> <style> table { border: 1px solid #000; } </style> </head> <body> <table> <tr> <th>姓名</th> <th>年齡</th> </tr> <tr> <td>小明</td> <td>20</td> </tr> <tr> <td>小紅</td> <td>22</td> </tr> <tr> <td>小華</td> <td>18</td> </tr> </table> </body> </html> "; return htmlData; } static byte[] Html2Word(string htmlContent) { //如果有正版授權(quán)請寫入 //var memoryStream = new MemoryStream(Convert.FromBase64String("")); //var license = new Aspose.Words.License(); //license.SetLicense(memoryStream); var doc = new Aspose.Words.Document(); doc.RemoveAllChildren(); Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc); builder.InsertHtml(htmlContent); //var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"); //var docPath = now + ".docx"; //doc.Save(docPath); var resultMemoryStream = new MemoryStream(); doc.Save(resultMemoryStream, SaveOptions.CreateSaveOptions(SaveFormat.Docx)); return resultMemoryStream.ToArray(); } } }
安卓手機解壓縮出現(xiàn)損壞的問題
方案1 使用SharpCompress
using Aspose.Words; using Aspose.Words.Saving; using SharpCompress.Archives.Zip; using System; using System.IO; namespace ZipStu02 { internal class Program { static void Main(string[] args) { var html = GetHtml(); var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"); var archive = ZipArchive.Create(); for (int i = 0; i < 3; i++) { var docName = now + "_" + i + ".docx"; var bytes = Html2Word(html); var stream = new MemoryStream(bytes); archive.AddEntry(docName, stream); } string filePath = now + ".zip"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { archive.SaveTo(fileStream); } Console.WriteLine("生成成功"); Console.ReadKey(); } } }
方案2 使用aspose.zip
//var license = new Aspose.Zip.License(); //license.SetLicense("Aspose.Total.lic"); var html = GetHtml(); //Html2Word(html); var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"); var archive = new Archive(); for (int i = 0; i < 3; i++) { var docName = now + "_" + i + ".docx"; var bytes = Html2Word(html); var stream = new MemoryStream(bytes); archive.CreateEntry(docName, stream); } string filePath = now + ".zip"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { archive.Save(fileStream); } Console.WriteLine("生成成功"); Console.ReadKey();
參考
https://docs.aspose.com/zip/net/
https://github.com/adamhathcock/sharpcompress/wiki/API-Examples
到此這篇關于asp.net core實現(xiàn)在線生成多個文件將多個文件打包為zip返回的文章就介紹到這了,更多相關asp.net core在線生成多個文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ASP.NET對HTML頁面元素進行權(quán)限控制(一)
界面每個元素的權(quán)限也是需要控制的。比如一個查詢用戶的界面里面有查詢用戶按鈕,添加用戶按鈕,刪除用戶按鈕,不同的角色我們得分配不同的權(quán)限2013-12-12ASP.NET Core 2.0 帶初始參數(shù)的中間件問題及解決方法
這篇文章主要介紹了ASP.NET Core 2.0 帶初始參數(shù)的中間件問題及解決方法,需要的朋友可以參考下2017-10-10在Apache環(huán)境下成功的運行ASP.NET的注意事項
在Apache環(huán)境下成功的運行ASP.NET的注意事項...2007-08-08.Net插件框架Managed Extensibility Framework簡介
這篇文章介紹了.Net插件框架Managed Extensibility Framework,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
DropDownList 控件用于創(chuàng)建下拉列表。DropDownList 控件中的每個可選項都是由 ListItem 元素定義的!該控件支持數(shù)據(jù)綁定2012-12-12