- 论坛徽章:
- 12
|
本帖最后由 523066680 于 2018-02-03 16:33 编辑
回复 33# 情节可以很简单
多谢打赏。CU 这方面还可以。就是人气越来越少了。
要带坐标的话,把字符间隔改为 tab 然后整个粘贴到 excel,这样不用考虑坐标字符宽度。
Tab:
- use List::Util qw/max min/;
- STDOUT->autoflush(1);
- my @data = read_file("bgr_trim8.txt");
- our ( @X, @Y, @Z, %edge );
- for my $row ( @data )
- {
- $row=~/\((-?\d+),(-?\d+)\)\s+(\w+)/;
- push @X, $1;
- push @Y, $2;
- push @Z, $3;
- }
- %edge = (
- 'x' => { 'min' => min( @X ), 'max' => max( @X ) },
- 'y' => { 'min' => min( @Y ), 'max' => max( @Y ) },
- );
- our @buffer;
- create_buffer();
- draw_buffer();
- #<STDIN>;
- sub draw_buffer
- {
- our ( @X, @Y, @Z, %edge );
- my ($x, $y, $str);
- # 数据填入
- for my $id ( 0 .. $#X )
- {
- $x = $X[$id] - $edge{x}{min};
- $y = $Y[$id] - $edge{y}{min};
- $buffer[ $y ][ $x+1 ] = $Z[$id];
- }
- $str = join "\n", map { join("\t", @$_ ) } ( @buffer );
- write_file("tab.txt", $str );
- }
- sub create_buffer
- {
- my $dtx = $edge{x}{max} - $edge{x}{min} + 1;
- my $dty = $edge{y}{max} - $edge{y}{min} + 1;
- my $realy;
- for my $row ( 1 .. $dty )
- {
- $realy = $edge{y}{min} + $row - 1;
- push @buffer, [ $realy, ("") x $dtx, $realy ];
- }
- #添加 x 坐标轴
- push @buffer, [ "", map { $_ } ( int($edge{x}{min}) .. int($edge{x}{max}) ) ];
- unshift @buffer, $buffer[$#buffer];
- }
- sub read_file
- {
- open my $fh, "<", $_[0];
- return (<$fh>);
- }
- sub write_file
- {
- open my $fh, ">", $_[0];
- print $fh $_[1];
- close $fh;
- }
复制代码
Excel 版:
- use Win32::OLE qw (in with);
- use Win32::OLE::Const ('Microsoft Excel');
- use Win32::OLE::Variant;
- use List::Util qw/max min/;
- STDOUT->autoflush(1);
- use Cwd;
- my $cwd = getcwd();
- $cwd=~s/\//\\/g;
- our $ex;
- our $book;
- our $sheet;
- create_sheet();
- my @data = read_file("bgr_trim8.txt");
- our ( @X, @Y, @Z, %edge );
- for my $row ( @data )
- {
- $row=~/\((-?\d+),(-?\d+)\)\s+(\w+)/;
- push @X, $1;
- push @Y, $2;
- push @Z, $3;
- }
- %edge = (
- 'x' => { 'min' => min( @X ), 'max' => max( @X ) },
- 'y' => { 'min' => min( @Y ), 'max' => max( @Y ) },
- );
- our @buffer;
- create_buffer();
- draw_buffer();
- save_sheet( $cwd ."\\data.xlsx" );
- sub draw_buffer
- {
- our ( @X, @Y, @Z, %edge );
- my ($x, $y, $str);
- # 数据填入
- for my $id ( 0 .. $#X )
- {
- $x = $X[$id] - $edge{x}{min};
- $y = $Y[$id] - $edge{y}{min};
- $buffer[ $y ][ $x+1 ] = $Z[$id];
- $sheet->cells( $y+2, $x+2 )->{Value} = $Z[$id];
- }
- }
- sub create_buffer
- {
- my $dtx = $edge{x}{max} - $edge{x}{min} + 1;
- my $dty = $edge{y}{max} - $edge{y}{min} + 1;
- my $realy;
- for my $row ( 1 .. $dty )
- {
- $realy = $edge{y}{min} + $row - 1;
- $sheet->cells( $row+1, 1 )->{Value} = $realy;
- $sheet->cells( $row+1, $dtx+2 )->{Value} = $realy;
- }
- #添加 x 坐标轴
- for my $col ( 1 .. $dtx )
- {
- $realx = $edge{x}{min} + $col + 1;
- $sheet->cells( 1, $col+1 )->{Value} = $realx;
- $sheet->cells( $dty + 2, $col+1 )->{Value} = $realx;
- $sheet->columns( $col )->{ColumnWidth} = 2;
- }
- $sheet->columns( $dtx+1 )->{ColumnWidth} = 2;
- $sheet->columns( $dtx+2 )->{ColumnWidth} = 2;
- }
- sub create_sheet
- {
- our $ex;
- our $book;
- our $sheet;
- # use existing instance if Excel is already running
- eval { $ex = Win32::OLE->GetActiveObject('Excel.Application') };
- die "Excel not installed" if $@;
- unless ( defined $ex ) {
- $ex = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit; })
- or die "Oops, cannot start Excel";
- }
- #new workbook
- $book = $ex->Workbooks->Add;
-
- # write to a particular cell
- $sheet = $book->Worksheets(1);
- }
- sub save_sheet
- {
- my $file = shift;
- our $ex;
- our $book;
- our $sheet;
- # save and exit
- $ex->{DisplayAlerts} = 'False';
- $book->SaveAs( $file );
- undef $sheet;
- undef $book;
- undef $ex;
- }
- sub read_file
- {
- open my $fh, "<", $_[0];
- return (<$fh>);
- }
- sub write_file
- {
- open my $fh, ">", $_[0];
- print $fh $_[1];
- close $fh;
- }
复制代码
|
|