将工作表渲染到图形上下文
Contents
[
Hide
]
Aspose.Cells 现在可以将工作表呈现为图形上下文。图形上下文可以是任何图像文件、屏幕或打印机等。请使用以下两种方法之一将工作表渲染到图形上下文。
以下代码说明如何使用 Aspose.Cells 将工作表呈现为图形上下文。执行代码后,它将打印整个工作表并在图形上下文中用蓝色填充剩余的空白区域并将图像另存为**OutputImage_out_.png**文件。您可以使用任何源 excel 文件来尝试此代码。另请阅读代码中的注释以便更好地理解。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 workbook object from source file | |
Workbook workbook = new Workbook(dataDir + "SampleBook.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Create empty Bitmap | |
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(1100, 600); | |
// Create Graphics Context | |
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp); | |
g.Clear(System.Drawing.Color.Blue); | |
// Set one page per sheet to true in image or print options | |
Aspose.Cells.Rendering.ImageOrPrintOptions opts = new Aspose.Cells.Rendering.ImageOrPrintOptions(); | |
opts.OnePagePerSheet = true; | |
// Render worksheet to graphics context | |
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(worksheet, opts); | |
sr.ToImage(0, g, 0, 0); | |
// Save the graphics context image in Png format | |
bmp.Save(dataDir + "OutputImage_out.png", System.Drawing.Imaging.ImageFormat.Png); |