图像
将工作簿转换为TIFF
Excel 文件可以包含多个具有多个页面的工作表。 WorkbookRender 允许您将 Excel 转换为带有多个页面的 TIFF。 您还可以控制多个 TIFF 的选项,例如压缩、颜色深度、分辨率(水平分辨率、垂直分辨率)。
以下代码片段显示了如何将Excel转换为具有多个页面的TIFF。源Excel文件和生成的TIFF图像附在此供参考。
Workbook wb = new Workbook("workbook-to-tiff-with-mulitiple-pages.xlsx"); | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
imgOptions.ImageType = ImageType.Tiff; | |
//set Resolution to 200 | |
imgOptions.HorizontalResolution = 200; | |
imgOptions.VerticalResolution = 200; | |
//set TIFF compression to Lzw. | |
imgOptions.TiffCompression = TiffCompression.CompressionLZW; | |
WorkbookRender workbookRender = new WorkbookRender(wb, imgOptions); | |
workbookRender.ToImage("workbook-to-tiff-with-mulitiple-pages.tiff"); |
将工作表转换为图像
工作表包含您想要分析的数据。例如,工作表可以包含参数、总计、百分比、异常和计算。
作为开发人员,您可能需要将工作表呈现为图像。例如,您可能需要在应用程序或网页中使用工作表的图像。您可能希望将图像插入到 Microsoft Word 文档、PDF 文件、PowerPoint 演示文稿或其他文档类型中。简而言之,您希望将工作表呈现为图像,以便在其他地方使用它。
Aspose.Cells 支持将 Excel 工作表转换为图像。 要使用此功能,您需要将 Aspose.Cells.Rendering 命名空间导入到您的程序或项目中。 它有几个有价值的类用于渲染和打印,例如 SheetRender、ImageOrPrintOptions、WorkbookRender 等。
SheetRender 类代表要渲染为图像的工作表。 它有一个重载方法,ToImage,可以将工作表转换为具有不同属性或选项的图像文件。 它返回一个 System.Drawing.Bitmap 对象,您可以将图像文件保存到磁盘或流中。 支持几种图像格式,例如 BMP、PNG、GIF、JPG、JPEG、TIFF、EMF。
以下代码片段显示了如何将Excel文件中的工作表转换为图像文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
Workbook book = new Workbook(sourceDir + "sampleConvertWorksheetToImageByPage.xlsx"); | |
Worksheet sheet = book.Worksheets[0]; | |
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions(); | |
options.HorizontalResolution = 200; | |
options.VerticalResolution = 200; | |
options.ImageType = Drawing.ImageType.Tiff; | |
// Sheet2Image By Page conversion | |
SheetRender sr = new SheetRender(sheet, options); | |
for (int j = 0; j < sr.PageCount; j++) | |
{ | |
sr.ToImage(j, outputDir + "outputConvertWorksheetToImageByPage_" + (j + 1) + ".tif"); | |
} |
将工作表转换为SVG
SVG代表可缩放矢量图形。SVG是基于XML标准的二维矢量图形规范。自1999年以来,它一直是由万维网联盟(W3C)开发的开放标准。
自 7.1.0 版本以来,Aspose.Cells for .NET 已能够将工作表转换为 SVG 图像。以下代码片段显示了如何将 Excel 文件中的工作表转换为 SVG 图像文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiate a workbook | |
var workbook = new Workbook(); | |
// Put sample text in the first cell of first worksheet in the newly created workbook | |
workbook.Worksheets[0].Cells["A1"].Value = "DEMO TEXT ON SHEET1"; | |
// Add second worksheet in the workbook | |
workbook.Worksheets.Add(SheetType.Worksheet); | |
// Set text in first cell of the second sheet | |
workbook.Worksheets[1].Cells["A1"].Value = "DEMO TEXT ON SHEET2"; | |
// Set currently active sheet incex to 1 i.e. Sheet2 | |
workbook.Worksheets.ActiveSheetIndex = 1; | |
// Save workbook to SVG. It shall render the active sheet only to SVG | |
workbook.Save(outputDir + "ConvertWorksheetToSVG_out.svg"); |