原創|其它|編輯:郝浩|2011-09-05 11:22:50.000|閱讀 3154 次
概述:當你需要在應用程序或者網頁中呈現工作薄圖像時,你可能需要將圖像插入到Word文檔,PDF文件和Power Point演示中。一旦你將工作表渲染成圖像,這樣一來,你就可以輕松地將圖像粘貼到你的應用程序中。Aspose.Cells組件便提供了將Excel中的工作表轉換成圖像的功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
當你需要在應用程序或者網頁中呈現工作薄圖像時,你可能需要將圖像插入到Word文檔,PDF文件和Power Point演示中。一旦你將工作表渲染成圖像,這樣一來,你就可以輕松地將圖像粘貼到你的應用程序中。Aspose.Cells組件便提供了將Excel中的工作表轉換成圖像的功能。
Aspose.Cells.Rendering.SheetRender API可以將一個工作表轉換成你所指定屬性的圖像,例如,Imageformat類型,分頁表等。Aspose.Cells中支持多種圖像格式,所以你可以從bmp, .gif, .jpg, .jpeg, .tiff, .emf等格式中任選其一。
我們使用一個簡單的Excel文件。MS Excel中的預覽如下圖所示。你可以看見空白的區域圍繞著數據。你可以在輸出圖像中刪除數據周圍的空白區域。
[C#]
//Instantiate a workbook
//Open the template file
Workbook book = new Workbook(@"e:\test\MyTestBook1.xls");
//Get the first worksheet
Worksheet sheet = book.Worksheets[0];
//Specify your print area if you want
//sheet.PageSetup.PrintArea = "A1:H8";
//To remove the white border around the image.
sheet.PageSetup.LeftMargin = 0;
sheet.PageSetup.RightMargin = 0;
sheet.PageSetup.BottomMargin = 0;
sheet.PageSetup.TopMargin = 0;
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.ImageFormat = ImageFormat.Emf;
//Set only one page would be rendered for the image
imgOptions.OnePagePerSheet = true;
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
//Create the SheetRender object based on the sheet with its
//ImageOrPrintOptions attributes
SheetRender sr = new SheetRender(sheet, imgOptions);
//Convert the image
sr.ToImage(0, "e:\\test\\img_MyTestBook1.emf");
[VB.NET]
'Instantiate a workbook
'Open the template file
Dim book As New Workbook("e:\test\MyTestBook1.xls")
'Get the first worksheet
Dim sheet As Worksheet = book.Worksheets(0)
'Specify your print area if you want
'sheet.PageSetup.PrintArea = "A1:H8";
'To remove the white border around the image.
sheet.PageSetup.LeftMargin = 0
sheet.PageSetup.RightMargin = 0
sheet.PageSetup.BottomMargin = 0
sheet.PageSetup.TopMargin = 0
'Define ImageOrPrintOptions
Dim imgOptions As New ImageOrPrintOptions()
imgOptions.ImageFormat = ImageFormat.Emf
'Set only one page would be rendered for the image
imgOptions.OnePagePerSheet = True
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank
'Create the SheetRender object based on the sheet with its
'ImageOrPrintOptions attributes
Dim sr As New SheetRender(sheet, imgOptions)
'Convert the image
sr.ToImage(0, "e:\test\img_MyTestBook1.emf")
通過上面的代碼,下面是以Windows圖片和傳真查看器方式查看所生成的圖片:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網