ASP.NETはEXCELでコピーした内容をGridviewに貼り付けます(インポートではなく貼り付けです)

4045 ワード




    
        <script type="text/javascript">
            function insertTable(col, row) {
                if (event.ctrlKey && event.keyCode == 86) {
                    insertTableOnpaste(col, row);
                }
            }

            function insertTableOnpaste(col, row) {
                var table4 = document.getElementById("GridView1");
                var txtObj = window.clipboardData.getData("Text");
                if (txtObj.indexOf("\r<br>") != -1) {
                    event.keyCode = 0;

                    var sheetrows = txtObj.split("\r<br>");
                    for (var i = 0; i < sheetrows.length - 1; i++) {
                        var sheetcols = sheetrows[i].split("\t");
                        for (var j = 0; j < sheetcols.length; j++) {
                            if (row + i <= table4.rows.length - 1 && col + j <= table4.rows[i].cells.length - 1) {
                                var value1 = table4.rows[row + i].cells[col + j];
                                var aInput = value1.getElementsByTagName("input");
                                aInput[0].value = sheetcols[j];
                            }
                        }
                    }
                }
            }
        </script>


    <form id="form1" runat="server">
        <gridview id="GridView1" runat="server" autogeneratecolumns="false">
        <columns>
           <templatefield headertext="Test1">
            <itemtemplate>
            <textbox id="TextBox1" runat="server" onkeydown="insertTable(this.parentNode.cellIndex,this.parentNode.parentNode.rowIndex)"/>
            </itemtemplate>
           </templatefield>
           <templatefield headertext="Test2">
            <itemtemplate>
            <textbox id="TextBox1" runat="server" onkeydown="insertTable(this.parentNode.cellIndex,this.parentNode.parentNode.rowIndex)"/>
            </itemtemplate>
           </templatefield>
           <templatefield headertext="Test3">
            <itemtemplate>
            <textbox id="TextBox1" runat="server" onkeydown="insertTable(this.parentNode.cellIndex,this.parentNode.parentNode.rowIndex)"/>
            </itemtemplate>
           </templatefield>
           <templatefield headertext="Test4">
            <itemtemplate>
            <textbox id="TextBox1" runat="server" onkeydown="insertTable(this.parentNode.cellIndex,this.parentNode.parentNode.rowIndex)"/>
            </itemtemplate>
           </templatefield>
           <templatefield headertext="Test5">
            <itemtemplate>
            <textbox id="TextBox1" runat="server" onkeydown="insertTable(this.parentNode.cellIndex,this.parentNode.parentNode.rowIndex)"/>
            </itemtemplate>
           </templatefield>
        </columns>
        </gridview>
    </form>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IList<int> list = new List<int>();
        for (int i = 0; i < 20; i++)
        {
            list.Add(i);
        }

        GridView1.DataSource = list;
        GridView1.DataBind();
    }
}</int></int></code></pre> 
  <p><br/>  </p> 
 </div> 
</div>
                            </div>
                        </div>