When copying from the clipboard to Excel, the default cell delimiter is a tab character ('\t'), while the row delimiter is the newline character ('\n'). Therefore, in Javascript, you can build a string of the cells and rows by iterating through the selected rows collection of the WebGrid, and inserting the proper delimiters as necessary. For this article, this functionality is wired to the standard key combination Ctrl-C, using a combination of the KeyDownHandler and KeyUpHandler client-side events:
In JavaScript:
var ctrlPressed = false;
function UltraWebGrid1_KeyDownHandler(gridName, cellId, key)
{
if (key == 17)
ctrlPressed = true;
else if (ctrlPressed && key == 67)
{
var rowString = "";
var isFirstRow = true;
var grid = igtbl_getGridById(gridName);
for (var rowId in grid.SelectedRows)
{
var row = igtbl_getRowById(rowId);
if (!isFirstRow)
rowString += '\n';
for (var i=0; i < cell =" row.getCell(i);" isfirstrow =" false;" key ="="" ctrlpressed =" false;">
Since there is no way to tell Excel to paste only into a certain column of a row using a delimiter, this functionality will only work correctly for selected rows.
Wednesday, December 3, 2008
Copy selected rows from a WebGrid to Excel using the clipboard
Posted by Suryan at 11:39 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment