NPOI在Excel中插入图片

 for (int i = 0; i < FeatureMapList.Count; i++){// 第二步:确定图片索引int pictureIdx = wk.AddPicture(FeatureMapBytesList[k], PictureType.PNG); // 注意图片格式// 第三步:创建画部IDrawing patriarch = image_sheet.CreateDrawingPatriarch();// 第四步:设置锚点int rowline = rowsCount-1; // y方向// 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数)int colline = 1;  //控制x方向IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, colline + i + 1, rowline  + 1, colline + 1 + i + 1, rowline + 1 + 1);// 第五步:把图片插到相应的位置IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);k++;}

patriarch.CreateAnchor: 8个参数分别代表左上角右下角所在单元格的偏移量和坐标

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace learning4NPOI
{class Program{static void Main(string[] args){//HSSFWorkbook wk = new HSSFWorkbook();//ISheet sheet = wk.CreateSheet("Images");//IRow row = sheet.CreateRow(0);//ICell cell = row.CreateCell(0);//cell.SetCellValue("test测试");//using (FileStream fs= File.OpenWrite("excel.xls"))//{//    wk.Write(fs);//}//读入之前生成的EXCEL,然后以其做为模版,修改之后保存为另一份EXCEL//string tempPath = "excel.xls";//using (FileStream fs = File.Open(tempPath, FileMode.Open,FileAccess.Read, FileShare.ReadWrite))//{//    //把xls文件读入workbook变量里,之后就可以关闭了  //    wk = new HSSFWorkbook(fs);//    fs.Close();//}//在第二行创建行   ////IRow row = sheet.CreateRow(1);在第二行的第一列创建单元格    //ICell cell = row.CreateCell(0);获取第一行  //IRow row = sheet.GetRow(0);在第一行获取第一列单元格    //ICell cell = row.GetCell(0);//sheet.GetRow(0).GetCell(0).SetCellValue("编辑的值");在sheet中创建画部//IDrawing patriarch = SheetOne.CreateDrawingPatriarch();第四步:设置锚点 (在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数)  //IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 4, 1, 6, 6);创建图片  //IPicture pict = patriarch.CreatePicture(anchor, picture);//pict.Resize(0.9);HSSFWorkbook wk = new HSSFWorkbook();ISheet sheet = wk.CreateSheet("Images");// 第一步:读入图片数据byte[] bytes = System.IO.File.ReadAllBytes(@"21CurvatureMaxPart.bmp");// 第二步:确定图片索引int pictureIdx = wk.AddPicture(bytes, PictureType.BMP);  // 注意图片格式// 第三步:创建画部IDrawing patriarch = sheet.CreateDrawingPatriarch();// 第四步:设置锚点int rowline = 1;// 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数)  IClientAnchor anchor = patriarch.CreateAnchor(70, 10, 0, 0, 1, rowline, 2, rowline + 1);// 第五步:把图片插到相应的位置IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);using (FileStream fs = File.OpenWrite("bbb.xls")){wk.Write(fs);}//using (Stream stream = File.OpenWrite("aaa.xls"))//{//    wk.Write(stream);//}}}
}
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4NPIO
{class Program{static void Main(string[] args){/**** * * * 1.建表* * * ***/HSSFWorkbook wk = new HSSFWorkbook();ISheet sheet = wk.CreateSheet("Images");/**** * * * 2.定义单元格格式* * * ***/sheet.DefaultRowHeight = 230 * 20;sheet.SetColumnWidth(1, 23 * 256);ICellStyle cellStyle = wk.CreateCellStyle();//边框颜色  cellStyle.BottomBorderColor = HSSFColor.Blue.Index;cellStyle.TopBorderColor = HSSFColor.Blue.Index;/**** * * * 3.插入内容* * ***//**** * 3.1 向Images表中插入内容* ***/// 第一步:读入图片数据byte[] bytes = System.IO.File.ReadAllBytes(@"CurvatureMaxPart1.bmp");// 第二步:确定图片索引int pictureIdx = wk.AddPicture(bytes, PictureType.PNG); // 注意图片格式// 第三步:创建画部IDrawing patriarch = sheet.CreateDrawingPatriarch();// 第四步:设置锚点int rowline = 1; // y方向// 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数)  IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 1, rowline, 2, rowline + 1);  // 第五步:把图片插到相应的位置IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);rowline = 2;IClientAnchor anchor2 = patriarch.CreateAnchor(0, 0, 0, 0, 1, rowline, 2, rowline + 1);// 第五步:把图片插到相应的位置IPicture pict2 = patriarch.CreatePicture(anchor2, pictureIdx);int coline = 3; // x方向// patriarch.CreateAnchor: 8个参数分别代表左上角和右下角所在单元格的偏移量和坐标IClientAnchor anchor3 = patriarch.CreateAnchor(0, 0, 0, 0, 0, 0, 1, 1);// 第五步:把图片插到相应的位置IPicture pict3 = patriarch.CreatePicture(anchor3, pictureIdx);IClientAnchor anchor5 = patriarch.CreateAnchor(0, 0, 0, 0, 1, 0, 2, 1);// 第五步:把图片插到相应的位置IPicture pict5 = patriarch.CreatePicture(anchor5, pictureIdx);coline = 4; // x方向IClientAnchor anchor4 = patriarch.CreateAnchor(0, 0, 0, 0, coline, 1, coline + 1, 2);// 第五步:把图片插到相应的位置IPicture pict4 = patriarch.CreatePicture(anchor4, pictureIdx);/**** * 3.2 向Confidence表中插入内容* ***//**** * * 4.保存Excel* * ***/using (FileStream fs = File.OpenWrite("bbb2.xls")){wk.Write(fs);}}}
}
if (dt.Rows.Count > 0)
{int rowline = 1;//从第二行开始(索引从0开始)foreach (DataRow datarow in dt.Rows){IRow row = sheet1.CreateRow(rowline);//设置行高 ,excel行高度每个像素点是1/20row.Height = 80 * 20;//填入生产单号row.CreateCell(0, CellType.STRING).SetCellValue(datarow["PRODID"].ToString());//将图片文件读入一个字符串byte[] bytes = System.IO.File.ReadAllBytes(datarow["KTL_PIC"].ToString());int pictureIdx=workbook.AddPicture(bytes,PictureType.JPEG);HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();// 插图片的位置  HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释HSSFClientAnchor anchor = new HSSFClientAnchor(70, 10,0,0, 1,rowline, 2, rowline +1);//把图片插到相应的位置HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);rowline++;}
}

整体实例

using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;namespace Test4NPIO
{class Program{private static IRow dataRow;static void Main(string[] args){/**** * * * 1.建表* * * ***/HSSFWorkbook wk = new HSSFWorkbook();ISheet image_sheet = wk.CreateSheet("Images");ISheet confidence_sheet = wk.CreateSheet("Confidence");/**** * * * 2.定义单元格格式* * * ***/ICellStyle cellstyle1 = wk.CreateCellStyle();cellstyle1.VerticalAlignment = VerticalAlignment.Center;cellstyle1.Alignment = HorizontalAlignment.Center;// image_sheet.DefaultRowHeight = 230 * 20;for (int i = 0; i < 100; i++){image_sheet.SetColumnWidth(i+1, 23 * 256);}ICellStyle cellStyle = wk.CreateCellStyle();//边框颜色  cellStyle.BottomBorderColor = HSSFColor.Blue.Index;cellStyle.TopBorderColor = HSSFColor.Blue.Index;// confidence_sheet//confidence_sheet.DefaultRowHeight = 230 * 20;for (int i =0;i<100;i++){confidence_sheet.SetColumnWidth(i + 1, 23 * 256);}/**** * * * 3.插入内容* * ***//**** * 3.1 向Images表中插入内容* ***/List<byte[]> img_bytes_list = new List<byte[]>();// 第一步:读入图片数据byte[] bytes = System.IO.File.ReadAllBytes(@"CurvatureMaxPart1.bmp");for (int i =0;i<140;i++){img_bytes_list.Add(bytes);}Console.WriteLine(img_bytes_list.Count);int k = 0;for (int i=0;i<20;i++){for (int j=0;j<7;j++){// 第二步:确定图片索引int pictureIdx = wk.AddPicture(img_bytes_list[k], PictureType.PNG); // 注意图片格式// 第三步:创建画部IDrawing patriarch = image_sheet.CreateDrawingPatriarch();// 第四步:设置锚点int rowline = 0; // y方向// 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数)int colline = 0;IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, colline+j+1, rowline+i+1, colline+1+j+1, rowline + 1+i+1);// 第五步:把图片插到相应的位置IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);k++;}}/**** * 3.2 向Confidence表中插入内容* ***/string[] txtLines = File.ReadAllLines(@"C:\Users\29939\Desktop\当前项目文件\res\data.txt");for (int i = 0; i < txtLines.Length; ++i){//将txt文本中的一行数据用','分割string[] line = txtLines[i].Split(',');//创建行IRow dataRow = confidence_sheet.CreateRow(i);for (int j = 0; j < line.Length; j++){//创建列,并写入值ICell  cell = dataRow.CreateCell(j+1);cell.SetCellValue(line[j]);cell.CellStyle = cellstyle1;}}/**** * * 4.保存Excel* * ***/using (FileStream fs = File.OpenWrite("bbb2.xls")){wk.Write(fs);}}}
}

NPOI在Excel中插入图片相关推荐

  1. [转载] 使用openpyxl模块向Excel中插入图片

    参考链接: Python | 使用openpyxl模块在Excel工作表中绘制图表 2 使用openpyxl模块向Excel中插入图片 示例Python实现结果展示 示例 向一个Excel表格中A1位 ...

  2. excel poi 加背景图_java 在Excel中插入图片 POI实现

    一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...

  3. python下载图片插入excel_Python向Excel中插入图片的简单实现方法

    本文实例讲述了Python向Excel中插入图片的简单实现方法.分享给大家供大家参考,具体如下: 使用Python向Excel文件中插入图片,这个功能之前学习xlwt的时候通过xlwt模块实现过.那时 ...

  4. java poi 在excel中插入图片

    java web中导出excel数据是常见的功能,最近遇到一个需求是在excel中插入图片.处理excel及其他微软办公系列软件常用的就是apache poi,它也是支持图片插入的.插入图片最主要的用 ...

  5. java中excel导入图片_java POI实现向Excel中插入图片

    做Web开发免不了要与Excel打交道.今天老大给我一个任务-导出Excel.开始想的还是蛮简单的,无非就是查找,构建Excel,response下载即可.但是有一点不同,就是要加入图片,就是这个加入 ...

  6. java 读取excel 图片_利用Java+POI 读写Excel文档向Excel中插入图片

    一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...

  7. poi 向Excel中插入图片

    使用poi向excel中插入图片 直接上代码,我这里的图片是项目中的一个logo图片,所以是直接springboot读取本地文件的方式 主要解释一下定位图片位置的八个参数int dx1, int dy ...

  8. Java Poi 向excel中插入图片

    博客 package com.unicom.yangjiabin.utils;import java.awt.image.BufferedImage; import java.io.ByteArray ...

  9. POI 在导出的Excel中插入图片

    项目中遇到了在导出Excel表格时插入图片至单元格,测试单元代码如下: import java.awt.image.BufferedImage; import java.io.ByteArrayOut ...

最新文章

  1. Java Web项目第二次总结
  2. 编译器错误信息: CS1595: 已在多处定义“ ”;一种可能出现的情况
  3. step3 . day6数据结构之非线性表 满二叉树和不完全二叉树
  4. PaddlePaddle Perceptron Example
  5. Bootstrap 中的工具Mixin
  6. 条件语句的多层嵌套问题优化,助你写出不让同事吐槽的代码
  7. android简单课表,模仿大神的一个android课程表
  8. python class用法理解_通过钢铁侠变身快速理解Python的装饰器用法
  9. pytorch torch.linspace
  10. 计算机题硬盘分区首先,您对计算机硬盘分区了解多少: 如何进行分区合理?
  11. 微软 Edge 浏览器被指共享隐私遥测数据
  12. php eclipse 插件安装,phpeclipse插件下载-phpeclipse插件官方版 v1.2.3 官方最新版 - 安下载...
  13. html css 美化模板,通用css样式,全局css样式,css样式模板,
  14. mkv文件用什么软件打开_什么是MKV文件,以及如何播放它们?
  15. 【响应式Web前端设计】i标签和em标签的区别
  16. java privatekey输出字符串_[Java教程]根据字符串(String)生成公钥(PublicKey)和私钥(PrivateKey)对象_星空网...
  17. android模拟器MAC无法联网(安卓开发)
  18. JAVA社招面试问题总结(一年工作经验)
  19. 京东实时计算架构演进之路
  20. 二〇一六年大四狗找工作

热门文章

  1. 上海腾享-舞台灯光设备为实现安全稳定、灵活多变、色彩绚丽的舞台灯光效果
  2. Vue过滤器:第二天
  3. 系统性思考-思考习惯的养成
  4. js 中的SetTimeOut()
  5. mysql 1142 问题解决
  6. Cesium 基础知识和文档记录
  7. 全球地形数据下载分享
  8. 为什么用红墨水试验检测BGA焊接情况?我教你如何对结果预判!
  9. multimodal prompting
  10. 第三章 软件需求分析