Bilder verwalten

Aspose.Cells ermöglicht es Entwicklern, Bilder zur Laufzeit zu Tabellenkalkulationen hinzuzufügen. Darüber hinaus kann die Positionierung dieser Bilder zur Laufzeit gesteuert werden, was in den nächsten Abschnitten näher erläutert wird.

In diesem Artikel wird erläutert, wie Sie Bilder hinzufügen und wie Sie ein Bild einfügen, das den Inhalt bestimmter Zellen zeigt.

Bilder hinzufügen

Das Hinzufügen von Bildern zu einer Tabelle ist sehr einfach. Es dauert nur ein paar Zeilen Code: Einfach anrufenAddieren Methode derBilder Sammlung (eingekapselt in derArbeitsblatt Objekt). DasAddierenDie Methode nimmt die folgenden Parameter an:

  • Zeilenindex oben links, der Index der oberen linken Zeile.
  • Spaltenindex oben links, der Index der oberen linken Spalte.
  • Name der Bilddatei, der Name der Bilddatei, komplett mit Pfad.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding a picture at the location of a cell whose row and column indices
// Are 5 in the worksheet. It is "F6" cell
worksheet.Pictures.Add(5, 5, dataDir + "logo.jpg");
// Saving the Excel file
workbook.Save(dataDir + "output.xls");

Bilder positionieren

Es gibt zwei Möglichkeiten, die Positionierung von Bildern mit Aspose.Cells zu steuern:

  • Proportionale Positionierung: Definieren Sie eine Position proportional zur Zeilenhöhe und -breite.
  • Absolute Positionierung: Definieren Sie die genaue Position auf der Seite, an der das Bild eingefügt wird, z. B. 40 Pixel links und 20 Pixel unterhalb des Rands der Zelle.

Proportionale Positionierung

Entwickler können die Bilder mithilfe von proportional zur Zeilenhöhe und Spaltenbreite positionierenUpperDeltaX undUpperDeltaY Eigenschaften derAspose.Cells.Drawing.Picture Objekt. EINBild Objekt kann von der bezogen werdenBilderSammlung durch Übergabe ihres Bildindexes. Dieses Beispiel platziert ein Bild in der Zelle F6.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding a picture at the location of a cell whose row and column indices
// Are 5 in the worksheet. It is "F6" cell
int pictureIndex = worksheet.Pictures.Add(5, 5, dataDir + "logo.jpg");
// Accessing the newly added picture
Aspose.Cells.Drawing.Picture picture = worksheet.Pictures[pictureIndex];
// Positioning the picture proportional to row height and colum width
picture.UpperDeltaX = 200;
picture.UpperDeltaY = 200;
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Absolute Positionierung

Entwickler können die Bilder auch absolut positionieren, indem sie die verwendenLinks undoben Eigenschaften derBildObjekt. Dieses Beispiel platziert ein Bild in Zelle F6, 60 Pixel vom linken und 10 Pixel vom oberen Rand der Zelle.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding a picture at the location of a cell whose row and column indices
// Are 5 in the worksheet. It is "F6" cell
int pictureIndex = worksheet.Pictures.Add(5, 5, dataDir + "logo.jpg");
// Accessing the newly added picture
Aspose.Cells.Drawing.Picture picture = worksheet.Pictures[pictureIndex];
// Absolute positioning of the picture in unit of pixels
picture.Left = 60;
picture.Top = 10;
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Einfügen eines Bildes basierend auf der Referenz Cell

Mit Aspose.Cells können Sie den Inhalt einer Arbeitsblattzelle in einer Bildform anzeigen. Sie können das Bild mit der Zelle verknüpfen, die die Daten enthält, die Sie anzeigen möchten. Da die Zelle oder der Zellbereich mit dem Grafikobjekt verknüpft ist, werden Änderungen, die Sie an den Daten in dieser Zelle oder diesem Zellbereich vornehmen, automatisch im Grafikobjekt angezeigt.

Fügen Sie dem Arbeitsblatt ein Bild hinzu, indem Sie die aufrufenBild hinzufügen Methode derShapeCollection Sammlung (eingekapselt in derArbeitsblatt Objekt). Geben Sie den Zellbereich mithilfe von anFormel Attribut derBildObjekt.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet's cells collection
Cells cells = workbook.Worksheets[0].Cells;
// Add string values to the cells
cells["A1"].PutValue("A1");
cells["C10"].PutValue("C10");
// Add a blank picture to the D1 cell
Picture pic = workbook.Worksheets[0].Shapes.AddPicture(0, 3, 10, 6, null);
// Specify the formula that refers to the source range of cells
pic.Formula = "A1:C10";
// Update the shapes selected value in the worksheet
workbook.Worksheets[0].Shapes.UpdateSelectedValue();
// Save the Excel file.
workbook.Save(dataDir + "output.out.xls");

Themen vorantreiben