Devexpress SpreadSheet 如何在工作表中插入图片

本文介绍了使用 DevExpress Office 库在工作表中插入、移动、调整大小及删除图片的方法,并展示了如何设置图片属性如名称、边框等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于文件流法:
workbook.BeginUpdate();
// Set the measurement unit to Millimeter.
workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter;
try
{
    Worksheet worksheet = workbook.Worksheets[0];
    // Insert a picture from a file so that its top left corner is in the specified cell.
    // By default the picture is named Picture 1.. Picture NN.
    worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", worksheet.Cells["A1"]);
    // Insert a picture at 70 mm from the left, 40 mm from the top, 
    // and resize it to a width of 85 mm and a height of 25 mm, locking the aspect ratio.
    worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", 70, 40, 85, 25, true);
    // Insert the picture to be removed.
    worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", 0, 0);
    // Remove the last inserted picture.
    // Find the shape by its name. The method returns a collection of shapes with the same name.
    Picture picShape = worksheet.Pictures.GetPicturesByName("Picture 3")[0];
    picShape.Delete();
}
finally
{
    workbook.EndUpdate();
}

基于URL法:

string imageUri = "https://www.devexpress.com/Products/NET/Controls/WinForms/spreadsheet/i/winforms-spreadsheet-control.png";
// Create an image from Uri.
SpreadsheetImageSource imageSource = SpreadsheetImageSource.FromUri(imageUri, workbook);
// Set the measurement unit to point.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;

workbook.BeginUpdate();
try
{
    Worksheet worksheet = workbook.Worksheets[0];
    // Insert a picture from the SpreadsheetImageSource at 100 pt from the left, 40 pt from the top, 
    // and resize it to a width of 300 pt and a height of 200 pt.
    worksheet.Pictures.AddPicture(imageSource, 100, 40, 300, 200);
}
finally
{
    workbook.EndUpdate();
}

如何修改:

workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter;
workbook.BeginUpdate();
try
{
    Worksheet worksheet = workbook.Worksheets[0];
    // Insert pictures from the file.
    Picture pic = worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", worksheet.Cells["A1"]);
    // Specify picture name and draw a border.
    pic.Name = "Logo";
    pic.AlternativeText = "Spreadsheet Logo";
    pic.BorderWidth = 1;
    pic.BorderColor = DevExpress.Utils.DXColor.Black;
    // Move a picture.
    pic.Move(20, 30);
    // Change picture behavior so it will move and size with underlying cells. 
    pic.Placement = Placement.MoveAndSize;
    worksheet.Rows[5].Height += 10;
    worksheet.Columns["D"].Width += 10;
    // Specify rotation angle.
    pic.Rotation = 30;
    // Add a hyperlink.
    pic.InsertHyperlink("http://www.devexpress.com/Products/NET/Document-Server/", true);
}
finally
{
    workbook.EndUpdate();
}



### 关于DevExpress Spreadsheet控件的使用教程 DevExpress Spreadsheet 控件是一款功能强大的工具,用于创建类似于 Excel 的电子表格应用程序。它支持多种操作,包括但不限于从数据库加载和保存文档、自定义界面行为以及优化单元格内容显示。 #### 1. **DevExpress Spreadsheet 报表模板演示源码** 如果希望获取 DevExpress Spreadsheet 的实际应用案例,可以参考官方提供的报表模板演示源码[^1]。此资源位于 GitCode 平台上的开源项目中,提供了关于 `SpreadSheetControl` 的配置和实现方法的具体实例。通过该项目,开发者能够快速上手并理解如何高效利用该控件完成复杂报表的设计与开发。 访问地址如下: ```plaintext https://gitcode.com/open-source-toolkit/34b6c ``` --- #### 2. **从数据库保存和加载文档的功能实现** 为了使 Spreadsheet 能够无缝集成到基于 Web 的应用程序中,可以通过 Razor 视图中的辅助函数来简化其初始化过程[^2]。以下是具体的代码片段: 在视图层中调用 Spreadsheet 辅助器的方法: ```html @Html.DevExpress().Spreadsheet(SpreadsheetSettingsHelper.GetSpreadsheetSettings()) .Open(Model.DocumentId, Model.DocumentFormat, () => { return Model.Document; }) .GetHtml() ``` 而在控制器端,则需处理回调请求以更新或返回数据: ```csharp public ActionResult SpreadsheetPartial() { // 处理 Spreadsheet 的 CallbackRouteAction 请求 return SpreadsheetExtension.GetCallbackResult( SpreadsheetSettingsHelper.GetSpreadsheetSettings()); } ``` 上述代码展示了如何将 Spreadsheet 集成至 ASP.NET MVC 应用程序,并实现了动态加载文档的核心逻辑。 --- #### 3. **禁用工作表添加功能** 有时出于安全或其他原因考虑,需要限制最终用户对某些功能的操作权限。例如,在特定场景下可完全隐藏新增工作表的能力。这可通过调整控件的行为选项轻松达成[^3]: ```csharp this.spreadsheetControl1.Options.Behavior.Worksheet.Insert = DevExpress.XtraSpreadsheet.DocumentCapability.Hidden; ``` 以上代码片段的作用在于关闭用户的“插入工作表”的能力,从而增强系统的可控性和一致性。 --- #### 4. **解决单元格宽度不足的问题** 当向单元格填充大量文本时,可能会遇到内容超出边界的情况。为了避免手动逐一设定每列宽度带来的不便,建议启用自动适配机制[^4]。具体做法如下所示: ```csharp // 获取当前 Workbook 中已使用的区域范围 var usedRange = ws.GetUsedRange(); // 根据内容自动调整列宽 usedRange.AutoFitColumns(); // 同样适用于行高的自动化调节 usedRange.AutoFitRows(); ``` 这种方法不仅提高了用户体验,还减少了因固定尺寸设置不当而导致的内容截断风险。 --- ### 总结 综上所述,DevExpress Spreadsheet 提供了一系列丰富的特性,涵盖了从基础的数据展示到高级交互控制等多个方面。无论是初学者还是经验丰富的开发者都可以从中受益匪浅。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值