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");
}
相关推荐
数据的世界0110 小时前
C#4.0权威指南第12章:接口
开发语言·c#
c#上位机13 小时前
C#读取保存图像踩坑之FileStream类
开发语言·c#
醉酒柴柴17 小时前
word中没文字地方添加下划线方法
学习·word
manyikaimen18 小时前
博派智能-运动控制技术-RTCP-五轴联动
c++·图像处理·qt·算法·计算机视觉·机器人·c#
武藤一雄1 天前
C# 异步回调与等待机制
前端·microsoft·设计模式·微软·c#·.netcore
乱蜂朝王1 天前
使用 C# 和 ONNX Runtime 部署 PaDiM 异常检测模型
开发语言·c#
JosieBook2 天前
【C#】VS中的 跨线程调试异常:CrossThreadMessagingException
开发语言·c#
追雨潮2 天前
BGE-M3 多语言向量模型实战:.NET C# 从原理到落地
开发语言·c#·.net
CheerWWW2 天前
GameFramework——Download篇
笔记·学习·unity·c#
格林威2 天前
ZeroMQ 在视觉系统中的应用
开发语言·人工智能·数码相机·机器学习·计算机视觉·c#·视觉检测