Aspose.Words导出word,服务器用内存流处理,不生成磁盘文件

框架集:.NET8

csharp 复制代码
public async Task<IActionResult> ExportPDF(long? id)
{
	  var info=await  form_Dahui_ReportDao.GetAsync(id);
	   if (info == null)
	   {
		   return Content("没找到数据");
	   }
	  //读取word模板
	  string fileTemp = Path.Combine(AppContext.BaseDirectory, "temp_reportDahui.docx");
	  Aspose.Words.Document doc = new Aspose.Words.Document(fileTemp);
	  Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

	  //书签1
	  builder.MoveToBookmark("chejian");
	  builder.Write(info.Dangzhibu_name);

	  //书签2
	  builder.MoveToBookmark("chejian2");
	  builder.Write(info.Dangzhibu_name);

	  builder.MoveToBookmark("chejina3");
	  builder.Write(info.Dangzhibu_name);
	 
	  //内存流处理
	  Stream stream = new System.IO.MemoryStream();
	  try
	  {
		  //doc.Save(stream, Aspose.Words.SaveFormat.Pdf);//导出pdf文件

		  doc.Save(stream, Aspose.Words.SaveFormat.Doc);//导出word文件              
		  stream.Seek(0, SeekOrigin.Begin);
	  }
	  catch (Exception ex)
	  {
		  string msg = ex.Message;
		  return Content("导出出错="+ msg  );
	  }     
	 
	  string fileName = $"{info.Dangzhibu_name}_{info.Createtime.Value.ToString("yyyyMMdd")}";
	  if (fileName.IndexOf("\\") >= 0
		  || fileName.IndexOf("/") >= 0
		  || fileName.IndexOf(":") >= 0
		  || fileName.IndexOf("*") >= 0
		  || fileName.IndexOf("?") >= 0
		  || fileName.IndexOf("<") >= 0
		  || fileName.IndexOf(">") >= 0
		  || fileName.IndexOf("|") >= 0
		  )
	  {
		  fileName = fileName.Replace("\\", "_");
		  fileName = fileName.Replace("/", "_");
		  fileName = fileName.Replace(":", "_");
		  fileName = fileName.Replace("*", "_");
		  fileName = fileName.Replace("?", "_");
		  fileName = fileName.Replace("<", "_");
		  fileName = fileName.Replace(">", "_");
		  fileName = fileName.Replace("|", "_");
	  }
	  //return File(stream, "application/pdf ", $"{fileName}.pdf");
	  
	  return File(stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", $"{fileName}.docx");
}
相关推荐
寒山李白7 小时前
解决 python-docx 生成的 Word 文档打开时弹出“无法读取内容“警告
python·word·wps·文档·docx·qoder
宝桥南山14 小时前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
hixiong12317 小时前
C# OpenvinoSharp部署INSID3
开发语言·人工智能·ai·c#·openvinosharp
星辰徐哥17 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
leoufung17 小时前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
hacker70718 小时前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
SKY -dada18 小时前
Understand 使用教程
开发语言·c#·流程图·软件构建·敏捷流程·代码复审·源代码管理
AI进化营-智能译站19 小时前
ROS2 C++开发系列01:在ROS2上编写第一个C++ hello word
开发语言·c++·ai·word
William_cl20 小时前
【C#/.NET 进阶】ASP.NET 架构与最佳实践:DI 依赖注入(IoC 核心)从入门到避坑
c#·asp.net·.net
武藤一雄20 小时前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf