原文发布时间为:2008-10-27 —— 来源于本人的百度文章 [由搬家工具导入]

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导入Excle预览" />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        </div>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="确定将以上数据导入数据库"
            Visible="False" />
    </form>
</body>
</html>

后台:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string type1 = FileUpload1.PostedFile.ContentType;
        string type = type1.Substring(type1.LastIndexOf("-") + 1, 5);

        if (string.Equals(type, "excel"))
        {
            string newName = Server.MapPath("App_Data//Uploads//") + DateTime.Now.ToString("hhmmss") + ".xls";
            FileUpload1.SaveAs(newName);
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newName + ";Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(connStr);
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
            DataSet ds = new DataSet();
            oda.Fill(ds);
            conn.Close();
            GridView1.DataSource = ds;
            GridView1.DataBind();
            File.Delete(newName);
            Session["paper"] = ds;
            Button2.Visible = true;
        }

        else
        {
            Page.RegisterStartupScript("", "<script>alert('文件格式不正确')</script>");
        }
       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DataSet ds=(DataSet)Session["paper"];
        SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;DataBase=master;Integrated Security=True");
        if(conn.State.ToString()=="Closed")
        {
            conn.Open();
        }
        SqlCommand cmd = new SqlCommand();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string user = ds.Tables[0].Rows[i][0].ToString();
            string paper = ds.Tables[0].Rows[i][1].ToString();
            string score = ds.Tables[0].Rows[i][2].ToString();
            string sql = "insert into [dbo].[paper]([user],[paper],[score]) values('" + user + "','" + paper + "','" + score + "')";
            cmd.Connection = conn;
            cmd.CommandText = sql;
            try
            {
               cmd.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
                Response.Write("插入失败!由于:"+ex.Message);
            }
        }
        conn.Close();
        Response.Write("<SCRIPT>alert('数据已成功导入到数据库!');</SCRIPT>");
    }
}

先将Excel导入到gridview再添加进数据库【Excel批量添加】的更多相关文章

  1. Web安全--使用Salt + Hash将密码加密后再存储进数据库

    转载原地址 http://www.bozhiyue.com/mianshiti/_net/2016/0728/314239.html (一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站 ...

  2. Java使用Mysql数据库实现批量添加数据

    EmployeeDao.java //批处理添加数据 public int saveEmploeeBatch(){ int row = 0; try{ con = DBCon.getConn(); S ...

  3. dedecms--后台添加会员栏目(批量添加)

    最近在用dedecms二次开发会员功能,一开始做了一个会员添加,但是领导要求可以批量添加,最好是可以输入添加个数:这样我想添加几个就添加几个了 1:会员添加的htm页面 <html> &l ...

  4. ASP.NET Aries 高级开发教程:Excel导入之单表配置(上)

    前言: 随着ASP.NET Aries的普及,刚好也有点闲空,赶紧把Excel导入功能的教程补上. Excel导入功能,分为四篇:单表配置(上).多表高级配置(中).配置规则(下).代码编写(番外篇) ...

  5. excel导入工具

    1 介绍 excel导入工具 整个项目的代码结构如下 \---excelExport # 导出工具包 | AsyncExportExcel.java #多线程导出 | ExcelImport.java ...

  6. Excel导入工具类

    项目需要从Excel导入数据,然后插入到数据库对应表中.设计了一个导入工具类,导入数据和导入结果如下图示: poi jar版本采用的3.15 导入工具类实现如下: package com.alphaj ...

  7. php Excel 导入

    php Excel 导入 public function storeSql() { $file = input('file.excel'); $path = ROOT_PATH . 'public' ...

  8. spring MVC 后端 接收 前端 批量添加的数据(简单示例)

    第一种方式:(使用ajax的方式) 前端代码: <%@ page contentType="text/html;charset=UTF-8" language="j ...

  9. Crowd 批量添加用户(Postman 数据驱动)

    背景 最近公司大量新员工入职,需要批量创建 Crowd 用户.设置密码.分配应用组等机械性重复工作(主要还是懒~),故把这个加餐任务分配给刚来的测试同学去研究. 一是:让他了解下 Postman 的数 ...

随机推荐

  1. 2017 ACM-ICPC网络赛 H.Skiing 有向图最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  2. dfs 的全排列

    #include <iostream> #include <algorithm> #include <cstdio> #include <string> ...

  3. Linux命令之---cat

    命令简介 cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 命令格式 cat [选项] [文 ...

  4. HDU 2177 取(2堆)石子游戏

    取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. 巧用Windows Server 2008的NPS策略

    单位员工大部分是移动办公一族,由于病毒库更新不及时.系统补丁没有安装,使移动办公设备处于危险状态,访问内部网络时很可能威胁整个网络.该如何防守网络访问这扇门呢? 笔者所在的单位是一家传媒公司,有数百人 ...

  6. 【SCOI 2010】股票交易

    题目 最近 \(\text{lxhgww}\) 又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,\(\text{lxhgww}\) 预测到了未来 \(T ...

  7. MongoDB01——安装MangoDB

    一.MongoDB的下载 到MongoDB的官网——https://www.mongodb.com/download-center/community,选择要下载的版本,点击Download 二.安装 ...

  8. DWR搭建以及使用教程

    DWR搭建以及使用教程   DWR(Direct Web Remoting)是一个Ajax的开源框架,用于改善web页面与Java类交互的远程服务器端的交互体验,可以帮助开发人员开发包含AJAX技术的 ...

  9. Hibernate命名策略及配置

    hibernate 表 命名策略         分类:            hibernate2013-02-27 18:46464人阅读评论(0)收藏举报 Hibernate注释下的自定义架构实 ...

  10. nyoj 题目12 喷水装置(二)

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...