dll文件獲取及引用:
創(chuàng)新互聯(lián)專注于永福網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供永福營銷型網(wǎng)站建設,永福網(wǎng)站制作、永福網(wǎng)頁設計、永福網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務,打造永福網(wǎng)絡公司原創(chuàng)品牌,更為您提供永福網(wǎng)站排名全網(wǎng)營銷落地服務。
方法1:通過官網(wǎng)下載dll文件包,并解壓。解壓文件后,將bin文件夾下的Spire.Xls.dll文件引用到C#程序。
方法2:通過Nuget網(wǎng)站獲取dll。
C#代碼示例
using Spire.Xls; using System.Drawing; namespace InsertImage_XLS { class Program { static void Main(string[] args) { //創(chuàng)建Workbook對象 Workbook workbook = new Workbook(); //獲取第一張工作表 Worksheet sheet = workbook.Worksheets[0]; //指定列寬、行高 sheet.Columns[0].ColumnWidth = 50; sheet.Rows[0].RowHeight = 160; //加載圖片,添加到指定單元格 ExcelPicture picture = sheet.Pictures.Add(1,1, "tp.png"); //指定圖片寬度和高度 picture.Width = 300; picture.Height = 180; //通過LeftColumnOffset和TopRowOffset屬性值設置圖片在單元格中的橫向、縱向?qū)R picture.LeftColumnOffset = 75; picture.TopRowOffset = 20; //保存文檔 workbook.SaveToFile("result.xlsx", FileFormat.Version2013); System.Diagnostics.Process.Start("result.xlsx"); } } }
圖片插入效果:
擴展知識點:
C# 導入圖片到Excel單元格
導出Excel功能時需要將數(shù)據(jù)中含有圖片也導出到excel單元格中
public void InsertPicture(string RangeName, string PicturePath, Microsoft.Office.Interop.Excel.Worksheet worksheet) { Microsoft.Office.Interop.Excel.Range m_objRange = worksheet.get_Range(RangeName, Type.Missing); m_objRange.Select(); float PicLeft, PicTop, PicWidth, PicHeight; //距離左邊距離,頂部距離,圖片寬度、高度 PicTop = Convert.ToSingle(m_objRange.Top); PicWidth = Convert.ToSingle(m_objRange.MergeArea.Width); PicHeight = Convert.ToSingle(m_objRange.Height)-10; PicWidth = Convert.ToSingle(m_objRange.Width)-10; PicLeft = Convert.ToSingle(m_objRange.Left);//+ (Convert.ToSingle(rng.MergeArea.Width) - PicWidth) / 2; try { //worksheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, PictuteWidth, PictureHeight); worksheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, 90, 70); } catch { } }