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");
}
相关推荐
唐青枫15 小时前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
c#·.net
小峥降临1 天前
Rokid UXR 的手势追踪虚拟中更真实的手实战开发【含 工程源码 和 最终完成APK】
c#
晨星shine5 天前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530145 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526746 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526746 天前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang6 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf9 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530149 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools11 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net