Excel圖表轉成圖片


關於excel 圖表轉成圖片

 

知識點:excel 生成的圖表不是圖片

嘗試.    通過Java調用POI接口挺難把excel生成的圖表轉成圖片導出來

ps.      其它生成圖表的工具,如jfreechart,參考鏈接:http://www.open-open.com/lib/view/open1365997415828.html

       但是生成的圖表會和Excel生成的有差異

辦法一:excel中創建宏,將圖表生成圖片到指定目錄,

           只需要幾行代碼:

           Sub SaveChartAsGIF ()
    Fname = ThisWorkbook.Path & "\" & ActiveChart.Name & ".gif"
    ActiveChart.Export FileName:=Fname, FilterName:="GIF"
    End Sub

    參考鏈接: http://soft.yesky.com/office/122/2296622.shtml 

辦法二:VBA編程:

    用VBA先轉成圖片,並為有規律的圖片名,插入PPT如果也用VBA實現,那只要按2次快捷鍵(包括轉換圖片)。

    當然也可一次在PPT中實現,只要在PPT的VBA中CreateObject("Excel.application")。

辦法三:perl編程:

    用PERL則只要寫個腳本運行一次,就可以把excel中的圖表生成圖片,並轉到ppt中(PERL中使用Win32-OLE和Win32-PowerPoint模塊)。

    用PERL還能很簡單地實現圖片縮放和排版,一句代碼搞定:

    $pp->add_picture($picture, { left => $left, top => $top ,width=>650,height=>300})。

    方法二、三的思路 參考:http://club.excelhome.net/thread-250076-1-1.html  中aef25uuu 的回復

      

    代碼實現:

    一、      save a chart from Microsoft Excel as GIF/JPEG/PNG

           參考:http://www.uni-hildesheim.de/rz/DOC/perl/html/faq/Windows/ActivePerl-Winfaq12.html  

    中的“How do I save a chart from Microsoft Excel as GIF/JPEG/PNG?”部分

           代碼:

    
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const;
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;        # die on errors...

my $filename = 'E:\\visual box\\share\\perl\\DataTemplate1.xlsx';
my $filter = 'JPG';           # can be GIF, JPG, JPEG or PNG
my $count = 0;

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit');  # use the Excel application if it's open, otherwise open new
my $Book = $Excel->Workbooks->Open( $filename );      # open the file
foreach my $Sheet (in $Book->Sheets) {                # loop through all sheets
    foreach my $ChartObj (in $Sheet->ChartObjects) {  # loop through all chartobjects in the sheet
        my $savename = "$filename." . $count++ . ".$filter";
        $ChartObj->Chart->Export({
            FileName    => $savename,
            FilterName  => $filter,
            Interactive => 0});
    }
}
$Book->Close;
ExcelChartToImage

    二、      perl 把excel轉成html

    參考:http://zyj4538.blog.163.com/blog/static/2765753220112285537776/

    

    遇到問題:

    一、  

        描述:Perl lib version <v5.8.3> doesn't match executable version <v5.8.8>

           at E:\oracle\product\10.2.0\db_1\perl\5.8.3\lib....

        原因:之前安裝過oracle,其中默認安裝了perl;現在安裝的perl與其沖突了,

        解決辦法:將現在perl安裝目錄下lib目錄中的Config.pm、Config.pod拷貝到oracle中對應的perl下:

        E:\oracle\product\10.2.0\db_1\perl\5.8.3\lib\MSWin32-x86-multi-thread

    二、  

        描述:運行上述寫好的pl代碼時,出現這個錯誤

        原因:本機上安裝的Excel的問題,在別的電腦上運行時成功地生成了圖片,可能是由於本機Excel是精簡版的,W32-OLE獲取不到Excel的com object

        解決辦法:安裝原裝版的Excel,而不是精簡版的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM