画像の管理
Aspose.Cellsを使用すると、開発者は実行時にスプレッドシートに画像を追加できます。さらに、これらの画像の位置を実行時に制御することができます。これについては後のセクションで詳しく説明します。
この記事では、画像の追加方法と特定のセルの内容を示す画像の挿入方法について説明します。
画像の追加
スプレッドシートに写真を追加するのは非常に簡単です。わずかなコード行だけで済みます: 単純に、Picturesコレクション(Worksheetオブジェクトでカプセル化)のAddメソッドを呼び出します。Addメソッドには以下のパラメータが必要です:
- 左上の行インデックス、左上の行のインデックス。
- 左上の列インデックス、左上の列のインデックス。
- 画像ファイル名、パスを含む画像ファイルの名前。
// 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"); |
写真の位置合わせ
Aspose.Cellsを使用して写真の位置合わせを制御する方法には2つの方法があります:
- 比例位置合わせ:行の高さと幅に比例した位置を定義します。
- 絶対位置合わせ:ページ上の画像の挿入位置を正確に定義します。例:セルの左から40ピクセル、上から20ピクセル。
比例位置合わせ
開発者は、Aspose.Cells.Drawing.PictureオブジェクトのUpperDeltaXおよびUpperDeltaYプロパティを使用して、行の高さと列の幅に比例した位置に写真を配置できます。Picturesコレクションからその写真のインデックスを渡すことでPictureオブジェクトを取得できます。この例では、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"); |
絶対位置づけ
開発者は、PictureオブジェクトのLeftおよびTopプロパティを使用して、絶対位置に写真を配置することもできます。この例では、セルF6に画像を配置し、左から60ピクセル、上から10ピクセルに配置します。
// 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"); |
セル参照に基づいて画像を挿入
Aspose.Cellsを使用すると、ワークシートのセルの内容を画像形状で表示できます。画像は、データを表示したいセルにリンクされています。セルまたはセル範囲がグラフィックオブジェクトにリンクされているため、そのセルまたはセル範囲のデータを変更すると、自動的にグラフィックオブジェクトに変更が反映されます。
Worksheetオブジェクトでカプセル化されたShapeCollectionコレクションのAddPictureメソッドを呼び出すことで、ワークシートに画像を追加します。PictureオブジェクトのFormula属性を使用してセル範囲を指定します。
// 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"); |