您的位置:首页 > 房产 > 家装 > 缅甸最新消息_莆田中小企业网站制作_外链seo_做网站seo推广公司

缅甸最新消息_莆田中小企业网站制作_外链seo_做网站seo推广公司

2025/4/19 0:28:25 来源:https://blog.csdn.net/qq_36437991/article/details/143450153  浏览:    关键词:缅甸最新消息_莆田中小企业网站制作_外链seo_做网站seo推广公司
缅甸最新消息_莆田中小企业网站制作_外链seo_做网站seo推广公司
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();}}
}

网页中返回的代码如下

using (var resultZipStream = new MemoryStream())
{archive.SaveTo(resultZipStream);resultZipStream.Flush();resultZipStream.Position = 0;//下载时用它HttpContext.Current.Response.ContentType = "application/octet-stream";HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName, System.Text.Encoding.UTF8));HttpContext.Current.Response.BinaryWrite(resultZipStream.ToArray());HttpContext.Current.Response.Flush();
};

如果出现未能加载文件或程序集“System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配错误

请检查web.config下的引用或者app.config

<dependentAssembly><assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

参考

方案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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com