Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I want to drag an excel spreadsheet, parse the content, and move the data to a datagrid on my web page.

 

This needs to be done with cut and paste and, seeing as adding a mouse-event to the datagrid adds it to every member of the datagrid, making it practically unuseable, my idea was:

 

Put a button on the page that says, "Move data from clipboard"

 

Use that to kick off a clipboard reader.

 

It appears that the method for this lies in System.Runtime.InteropServices.ComTypes.IDataObject. Darned if I can find an example that works

 

Anyone been down this road before?

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Posted

Ok, I worked this out, so I am going to share it.

 

The problem here, of course, is that everything you do in ASP.net is Server Side, and what you need to do here is a client side append

 

So you have to use javascript to capture the data client side, and then force a postback to move the data server-side, where you capture it AGAIN, and put it in your datagrid

 

So I created a button on the form, which was a simple HTML input button:

 


<input type="button", id="btnCB" value="Import From Clipboard" onclick="fnPaste()" />

 

Next I created the function mentioned in the onclick in the Script section of the header

 

function fnPaste() {
  //this captures the click event for btnCB and creates a postback
  //the is picked up in the postback of the page load, and then moved to 
  //the processarg sub
  var s = window.clipboardData.getData("Text");
 
   __doPostBack('__Page', s);
  
  //txt.focus;
}
function __doPostBack(eventTarget, eventArgument) {
  if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
      theForm.__EVENTTARGET.value = eventTarget;
      theForm.__EVENTARGUMENT.value = eventArgument;
      theForm.submit();
  }
}

 

And in the postback of the page, we capture the data and send it off to be processed:

 


           Dim eventArg As String = Request("__EVENTARGUMENT")
           If Not eventArg Is Nothing Then
               If eventArg.Length > 0 Then
                   ProcessArg(eventArg)
               End If
           End If

 

In an excel spreadsheet, the lines come in broken up by chr(10) + chr(13), which is the equivalent of VBCRLF. The cells are broken up by the tab, which is chr(9).

 

This lets you easily grab the data per line, and diseminate it per cell

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...