当前位置: 首页 > news >正文

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 中的正确位置。最后图像将成功输入到所需位置:

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

http://www.lryc.cn/news/518696.html

相关文章:

  • Lambda expressions in C++ (C++ 中的 lambda 表达式)
  • 【Rust自学】11.4. 用should_panic检查恐慌
  • 高斯函数Gaussian绘制matlab
  • 获取客户端真实IP地址
  • Kotlin学习(一)
  • 鸿蒙UI开发——日历选择器
  • 2025-1-9 QT 使用 QXlsx库 读取 .xlsx 文件 —— 导入 QXlsx库以及读取 .xlsx 的源码 实践出真知,你我共勉
  • React中createRoot函数原理解读——Element对象与Fiber对象、FiberRootNode与HostRootNode
  • 利用Python实现Union-Find算法
  • 【LeetCode: 912. 排序数组 + 归并排序】
  • AI时代来了,我们不再需要IDE了
  • PL/SQL语言的网络编程
  • vue video重复视频 设置 srcObject 视频流不占用资源 减少资源浪费
  • JavaFx 21 项目Markdown 预览、编辑、新建、文件树、删除、重命名
  • git项目提交步骤(简洁版)
  • 风水算命系统架构与功能分析
  • Clojure语言的学习路线
  • 网络安全核心目标CIA
  • Wi-Fi Direct (P2P)原理及功能介绍
  • Perl语言的数据结构
  • 【MFC】设置CTreeCtrl单个节点的文字颜色
  • 【CSS】设置滚动条样式
  • Gitlab-Runner配置
  • 代码随想录 哈希 test 8
  • [SAP ABAP] 使用LOOP AT...ASSIGNING FIELD-SYMBOL 直接更新内表数据
  • MySQL数据导出导入
  • leetcode 127. 单词接龙
  • 如何开发一个支持海量分布式锁的应用库
  • JavaScript系列(17)--类型系统模拟
  • openssl编译