首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

asp.net怎么将excel导入到access数据库

2012-04-04 
asp.net如何将excel导入到access数据库?我手上有个导入代码,如果单独存放能正常导入,若放到一个asp.net网

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("", "<script>alert('数据导入成功');</script>");
   
  }
   
}


------解决方案--------------------


string strpath = this.FileUpload1.PostedFile.FileName;
你这是客户端的路径的啊,服务器上是没有的,你必须

string strpath = Server.MapPath("~/" + Path.GetFileName(this.FileUpload1.PostedFile.FileName));
FileUpload1.SaveAs(strpath);


[解决办法]
我觉得有可能是2个问题
1.就像1楼说的那样
2. string str = "Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + Server.MapPath("database/0451skyEvaluation.mdb");我记得好像access的链接字符的路径不是这样写的,你断点调试一下,看看你的这里执行之后的路径是否正确~~

热点排行