开发者

Select, Copy from IE and paste to Excel

So the basic question is, how can I make table nicely copy pastable from IE to Excel or OpenOffice calc. The page is located here: http://tuudik.lohv.eu/Asjad/EURXML/

The table is created dynamically by PHP code. There's a button with tekst "Vali tabel!" which means, it selects the table. Then I want to copy it, for example using CTRL+C and paste it into a spreadsheet.

What are the best options? Should I create a hidden div what then has t开发者_开发问答he same Data like in the table but formatted other way?

Also, anyone knows how to make the "Vali kõik!" button work so, that it would automatically copy the contents to the clipboard?


Why not modify your PHP script to write the page content directly to Excel, writing the content to excel cells rather than HTML where you're writing to the <td> elements, then set the headers to download it... so passing an additional 'format' parameter to your script could determine the output format as HTML of XLS. The PHPExcel library offers the capability of generating XLS files in pure PHP. A link on your HTML page could then offer the option to recreate the output directly as an XLS file for download, rather than copy-and-paste.


In my experiences copying from html pages is best done through a text editor that doesn't have formatting. This way the content is usually delimited in some kind (tab usually) that makes pasting to a spreadsheet much easier.

  • Copy table from browser
  • Paste in notepad, select, copy again
  • Paste in Spreadsheet application

Might not answer your question directly but if you somehow can make the data copyable as plain text with tab delimiter (as in a hidden div or something) - you would probably have greater success in pasting it into spreadsheets that supports all kinds of formatting.


Using a slightly different approach, try the Get External Data feature in excel

( i tested on Using Excel 2010, but 2007 is similar, and it is available in 2003 in a slightly different guise )

From menu Data/Get External Data/From Web

opens a form:

  • paste address;(eg http://bit.ly/Kurss) and click Go
  • web page is displayed
  • scroll down to the table you want
  • select it with the little yellow arrow thingy
  • Click Import

Voila, data is in excel, as unformatted data from a background query!

Then whenever you waht to update the data, just click Refresh


I to needed a way to copy html table information and paste into excel. I used a combination of techniques. First I added this code so I could automatically copy data into the paste buffer of the clients pc.

How do I copy to the clipboard in JavaScript?

// Copies a string to the clipboard. Must be called from within an 
// event handler such as click. May return false if it failed, but
// this is not always possible. Browser support for Chrome 43+, 
// Firefox 42+, Safari 10+, Edge and IE 10+.
// IE: The clipboard feature may be disabled by an administrator. By
// default a prompt is shown the first time the clipboard is 
// used (per session).
function copyToClipboard(text) {
    if (window.clipboardData && window.clipboardData.setData) {
        // IE specific code path to prevent textarea being shown while dialog is visible.
        return clipboardData.setData("Text", text); 

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
        document.body.appendChild(textarea);
        textarea.select();
        try {
            return document.execCommand("copy");  // Security exception may be thrown by some browsers.
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {
            document.body.removeChild(textarea);
        }
    }
}

I then created a copy string. Separate the cell values by a tab \t and separate the rows by a new line \n. see the example below.

I create the copy data string using PHP.

//-- Create data for a single table row
//-- We needed to escape single quotes for HTML and javascript
$copy_data = str_replace("'","\'",$col1."\t".$col2."\t".$col3);

//-- Create data for a multi rows
//-- Separate multiple rows by \n but needs to be escaped so use \\n
$copy_data = str_replace("'","\'",$col1."\t".$col2."\t".$col3."\\n".$col1."\t".$col2."\t".$col3."\\n".$col1."\t".$col2."\t".$col3);

Then I place it in a copy button on the web page.

<a href="#" onclick="copyToClipboard('<?=$copy_data?>')">Copy</a>

Then you click on the "Copy" link to get the data into your copy buffer. Next go to Excel and click in the cell where you want to paste the data, then do a paste.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜