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/20
row.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);
}
}
}
}