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");
}
相关推荐
不会编程的懒洋洋20 分钟前
VisionPro 中 几何相交工具 Geometry-Intersection
图像处理·笔记·c#·视觉检测·机器视觉·visionpro
不会编程的懒洋洋3 小时前
VisionPro 中 图像预处理工具
图像处理·笔记·c#·视觉检测·visionpro
雪豹阿伟5 小时前
9.C# —— string拓展方法、StringBuilder高性能处理
c#·上位机
李星星_Alex6 小时前
如何通过 C# 抓取抖音 RTMP 推流码实现自主推流
c#·抖音
qq7422349849 小时前
全面深入的C#核心知识体系与编程实践精要——从语法基础到高级特性系统学习指南
java·算法·c#
fai厅的秃头姐!11 小时前
2586. 统计范围内的元音字符串数
开发语言·c#
正运动技术11 小时前
C#运动控制开源(二): CAD导图和小线段速度前瞻优化
c#·正运动技术·运动控制器·运动控制卡·正运动控制器·运动控制开源·ethercat运动控制器
楼田莉子11 小时前
C#学习:分支与循环
服务器·后端·学习·c#
chéng ௹11 小时前
python将word文档转化成html
python·html·word
还是叫明12 小时前
C#使用YOLO26进行图像识别(目标检测)
opencv·yolo·目标检测·c#