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;
// 创建一个FileStream,并将MemoryStream的内容写入该文件
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)
{
//如果有正版授权请写入
//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();
}
}
}


安卓手机解压缩出现损坏的问题
方案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实现在线生成多个文件将多个文件打包为zip返回的文章就介绍到这了,更多相关asp.net core在线生成多个文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源链接:https://www.jb51.net/aspnet/330009hkf.htm
© 版权声明
本站所有资源来自于网络,仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您(转载者)自己承担!
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
THE END














暂无评论内容