使用NPOI操作EXCEL中的图片导入导出
//本地图片插入Excel中
//参考资料:NPOI导出excel,插入图片
//http://blog.csdn.net/zhenzhenzhao12/article/details/22170027
//author:zhenzhenzhao12
//Excel中的图片导出
//参考资料:使用NPOI从Excel中提取图片及图片位置信息
// http://www.cnblogs.com/hanzhaoxin/p/4442369.html
1.准备信息
NPOI 1.2.5,要插入的图片
2.插入方法
private void InsertImageToExcel()
{
byte[] bytes = System.IO.File.ReadAllBytes(@"myImage.png");
IWorkbook wk = new HSSFWorkbook();
int pictureIndex = wk.AddPicture(bytes, PictureType.PNG);
ISheet sheet = wk.CreateSheet("Sheet1");
// Create the drawing patriarch(家长,宗长之类的意思,这里取容器之意). This is the top level container for all shapes.
//HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch();
IDrawing patriarch = sheet.CreateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 3);
IPicture pict = patriarch.CreatePicture(anchor, pictureIndex);
//使图像恢复到原始大小
//pict.Resize();
using (FileStream fsWrite = File.OpenWrite("tt.xls"))
{
wk.Write(fsWrite);
}
MessageBox.Show("成功");
}
3.导出方法
private void ExcelToImage()
{
using (FileStream fsReader = File.OpenRead("tt.xls"))
{
IWorkbook wk = new HSSFWorkbook(fsReader);
ISheet sheet = wk.GetSheetAt(0);
List pictures = sheet.GetAllPictureInfos();
for (int i = 0; i < pictures.Count; i++)
{
using (FileStream fsWrite = File.OpenWrite(i.ToString() + ".jpg"))
{
fsWrite.Write(pictures[i].PictureData, 0, pictures[i].PictureData.Length);
}
}
}
MessageBox.Show("导出成功");
}
4.其他类:
PicturesInfo 来自韩兆新的博客园(文前的第二个链接)
public class PicturesInfo
{
public int MinRow { get; set; }
public int MaxRow { get; set; }
public int MinCol { get; set; }
public int MaxCol { get; set; }
public Byte[] PictureData { get; private set; }
public PicturesInfo(int minRow, int maxRow, int minCol, int maxCol, Byte[] pictureData)
{
this.MinRow = minRow;
this.MaxRow = maxRow;
this.MinCol = minCol;
this.MaxCol = maxCol;
this.PictureData = pictureData;
}
}
NpoiExtend.cs类,同样来自韩兆新的博客,有修改
public static class NpoiExtend
{
public static List GetAllPictureInfos(this ISheet sheet)
{
return sheet.GetAllPictureInfos(null, null, null, null);
}
public static List GetAllPictureInfos(this ISheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal = true)
{
if (sheet is HSSFSheet)
{
return GetAllPictureInfos((HSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
}
//else if (sheet is XSSFSheet)
//{
// return GetAllPictureInfos((XSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
//}
else
{
throw new Exception("未处理类型,没有为该类型添加:GetAllPicturesInfos()扩展方法!");
}
}
private static List GetAllPictureInfos(HSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
{
List picturesInfoList = new List();
var shapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer;
if (null != shapeContainer)
{
var shapeList = shapeContainer.Children;
foreach (var shape in shapeList)
{
if (shape is HSSFPicture)
{
var picture = (HSSFPicture)shape;
// var anchor = (HSSFClientAnchor)shape;
var anchor = (HSSFClientAnchor)picture.Anchor;
if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
{
picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data));
}
}
}
}
return picturesInfoList;
}
//private static List GetAllPictureInfos(XSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
//{
// List picturesInfoList = new List();
// var documentPartList = sheet.GetRelations();
// foreach (var documentPart in documentPartList)
// {
// if (documentPart is XSSFDrawing)
// {
// var drawing = (XSSFDrawing)documentPart;
// var shapeList = drawing.GetShapes();
// foreach (var shape in shapeList)
// {
// if (shape is XSSFPicture)
// {
// var picture = (XSSFPicture)shape;
// var anchor = picture.GetPreferredSize();
// if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
// {
// picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data));
// }
// }
// }
// }
// }
// return picturesInfoList;
//}
private static bool IsInternalOrIntersect(int? rangeMinRow, int? rangeMaxRow, int? rangeMinCol, int? rangeMaxCol,
int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol, bool onlyInternal)
{
int _rangeMinRow = rangeMinRow ?? pictureMinRow;
int _rangeMaxRow = rangeMaxRow ?? pictureMaxRow;
int _rangeMinCol = rangeMinCol ?? pictureMinCol;
int _rangeMaxCol = rangeMaxCol ?? pictureMaxCol;
if (onlyInternal)
{
return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow &&
_rangeMinCol <= pictureMinCol && _rangeMaxCol >= pictureMaxCol);
}
else
{
return ((Math.Abs(_rangeMaxRow - _rangeMinRow) + Math.Abs(pictureMaxRow - pictureMinRow) >= Math.Abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow)) &&
(Math.Abs(_rangeMaxCol - _rangeMinCol) + Math.Abs(pictureMaxCol - pictureMinCol) >= Math.Abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol)));
}
}
}
其他,本文参考资料已附上参考博文的链接,如有冒犯之处,请作者联系我,我必将其删除,谢谢。