إدارة الصور
Aspose.Cells يسمح للمطورين بإضافة الصور إلى جداول البيانات في وقت التشغيل. علاوة على ذلك ، يمكن التحكم في موضع هذه الصور في وقت التشغيل ، وهو ما سيتم مناقشته بمزيد من التفصيل في الأقسام القادمة.
تشرح هذه المقالة كيفية إضافة الصور وكيفية إدراج صورة تعرض محتوى خلايا معينة.
مضيفا الصور
من السهل جدًا إضافة الصور إلى جدول البيانات. لا يتطلب الأمر سوى بضعة أسطر من التعليمات البرمجية: ما عليك سوى الاتصال بـيضيف طريقةالصور مجموعة (مغلفة في ملفورقة عمل موضوع). اليضيفتأخذ الطريقة المعلمات التالية:
- فهرس الصف العلوي الأيسر، فهرس الصف العلوي الأيسر.
- فهرس العمود الأيسر العلوي، فهرس العمود الأيسر العلوي.
- اسم ملف الصورة، اسم ملف الصورة ، كامل مع المسار.
// 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:
- تحديد الموضع النسبي: تحديد موضع يتناسب مع ارتفاع الصف وعرضه.
- تحديد الموضع المطلق: حدد الموضع الدقيق على الصفحة حيث سيتم إدراج الصورة ، على سبيل المثال ، 40 بكسل إلى اليسار و 20 بكسل أسفل حافة الخلية.
التموضع النسبي
يمكن للمطورين وضع الصور بما يتناسب مع ارتفاع الصف وعرض العمود باستخدام امتدادUpperDeltaX وUpperDeltaY خصائصAspose.Cells.Drawing.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"); |
تحديد المواقع المطلقة
يمكن للمطورين أيضًا وضع الصور تمامًا باستخدام ملفاليسار وقمة خصائصصورةموضوع. يضع هذا المثال صورة في الخلية 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"); |
إدراج صورة بناءً على مرجع Cell
Aspose.Cells يتيح لك عرض محتويات خلية ورقة عمل في شكل صورة. يمكنك ربط الصورة بالخلية التي تحتوي على البيانات التي تريد عرضها. نظرًا لأن الخلية أو نطاق الخلية مرتبط بكائن رسومي ، فإن التغييرات التي تجريها على البيانات الموجودة في تلك الخلية أو نطاق الخلايا تظهر تلقائيًا في الكائن الرسومي.
أضف صورة إلى ورقة العمل عن طريق استدعاءإضافة الصورة طريقةShapeCollection مجموعة (مغلفة في ملفورقة عمل موضوع). حدد نطاق الخلايا باستخداممعادلة سمة من سماتصورةموضوع.
// 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"); |