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 kontrolliert werden, was in den folgenden Abschnitten detaillierter erläutert wird.

Dieser Artikel erklärt, wie man Bilder hinzufügt und wie man ein Bild einfügt, das den Inhalt bestimmter Zellen zeigt.

Bilder hinzufügen

Das Hinzufügen von Bildern zu einer Tabelle ist sehr einfach. Es dauert nur wenige Zeilen Code: Rufen Sie einfach die Add-Methode der Pictures-Sammlung (die im Worksheet-Objekt verkapselt ist) auf. Die Add-Methode akzeptiert die folgenden Parameter:

  • Index der oberen linken Zeile, der Index der oberen linken Zeile.
  • Index der oberen linken Spalte, der Index der oberen linken Spalte.
  • Bilddateiname, der Name der Bilddatei inklusive des Pfads.
// 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ögliche Methoden, um die Positionierung von Bildern mithilfe von Aspose.Cells zu kontrollieren:

  • 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 werden soll, z.B. 40 Pixel links und 20 Pixel unterhalb des Zellrandes.

Proportionale Positionierung

Entwickler können die Bilder proportional zur Zeilenhöhe und Spaltenbreite mithilfe der UpperDeltaX- und UpperDeltaY-Eigenschaften des Aspose.Cells.Drawing.Picture-Objekts positionieren. Ein Picture-Objekt kann aus der Pictures-Sammlung erhalten werden, indem sein Bildindex übergeben wird. 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 auch die Bilder absolut positionieren, indem sie die Eigenschaften Left und Top des Picture-Objekts verwenden. Dieses Beispiel platziert ein Bild in der Zelle F6, 60 Pixel von links und 10 Pixel von oben der Zelle entfernt.

// 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 Zellverweis

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

Fügen Sie ein Bild zum Arbeitsblatt hinzu, indem Sie die AddPicture-Methode der ShapeCollection-Sammlung aufrufen (die im Worksheet-Objekt verkapselt ist). Geben Sie den Zellenbereich mit dem Formula-Attribut des Picture-Objekts an.

// 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");

Erweiterte Themen