第一种方式InsertPicToExcel类的地址:https://blog.csdn.net/zhu58252601/article/details/101026675
#region //第一种方式
//InsertPicToExcel ipt = new InsertPicToExcel();
//ipt.Open();
//ipt.InsertPicture("B2", @"F:\1.png");
//ipt.InsertPicture("B18", @"F:\1.png", 100, 80);
//ipt.SaveFile(@"F:\excel1.xlsx");
//ipt.Dispose();
#endregion
#region //第二种方式
//创建Applicaton对象
//Excel.Application xApp = new Excel.Application();
//xApp.Visible = true;
//#region 打开excel文档
得到workbook对象,可以用两种方式之一:下面是打开已有的excel文件
//Excel.Workbook xBook = xApp.Workbooks.Open(@"F:\excel.xlsx",
// Missing.Value, Missing.Value, Missing.Value, Missing.Value,
// Missing.Value, Missing.Value, Missing.Value, Missing.Value,
// Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//#endregion
指定要操作的Sheet,两种方式:
//Excel.Worksheet xSheet = xBook.Sheets[1];
Excel.Worksheet xSheet = xApp.ActiveSheet;
读取数据,通过Range对象
//Excel.Range rng1 = xSheet.get_Range("A1", Type.Missing);
//if (rng1.Value2 != null)
// Console.WriteLine(rng1.Value2);
读取数据,通过Range对象,但使用不同的接口得到Range
//Excel.Range rng2 = (Excel.Range)xSheet.Cells[1, 3]; //得到第1行第3列的值 cells[行,列]
//if (rng2.Value2 != null)
// Console.WriteLine(rng2.Value2);
动态写入数据,将数据写入到不同的单元格
//int i = 0;
//for (i = 1; i < 10; i++)
//{
// Excel.Range rng4 = (Excel.Range)xSheet.Cells[2, i];
// rng4.Value2 = i;
// rng4.Interior.ColorIndex = 6; //设备Range的背景色
//}
保存 :
//xBook.Save();
//xSheet = null;
//xBook = null;
//xApp.Quit(); //这一句非常重要,否则Excel对象不能从内存中退出
//xApp = null;
//Console.ReadKey();
#endregion
#region//第三种方式
//IWorkbook workbook = new HSSFWorkbook();
//ISheet sheet = workbook.CreateSheet("Sheet1");
//IDrawing patriarch = sheet.CreateDrawingPatriarch();
//string setVal = @"F:\1.png";
将图片文件读入一个字符串
//byte[] bytes = System.IO.File.ReadAllBytes(setVal);
//int pictureIdx = sheet.Workbook.AddPicture(bytes, PictureType.JPEG);
//IClientAnchor anchor = null;
//IPicture pict = null;
//int dx1 = 0, dy1 = 0, dx2 = 1023, dy2 = 255;
//int col1 = 1, row1 = 1, col2 = 1, row2 = 1;
图片可能会合并多个单元格,需要计算合并的跨度
//if (sheet.GetRow(row1).GetCell(col1) != null && sheet.GetRow(row1).GetCell(col1).IsMergedCell)
//{
// int rowSpan = 0;
// int colSpan = 0;
// int regionsCount = sheet.NumMergedRegions;
// for (int i = 0; i < regionsCount; i++)
// {
// CellRangeAddress range = sheet.GetMergedRegion(i);
// sheet.IsMergedRegion(range);
// if (range.FirstRow == row1 && range.FirstColumn == col1)
// {
// rowSpan = range.LastRow - range.FirstRow + 1;
// colSpan = range.LastColumn - range.FirstColumn + 1;
// break;
// }
// }
// col2 = col1 + colSpan - 1;
// row2 = row1 + rowSpan - 1;
//}
//anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
把图片插到相应的位置
//pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
#endregion
#region//第四种方式,用epplus
FileInfo file = new FileInfo(@"D:\myExcel1.xlsx");
ExcelPackage package = new ExcelPackage(file);
Image logo = Image.FromFile(@"F:\1.png");
var ws = package.Workbook.Worksheets[1];
//int maxRowNum = ws.Dimension.End.Row;//最大行
for (int a = 0; a < 1; a++)
{
ws.Row(a * 5).Height = 39.00D;
var picture = ws.Drawings.AddPicture(a.ToString(), logo);
picture.SetPosition(a * 5, 0, 2, 0);
picture.EditAs=OfficeOpenXml.Drawing.eEditAs.TwoCell;//设定图片随单元格变动
}
package.Save();
#endregion