日期:2014-05-18  浏览次数:20431 次

asp.net如何将excel导入到access数据库?
我手上有个导入代码,如果单独存放能正常导入,若放到一个asp.net网站里面就提示“未将对象引用设置到对象的实例”,烦求大侠解?
附代码:
前台(DateIn.aspx):<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="DataIn.aspx.cs" Inherits="DataIn" %>

<!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;&nbsp;
  模板下载 :<a href="/admin/sheet1.xls">请点击这里 </a>(请严格按照模板进行数据填充)</div>
   
  <div style="z-index: 101; left: 16px; width: 629px; position: absolute; top: 75px;
  height: 42px; right: 516px;">
  <table>
  <tr>
  <td style="width: 101px">
  <asp:Label ID="Label1" runat="server" Text="请选择需要导入的excel文件" Width="202px"></asp:Label></td>
  <td style="width: 100px">
  <asp:FileUpload ID="FileUpload1" runat="server" BackColor="White" Height="23px" 
  Width="210px" /></td>
  <td style="width: 254px">
  <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="执行导入" 
  Height="24px" style="margin-left: 39px" Width="80px" /></td>
  </tr>
  </table>
  </div>
  </form>
</body>
</html>

后台(DateIn.aspx.cs):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;

public partial class DataIn : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
   
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
  string FileName = string.Empty;
  string strpath = this.FileUpload1.PostedFile.FileName;

  string str = "Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + Server.MapPath("database/0451skyEvaluation.mdb");
  OleDbConnection conn = new OleDbConnection(str);
  conn.Open();
  OleDbCommand cmd = new OleDbCommand("insert into SkyEvaluationSum (userid,username,dateid,datename,departmentid,departmentname,officesid,officesname,roleid,rolename,evalue,sumscore,sumcount,CreateDate,IP,UpdateDate,UTime) SELECT userid,username,dateid,datename,departmentid,departmentname,officesid,officesname,roleid,rolename,evalue,sumscore,sumcount,CreateDate,IP,UpdateDate,UTime FROM [Excel 5.0;HDR=YES;DATABASE=" + strpath + "].[sheet1$]", conn);
  cmd.ExecuteNonQuery();
  Page.RegisterStartupScript("