asp.net core實(shí)現(xiàn)在線(xiàn)生成多個(gè)文件將多個(gè)文件打包為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)建一個(gè)FileStream,并將MemoryStream的內(nèi)容寫(xiě)入該文件
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測(cè)試</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)請(qǐng)寫(xiě)入
//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();
}
}
}

安卓手機(jī)解壓縮出現(xiàn)損壞的問(wè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
到此這篇關(guān)于asp.net core實(shí)現(xiàn)在線(xiàn)生成多個(gè)文件將多個(gè)文件打包為zip返回的文章就介紹到這了,更多相關(guān)asp.net core在線(xiàn)生成多個(gè)文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- ASP.NET?Core使用EF創(chuàng)建模型(包含屬性、排除屬性、主鍵和生成值)
- ASP.NET Core自動(dòng)生成小寫(xiě)破折號(hào)路由的實(shí)現(xiàn)方法
- ASP.NET Core 5中如何生成PDF文檔
- Asp.Net Core使用swagger生成api文檔的完整步驟
- 詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯)
- Asp.NetCore1.1版本去掉project.json后如何打包生成跨平臺(tái)包
- Asp.net core WebApi 使用Swagger生成幫助頁(yè)實(shí)例
- 基于A(yíng)SP.NET Core數(shù)據(jù)保護(hù)生成驗(yàn)證token示例
相關(guān)文章
asp.net操作Word實(shí)現(xiàn)批量替換
這篇文章主要介紹了asp.net操作Word實(shí)現(xiàn)批量替換的方法,需要的朋友可以參考下2015-10-10
在 .NET 項(xiàng)目中復(fù)制資源文件夾到生成目錄的方法
本文主要介紹在使用 Visual Studio 進(jìn)行調(diào)試和發(fā)布時(shí),如何在 .NET 項(xiàng)目中復(fù)制資源文件夾到生成目錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03
適用與firefox ASP.NET無(wú)刷新二級(jí)聯(lián)動(dòng)下拉列表
適用與firefox ASP.NET無(wú)刷新二級(jí)聯(lián)動(dòng)下拉列表...2007-08-08
asp.net 在global中攔截404錯(cuò)誤的實(shí)現(xiàn)方法
asp.net 在global中攔截404錯(cuò)誤,增加用于體驗(yàn),不會(huì)因?yàn)樘崾菊也坏叫畔⒍苯油顺龅膶擂巍?/div> 2010-03-03
使用源鏈接對(duì)ASP.NET Core源代碼進(jìn)行調(diào)試
這篇文章介紹了使用源鏈接對(duì)ASP.NET Core源代碼進(jìn)行調(diào)試的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
ASP.NET MVC中Controller控制器向View視圖傳值的幾種方式
這篇文章介紹了ASP.NET MVC中Controller控制器向View視圖傳值的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03最新評(píng)論

