C# 或 .NetCore 如何使用 NPOI 导出图片到 Excel 文件

今天在本文中,我们将尝试使用NPOI库将图像插入到 Excel 文件的特定位置。请将以下逻辑添加到您的写作方法中,在 Excel 文件中添加图像(JPEG、PNG),我已经有一个示例 jpeg 文件 - Read-write-excel-npoi.jpg ,我们将尝试将其插入索引 (5,5),即第 5 行和第 5 列。

在第 5 行和第 5 列,将以编程方式插入上述图像,代码如下:

byte[] data = File.ReadAllBytes("Read-write-excel-npoi.jpg");//根据自己路径读取图片

int pictureIndex = workbook.AddPicture(data, PictureType.JPEG);

ICreationHelper helper = workbook.GetCreationHelper();

IDrawing drawing = excelSheet.CreateDrawingPatriarch();

IClientAnchor anchor = helper.CreateClientAnchor();

anchor.Col1 = 5;

anchor.Row1 = 5;

IPicture picture = drawing.CreatePicture(anchor, pictureIndex);

picture.Resize();

用图像写入 EXCEL

下面是一个 POC 完整代码示例,如下所示:

static void WriteExcel()

{

List<UserDetails> persons = new List<UserDetails>()

{

new UserDetails() {ID="1001", Name="ABCD", City ="City1", Country="USA"},

new UserDetails() {ID="1002", Name="PQRS", City ="City2", Country="INDIA"},

new UserDetails() {ID="1003", Name="XYZZ", City ="City3", Country="CHINA"},

new UserDetails() {ID="1004", Name="LMNO", City ="City4", Country="UK"},

};

// Lets converts our object data to Datatable for a simplified logic.

// Datatable is most easy way to deal with complex datatypes for easy reading and formatting.

DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(persons), (typeof(DataTable)));

var memoryStream = new MemoryStream();

using (var fs = new FileStream("Result.xlsx", FileMode.OpenOrCreate, FileAccess.Write))

{

IWorkbook workbook = new XSSFWorkbook();

ISheet excelSheet = workbook.CreateSheet("TestSheet1");

List<String> columns = new List<string>();

IRow row = excelSheet.CreateRow(0);

int columnIndex = 0;

foreach (System.Data.DataColumn column in table.Columns)

{

columns.Add(column.ColumnName);

row.CreateCell(columnIndex).SetCellValue(column.ColumnName);

columnIndex++;

}

int rowIndex = 1;

foreach (DataRow dsrow in table.Rows)

{

row = excelSheet.CreateRow(rowIndex);

int cellIndex = 0;

foreach (String col in columns)

{

row.CreateCell(cellIndex).SetCellValue(dsrow[col].ToString());

cellIndex++;

}

rowIndex++;

}

byte[] data = File.ReadAllBytes("Read-write-excel-npoi.jpg");

int pictureIndex = workbook.AddPicture(data, PictureType.JPEG);

ICreationHelper helper = workbook.GetCreationHelper();

IDrawing drawing = excelSheet.CreateDrawingPatriarch();

IClientAnchor anchor = helper.CreateClientAnchor();

anchor.Col1 = 5;

anchor.Row1 = 5;

IPicture picture = drawing.CreatePicture(anchor, pictureIndex);

picture.Resize();

workbook.Write(fs);

}

}

我将图像文件保存在同一个项目目录中,以便 Excel API 可以使用它并将其加载到 Excel 中的正确位置。最后图像将成功输入到所需位置:

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
小码编匠11 小时前
WPF 中的高级交互通过右键拖动实现图像灵活缩放
后端·c#·.net
唐青枫18 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez20101 天前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
葡萄城技术团队2 天前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
mudtools2 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫2 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz3 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
QQ3596773453 天前
ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
数据库·arcgis·excel
唐青枫3 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net