.NET Core word文档插入图片导出

 

 

private IHostingEnvironment _hostingEnvironment;

//TaskController 类名
public TaskController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}

private Tuple<string, string> GetTupleDocx(string DocxName)
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = $"{DocxName}.docx";
return Tuple.Create(sWebRootFolder, sFileName);
}

//导出方法

public IActionResult DaochuStep()
{

//路径
string sWebRootFolder = GetTupleDocx("Word模板").Item1;

//文件名
string sFileName = GetTupleDocx("Word模板").Item2;
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));//创建一个FileInfo实例,创建路径
if (!System.IO.Directory.Exists(sWebRootFolder + sFileName))//文件是否存在如果存在删掉文件重新创建。
{
file.Delete(); //删除服务器上的临时文件
}

string taskName = "";
string path = "";

//数据源表
DataTable dtlist = SqlHelper.Query(" SELECT PTId,PId,TaskName,TaskLable,TaskTemplateId ,JZ_Id ,JZ_Type,'中国' AreaName FROM 数据表);
foreach (DataRow item in dtlist.Rows)
{

string areaname = item["AreaName"].ToString().Trim();//城市名称
taskName = item["TaskName"].ToString().Trim();//任务名称,或者城市名称
int Temple = int.Parse(item["PTId"].ToString());//任务模板id


path = sWebRootFolder;
FileHelper.CreatePath(path);//判断路径是否存在
path = path + "\\" + sFileName;
XWPFDocument doc = new XWPFDocument();//创建对象XWPFDocument

// 添加段落
XWPFParagraph gp = doc.CreateParagraph();
gp.Alignment = ParagraphAlignment.CENTER;//水平居中
XWPFRun gr = gp.CreateRun();
gp = doc.CreateParagraph();
gr = gp.CreateRun();
gr.GetCTR().AddNewRPr().AddNewRFonts().ascii = "黑体";
gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑体";
gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia;
gr.GetCTR().AddNewRPr().AddNewSz().val = (ulong)44;//2号字体
gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)44;
gr.GetCTR().AddNewRPr().AddNewB().val = true; //加粗
gr.GetCTR().AddNewRPr().AddNewColor().val = "red";//字体颜色
gr.SetText("站点名称:" + taskName);
DataTable dttemple = SqlHelper.Query("SELECT Id,Description ,AddTime ,TempStepId,"无" StepDescription ,OrderNum FROM 数据表");
foreach (DataRow item1 in dttemple.Rows)
{
int tempid = int.Parse(item1["Id"].ToString());//步骤Id
string TempName = item1["StepDescription"].ToString();//步骤名称
int step = int.Parse(item1["OrderNum"].ToString());//步骤数
string stepDescrip = item1["Description"].ToString();//步骤描述
string addtime = item1["AddTime"].ToString();//步骤时间
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//添加任务步骤描述
gp = doc.CreateParagraph();
gr = gp.CreateRun();
CT_RPr rpr = gr.GetCTR().AddNewRPr();
CT_Fonts rfonts = rpr.AddNewRFonts();
rfonts.ascii = "宋体";
rfonts.eastAsia = "宋体";
rpr.AddNewSz().val = (ulong)21;//5号字体
rpr.AddNewSzCs().val = (ulong)21;
gr.SetText("步骤" + step + ":" + TempName + " 描述:" + stepDescrip+" 添加时间:"+addtime);
DataTable dtimg = SqlHelper.Query("SELECT TableName,Path,AddTime FROM 数据表 ");
gp = doc.CreateParagraph();
gr = gp.CreateRun();
foreach (DataRow item3 in dtimg.Rows)
{
string s = item3["Path"].ToString();
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( item3["Path"].ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream imgstream = response.GetResponseStream();
Image image = Image.FromStream(imgstream);

//ImgToStream()图片转换成字节流 AddPicture()插入图片

gr.AddPicture(ImgToStream(image), (int)PictureType.PNG, "1.png", image.Width * 800, image.Height * 800);
}
catch (Exception e)
{
}
}
}

//文档内容写入path路径
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
doc.Write(fs);
}

}
return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", taskName + ".docx");//导出excel表格
}


/// <summary>
/// 图片转换成字节流
/// </summary>
/// <param name="img">要转换的Image对象</param>
/// <returns>转换后返回的字节流</returns>
public static Stream ImgToStream(Image img)
{
byte[] imgByte = ImgToByt(img);
//img.Save(ms, img.RawFormat);//System.Drawing.Imaging.ImageFormat.Jpeg
Stream stream = new MemoryStream(imgByte);
return stream;
}
/// <summary>
/// 图片转换成字节流
/// </summary>
/// <param name="img">要转换的Image对象</param>
/// <returns>转换后返回的字节流</returns>
public static byte[] ImgToByt(Image img)
{
MemoryStream ms = new MemoryStream();
byte[] imagedata = null;
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imagedata = ms.GetBuffer();
return imagedata

  }

.NET Core word文档插入图片导出

 

上一篇:合伙人(GP) 有限合伙人(LP)是什么意思?


下一篇:GP\HBase\Redis\ES\分库分表比较