如何在Excel表格与c#中的图像设置单元格?

我正在尝试使用Bytescout库将图像插入到Excel表格中。 但是这并没有发生。

我的要求是创build一个新的Excel文件 ,然后插入数据,包括图像 。 我只是想要任何一种方法,就像任何一个库不仅Bytescout。

有人能帮我吗?

您可以使用以下代码将图像和图表添加到使用ByteScout Spreadsheet SDK生成的XLX / XLSX电子表格中 :

在Visual Basic .NET中:

Imports System.Collections.Generic Imports System.Diagnostics Imports System.IO Imports System.Text Imports Bytescout.Spreadsheet Class Program Friend Shared Sub Main(args As String()) ' Create spreadsheet Dim doc As New Spreadsheet() ' Add worksheet Dim worksheet As Worksheet = doc.Worksheets.Add() ' Put an image on the worksheet with 10 pixel margin from the top-left corner of the worksheet worksheet.Pictures.Add("image1.jpg", 10, 10) ' Put second image to 200 pixel offset and resize it to 250x200 px worksheet.Pictures.Add("image2.jpg", 200, 200, 250, 200) ' Save document doc.SaveAs("output.xls") ' Close spreadsheet doc.Close() ' Open generated XLS document in default application Process.Start("output.xls") doc.Dispose() End Sub End Class 

而在C#中:

 using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; using Bytescout.Spreadsheet; using Bytescout.Spreadsheet.MSODrawing; namespace AddImages { class Program { static void Main(string[] args) { // Create spreadsheet Spreadsheet doc = new Spreadsheet(); // Add worksheet Worksheet worksheet = doc.Worksheets.Add(); // Put an image to "C3" cell PictureShape shape = worksheet.Pictures.Add(2, 2, "image1.jpg"); // Make the picture "floating". It will be not moved if you move or resize the "C3" cell shape.PlacementType = Placement.FreeFloating; // Make the picture brighter shape.Brightness = 0.8f; // Put second image to "K11" cell shape = worksheet.Pictures.Add(10, 10, "image2.jpg"); // Make the picture bound to the cell. It will be moved along with the "K11" cell shape.PlacementType = Placement.Move; // Crop 10% from left and right side of the image shape.CropFromLeft = 0.1f; shape.CropFromRight = 0.1f; // Save document doc.SaveAs("output.xls"); // Close spreadsheet doc.Close(); // Open generated XLS document in default application Process.Start("output.xls"); doc.Dispose(); } } } 

有关更多代码示例,请浏览Spreadsheet SDK联机文档 – *高级示例.. *部分,以获取更多源代码示例,其中包括向新电子表格和现有电子表格添加图像,添加图表等。