Re[3]: [PHP-GTK] Editable tables and general planning help
| From: | Tom Rogers | Date: | Fri, 08 Jul 2005 16:32:01 +0000 |
| Subject: | Re[3]: [PHP-GTK] Editable tables and general planning help | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-5938@lists.php.net to get a copy of this message | ||
Hi,
Here is how to make data available to excel as well:
<?php
dl("php_w32api.dll");
$api =& new win32;
$api->registerfunction("bool OpenClipboard
(
long hWndNewOwner
)
From user32.dll");
$api->registerfunction("bool CloseClipboard
(
)
From user32.dll");
$api->registerfunction("int EnumClipboardFormats
(
int format
)
From user32.dll");
$api->registerfunction("int GetClipboardFormatName
(
int format,
string &FormatName,
int cchMaxCount
)
From user32.dll");
$api->registerfunction("long GetClipboardData
(
int uFormat
)
From user32.dll");
$api->registerfunction("string GlobalLock
(
long hMem
)
From kernel32.dll");
$api->registerfunction("bool GlobalUnlock
(
long hMem
)
From kernel32.dll");
$api->registerfunction("bool EmptyClipboard
(
)
From user32.dll");
$api->registerfunction("int RegisterClipboardFormat
(
string &Format
)
From user32.dll");
$api->registerfunction("long SetClipboardData
(
int uFormat,
long hMem
)
From user32.dll");
$api->registerfunction("long GlobalAlloc
(
int uFlags,
long Bytes
)
From kernel32.dll");
$api->registerfunction("long lstrcpyn
(
long String1,
string &String2,
int iMaxLength
)
From kernel32.dll");
$api->registerfunction("long GlobalHandle
(
long pMem
)
From kernel32.dll");
define('GPTR',0x0040); //allocate memory and zero fill it.
$id = $api->OpenClipboard('');
$NameSize = 128;
if($id){
//check for copy csv data
$fn = $api->EnumClipboardFormats(0);
do {
$name = str_repeat("\0", $NameSize);
$api->GetClipboardFormatName($fn,$name,$NameSize);
echo "Fn $fn $name\n";
if(preg_match('/CSV/i',$name)){
$data_handle = $api->GetClipboardData($fn);
if($data_handle){
$text = trim($api->GlobalLock($data_handle));
echo $text;
$rows = explode("\r\n",$text);
foreach($rows as $key=>$row){
$rows[$key] = explode(',',$row);
}
print_r($rows);
$api->GlobalUnlock($data_handle);
}
break;
}
}while($fn = $api->EnumClipboardFormats($fn));
//now paste (we already have an id so we stick with it)
$format_string = 'Csv';
$data = "1A,1B\r\n2A,2B\r\n"; //paste data
$data_length = strlen($data);
$api->EmptyClipboard(); //grab ownership
$paste_fn = $api->RegisterClipboardFormat($format_string);
echo "paste number $paste_fn\n"; //same as excel uses above
$dest = $api->GlobalAlloc(GPTR,$data_length+1); //get a pointer to a chunk of memory
$paste_handle = $api->GlobalHandle($dest); // get a handle to the memory block
$api->GlobalLock($paste_handle); //lock it while we copy
$ndest = $api->lstrcpyn($dest,$data,$data_length); //copy our csv data to it
$api->GlobalUnlock($paste_handle); //release the lock
//we can make the data available as plain text as well
//by un commenting the line below but then need to use paste special in excel....
//$api->SetClipboardData(1,$paste_handle); //setup the clipboard with plain text
//load as csv data
$api->SetClipboardData($paste_fn,$paste_handle); //setup the clipboard
$api->CloseClipboard(); //all done
}
To test just run script then do a paste to an excel cell.
--
regards,
Tom