利用NPOI给excel文件中添加图片
利用NPOI给excel中添加图片
核心代码,可自行封装
var imgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "seal.png");
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tttt.xls");
var newPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "aaaa.xls");
var buffer = File.OpenRead(filePath);
var bytes = File.ReadAllBytes(imgPath);
var workbook = new HSSFWorkbook(buffer);
var sheet = workbook.GetSheetAt(0);
var drawing = sheet.CreateDrawingPatriarch();
var pic = workbook.AddPicture(bytes, PictureType.PNG);
var anchor = new HSSFClientAnchor();
anchor.SetAnchor(
short.Parse(Col1.Text),
int.Parse(Row1.Text),
int.Parse(X1.Text),
int.Parse(Y1.Text),
short.Parse(Col2.Text),
int.Parse(Row2.Text),
int.Parse(X2.Text),
int.Parse(Y2.Text));
anchor.AnchorType = AnchorType.MoveAndResize;
drawing.CreatePicture(anchor, pic);
using (var fs = new FileStream(newPath, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
}
参数说明:
转载时请注明出处!