netcore3.1 NPOI生成带有图片的word文档并输出

1、首先在管理NuGet中安装NPOI

 

2、控制器端调接口,返回文件,可直接下载
public FileResult ExportStudent()
{
var outdata = _homeService.ExportStudent();
var mime = new FileExtensionContentTypeProvider().Mappings[".doc"];
HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "content-disposition");
return File(outdata, mime, DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".doc");//返回文件流,type是文件格式
}

 

3、接口实现:

public MemoryStream ExportStudent()
{
var docPath = Directory.GetCurrentDirectory() + "\\files\\二维码.doc";
XWPFDocument doc = new XWPFDocument(); //创建新的word文档

// 添加段落
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)36;
gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)36;
gr.GetCTR().AddNewRPr().AddNewB().val = true; //加粗
gr.GetCTR().AddNewRPr().AddNewColor().val = "red";//字体颜色
gr.SetText("深圳市南山外国语学校 - 初一(3)班");

var list = new List<textStudent>();
list.Add(new textStudent { Name = "小红", No = "001", Sex = "女" });
list.Add(new textStudent { Name = "小红1", No = "002", Sex = "女" });
list.Add(new textStudent { Name = "小红1", No = "003", Sex = "女" });
list.Add(new textStudent { Name = "小强1", No = "004", Sex = "男" });
list.Add(new textStudent { Name = "小强1", No = "005", Sex = "男" });
list.Add(new textStudent { Name = "小红0", No = "006", Sex = "女" });
list.Add(new textStudent { Name = "小红1", No = "007", Sex = "女" });
list.Add(new textStudent { Name = "小红1", No = "008", Sex = "女" });
list.Add(new textStudent { Name = "小强1", No = "009", Sex = "男" });
list.Add(new textStudent { Name = "小强1", No = "010", Sex = "男" });
list.Add(new textStudent { Name = "小强2", No = "011", Sex = "男" });
list.Add(new textStudent { Name = "小强9", No = "012", Sex = "男" });

//根据每列表格数计算所需要的行数
var rows = Convert.ToInt32(Math.Round(list.Count / 4m));
XWPFTable tableContent = doc.CreateTable(rows, 4);

tableContent.Width = 1300 * 4;
tableContent.SetColumnWidth(0, 1300);/* 设置列宽 */
tableContent.SetColumnWidth(1, 1300);
tableContent.SetColumnWidth(2, 1300);
tableContent.SetColumnWidth(3, 1300);

//二维码存放路径
string file_path = Directory.GetCurrentDirectory() + "\\files\\QRCode";

DirectoryInfo dir = new DirectoryInfo(file_path);

for (int i = 0; i < rows; i++)
{
var newList = list.Skip(i * 4).Take(4).ToList();
var j = 0;
foreach (var item in newList)
{
XWPFTableCell Cell = tableContent.GetRow(i).GetCell(j);//获取单元格

#region 连接生成二维码
var img = CommonCode.GetQRCode("https://www.baidu.com?Name=" + item.Name + "&No=" + item.No, 5);

#endregion

// 单元格的宽度和高度
var cW = 1500;
var cH = 1700;
XWPFParagraph p = Cell.AddParagraph();
XWPFRun run = p.CreateRun();
var widthPic = (int)((double)cW / 587 * 38.4 * 9525);
var heightPic = (int)((double)cH / 587 * 38.4 * 9525);
run.AddPicture(img, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "123.jpg", widthPic, heightPic);
run.AddCarriageReturn();//换行回车
run.SetText("姓名:" + item.Name);
XWPFParagraph p1 = Cell.AddParagraph();
XWPFRun run1 = p1.CreateRun();
run1.SetText("性别:" + item.Sex);

XWPFParagraph p3 = Cell.AddParagraph();
XWPFRun run3 = p3.CreateRun();
run3.SetText("学号:" + item.No);
//tableContent.GetRow(i).GetCell(j).SetParagraph(p);
j++;
}
}

//保存word
using (FileStream fs = new FileStream(docPath, FileMode.OpenOrCreate, FileAccess.Write))
{
doc.Write(fs);
}
//流文件返回给前端
var memory = new MemoryStream();
using (var stream = new FileStream(docPath, FileMode.Open))
{
stream.CopyTo(memory);
}
memory.Position = 0;
return memory;
}

 

4、生成二维码方法

/// <summary>
/// 根据连接返回二维码图片流
/// </summary>
/// <param name="url">存储内容</param>
/// <param name="pixel">像素大小</param>
/// <returns></returns>
public MemoryStream GetQRCode(string url, int pixel)
{
QRCodeGenerator generator = new QRCodeGenerator();
QRCodeData codeData = generator.CreateQrCode(url, QRCodeGenerator.ECCLevel.M, true);
QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData);

Bitmap qrImage = qrcode.GetGraphic(pixel, Color.Black, Color.White, true);

MemoryStream ms = new MemoryStream();
qrImage.Save(ms, ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);

return ms;
}

5、运行后执行接口

netcore3.1 NPOI生成带有图片的word文档并输出

 

 

6、下载文档打开

netcore3.1 NPOI生成带有图片的word文档并输出

上一篇:JSON 通过强类型或者匿名对象 序列化和反序列化 对象和集合


下一篇:JS-JSON