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");
}
相关推荐
CodeCraft Studio10 分钟前
国产化Word处理组件Spire.DOC教程:使用 Python 将 Markdown 转换为 HTML 的详细教程
python·html·word·markdown·国产化·spire.doc·文档格式转换
揭老师高效办公4 小时前
在Word和WPS文字中将手机中间4位替换为星号****
word·wps文字
上位机付工6 小时前
C#与倍福TwinCAT3进行ADS通信
开发语言·c#
土了个豆子的7 小时前
02.继承MonoBehaviour的单例模式基类
开发语言·visualstudio·单例模式·c#·里氏替换原则
疯狂的维修7 小时前
c#中public类比博图
c#·自动化
土了个豆子的10 小时前
03.缓存池
开发语言·前端·缓存·visualstudio·c#
打码人的日常分享1 天前
运维服务方案,运维巡检方案,运维安全保障方案文件
大数据·运维·安全·word·安全架构
揭老师高效办公1 天前
在Word和WPS文字的表格中快速输入连续的星期、月、日
word·wps
xiaowu0801 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
VisionPowerful1 天前
九.弗洛伊德(Floyd)算法
算法·c#