將Excel中數(shù)據(jù)導(dǎo)入到Access數(shù)據(jù)庫(kù)中的方法

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
<style type="text/css">
.style1
{
height: 16px;
}
.style3
{
height: 23px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table align="center" border="1" bordercolor="honeydew" cellpadding="0"
cellspacing="0">
<tr>
<td style="FONT-SIZE: 9pt; COLOR: #ff0000; BACKGROUND-COLOR: #ff9933; TEXT-ALIGN: center"
class="style1">
</td>
<td colspan="2"
style="FONT-SIZE: 9pt; COLOR: #ffffff; HEIGHT: 16px; BACKGROUND-COLOR: #ff9933; TEXT-ALIGN: center">
將Excel數(shù)據(jù)寫(xiě)入Access數(shù)據(jù)庫(kù)中</td>
</tr>
<tr>
<td style="BACKGROUND-COLOR: #ffffcc; TEXT-ALIGN: center">
</td>
<td style="BACKGROUND-COLOR: #ffffcc; TEXT-ALIGN: center">
<iframe id="I1" name="I1" scrolling="yes" src="學(xué)生成績(jī).xls"
style="WIDTH: 407px; HEIGHT: 280px"></iframe>
</td>
<td style="WIDTH: 190px; BACKGROUND-COLOR: #ffffcc; TEXT-ALIGN: center">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="9pt"
ForeColor="#333333" GridLines="None" Width="228px">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="BACKGROUND-COLOR: #ff9900; TEXT-ALIGN: center" class="style3">
</td>
<td style="HEIGHT: 23px; BACKGROUND-COLOR: #ff9900; TEXT-ALIGN: center"
valign="top">
<asp:Button ID="Button3" runat="server" Font-Size="9pt" onclick="Button1_Click"
Text="Excel數(shù)據(jù)寫(xiě)入Access數(shù)據(jù)庫(kù)中" />
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"
style="font-size: x-small"></asp:Label>
</td>
<td style="WIDTH: 190px; HEIGHT: 23px; BACKGROUND-COLOR: #ff9900; TEXT-ALIGN: center">
<asp:Button ID="Button2" runat="server" Font-Size="9pt" onclick="Button2_Click"
Text="數(shù)據(jù)庫(kù)中顯示Excel數(shù)據(jù)" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public OleDbConnection CreateCon()
{
string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("UserScore.mdb") + ";User Id=admin;Password=;";
OleDbConnection odbc = new OleDbConnection(strconn);
return odbc;
}
protected void Button1_Click(object sender, EventArgs e)
{
//定義Excel列表
string StyleSheet = "Sheet1";
//調(diào)用自定義LoadData方法,將Excel文件中數(shù)據(jù)讀到ASPNET頁(yè)面中
LoadData(StyleSheet);
//定義查詢的SQL語(yǔ)句
string sql = "select ID,用戶姓名,試卷,成績(jī),考試時(shí)間 from Score";
//創(chuàng)建Oledb數(shù)據(jù)庫(kù)連接
OleDbConnection con = CreateCon();
con.Open();//打開(kāi)數(shù)據(jù)庫(kù)連接
OleDbCommand com = new OleDbCommand(sql, con);
//開(kāi)始事務(wù)
OleDbTransaction tran = con.BeginTransaction();
com.Transaction = tran;
//創(chuàng)建適配器
OleDbDataAdapter da = new OleDbDataAdapter(com);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
//創(chuàng)建DataSet數(shù)據(jù)集
DataSet ds = new DataSet();
//填充數(shù)據(jù)集
da.Fill(ds);
int curIndex = 0;
if (ds.Tables[0].Rows.Count > 0)
{
curIndex = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
//創(chuàng)建一個(gè)內(nèi)存表
DataTable tb = this.getExcelDate();
string selsql = "";
for (int i = 0; i < tb.Rows.Count; i++)
{
string UserName = tb.Rows[i][0].ToString();
selsql = "select count(*) from Score where 用戶姓名='" + UserName + "'";
}
//判斷Excel文件中是否已經(jīng)導(dǎo)入到Access數(shù)據(jù)庫(kù)中
if (ExScalar(selsql) > 0)
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert('該Excle中的數(shù)據(jù)已經(jīng)導(dǎo)入數(shù)據(jù)庫(kù)中!');location='Default.aspx';</script>";
}
else
{
//循環(huán)讀取Excel文件中數(shù)據(jù),并添加到Access事先創(chuàng)建好的數(shù)據(jù)庫(kù)表中
for (int i = 0; i < tb.Rows.Count; i++)
{
DataRow dr = ds.Tables[0].NewRow();
dr[0] = ++curIndex;
dr[1] = tb.Rows[i][0];
dr[2] = tb.Rows[i][1];
dr[3] = tb.Rows[i][2];
dr[4] = tb.Rows[i][3];
ds.Tables[0].Rows.Add(dr);
}
try
{
da.Update(ds);//執(zhí)行插入操作
tran.Commit();//事務(wù)提交
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert('數(shù)據(jù)導(dǎo)入成功!');location='Default.aspx';</script>";
}
catch
{
tran.Rollback();//事務(wù)回滾
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert('數(shù)據(jù)導(dǎo)入失??!');location='Default.aspx';</script>";
}
finally
{
con.Close();//關(guān)閉數(shù)據(jù)庫(kù)連接
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string sqlstr = "select * from Score";
OleDbConnection conn = CreateCon();
conn.Open();
OleDbCommand mycom = new OleDbCommand(sqlstr, conn);
OleDbDataReader dr = mycom.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
GetDataSet(sqlstr);
}
else
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert('數(shù)據(jù)庫(kù)中沒(méi)有數(shù)據(jù)信息,請(qǐng)先導(dǎo)入再查詢!');location='Default.aspx';</script>";
}
dr.Close();
conn.Close();
}
public DataSet GetDataSet(string sqlstr)
{
OleDbConnection conn = CreateCon();
OleDbDataAdapter myda = new OleDbDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
myda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
return ds;
}
public DataTable getExcelDate()
{
string strExcelFileName = Server.MapPath("學(xué)生成績(jī).xls");
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
string sql = "select * from [Sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter(sql, strcon);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
public void LoadData(string StyleSheet)
{
//定義數(shù)據(jù)庫(kù)連接字符串 m
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("學(xué)生成績(jī).xls") + ";Extended Properties=Excel 8.0";
//創(chuàng)建數(shù)據(jù)庫(kù)連接
OleDbConnection myConn = new OleDbConnection(strCon);
//打開(kāi)數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集
myConn.Open();
//創(chuàng)建DataSet對(duì)象
DataSet myDataSet = new DataSet();
//定義查詢的SQL語(yǔ)句
string StrSql = "select * from [" + StyleSheet + "$]";
//創(chuàng)建數(shù)據(jù)庫(kù)適配器
OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
//填充數(shù)據(jù)集中的數(shù)據(jù)
myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
//釋放占有的資源
myCommand.Dispose();
//關(guān)閉數(shù)據(jù)庫(kù)連接
myConn.Close();
}
public int ExScalar(string sql)
{
OleDbConnection conn = CreateCon();
conn.Open();
OleDbCommand com = new OleDbCommand(sql, conn);
return Convert.ToInt32(com.ExecuteScalar());
conn.Close();
}
}
- access dbase excel foxpro 數(shù)據(jù)庫(kù)瀏覽器V3.1版 下載
- Excel數(shù)據(jù)導(dǎo)入Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- Excel導(dǎo)入Sqlserver數(shù)據(jù)庫(kù)腳本
- Linux下將excel數(shù)據(jù)導(dǎo)入到mssql數(shù)據(jù)庫(kù)中的方法
- php將數(shù)據(jù)庫(kù)導(dǎo)出成excel的方法
- php中使用ExcelFileParser處理excel獲得數(shù)據(jù)(可作批量導(dǎo)入到數(shù)據(jù)庫(kù)使用)
- php excel reader讀取excel內(nèi)容存入數(shù)據(jù)庫(kù)實(shí)現(xiàn)代碼
- ASP.NET下將Excel表格中的數(shù)據(jù)規(guī)則的導(dǎo)入數(shù)據(jù)庫(kù)思路分析及實(shí)現(xiàn)
- Excel導(dǎo)入數(shù)據(jù)庫(kù)時(shí)出現(xiàn)的文本截?cái)鄦?wèn)題解決方案
- 如何把Excel數(shù)據(jù)導(dǎo)入到SQL2008數(shù)據(jù)庫(kù)的實(shí)例方法
- Java 使用poi把數(shù)據(jù)庫(kù)中數(shù)據(jù)導(dǎo)入Excel的解決方法
- PHP 如何利用phpexcel導(dǎo)入數(shù)據(jù)庫(kù)
- 把excel表格里的數(shù)據(jù)導(dǎo)入sql數(shù)據(jù)庫(kù)的兩種方法
- Excel、記事本數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
- C#連接Excel2003和Excel2007以上版本做數(shù)據(jù)庫(kù)的連接字符串
- c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- 利用phpexcel把excel導(dǎo)入數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)導(dǎo)出excel實(shí)現(xiàn)
- c#生成excel示例sql數(shù)據(jù)庫(kù)導(dǎo)出excel
- android通過(guò)jxl讀excel存入sqlite3數(shù)據(jù)庫(kù)
- Drupal讀取Excel并導(dǎo)入數(shù)據(jù)庫(kù)實(shí)例
- 使用工具 plsqldev將Excel導(dǎo)入Oracle數(shù)據(jù)庫(kù)
- asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的方法
- php導(dǎo)入excel文件到mysql數(shù)據(jù)庫(kù)的方法
- C#窗體讀取EXCEL并存入SQL數(shù)據(jù)庫(kù)的方法
- C#定制Excel界面并實(shí)現(xiàn)與數(shù)據(jù)庫(kù)交互的方法
相關(guān)文章
ASP.NET C#生成下拉列表樹(shù)實(shí)現(xiàn)代碼
下拉列表樹(shù)很方便且時(shí)尚的一個(gè)導(dǎo)航,貌似很多的朋友都想實(shí)現(xiàn)這樣一個(gè)列表樹(shù),本文將滿足你們的設(shè)想,通過(guò)本文你們可以學(xué)到如何使用c#生成下拉列表樹(shù),感興趣的你可不要錯(cuò)過(guò)了啊2013-02-02
ASP.NET設(shè)置404頁(yè)面返回302HTTP狀態(tài)碼的解決方法
訪問(wèn)網(wǎng)站時(shí)錯(cuò)誤頁(yè)面可正常顯示,但HTTP狀態(tài)碼卻是302,對(duì)SEO很不友好,按下列步驟修改使錯(cuò)誤頁(yè)面返回正確的利于SEO的404狀態(tài)碼,感興趣的朋友可以了解下2013-09-09
如何在一個(gè).NET?Core項(xiàng)目中使用RabbitMQ進(jìn)行即時(shí)消息管理
本文提供了在.NETCore項(xiàng)目中使用RabbitMQ進(jìn)行即時(shí)消息管理的詳細(xì)操作指南,包括安裝Erlang和RabbitMQ,配置RabbitMQ,以及在.NETCore項(xiàng)目中編寫(xiě)代碼和調(diào)試,操作步驟詳盡,感興趣的朋友一起看看吧2024-09-09
ASP.NET?Core中的Caching組件簡(jiǎn)介
這篇文章介紹了ASP.NET?Core中的Caching組件,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
.net如何優(yōu)雅的使用EFCore實(shí)例詳解
這篇文章主要為大家介紹了.net如何優(yōu)雅的使用EFCore實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
ASP.NET?Core?實(shí)現(xiàn)自動(dòng)刷新JWT?Token
這篇文章主要介紹了ASP.NET?Core?實(shí)現(xiàn)自動(dòng)刷新JWT?Token,通過(guò)增加??refresh_token??,客戶端使用refresh_token去主動(dòng)刷新JWT?Token,下文具體操作過(guò)程需要的小伙伴可以參考一下2022-04-04
ASP.NET Core啟動(dòng)地址配置方法及優(yōu)先級(jí)順序
這篇文章主要介紹了ASP.NET Core啟動(dòng)地址配置方法及優(yōu)先級(jí)順序,如果在同一臺(tái)機(jī)器上運(yùn)行多個(gè)ASP.NET Core實(shí)例,使用默認(rèn)值肯定不合適,下面我們一起進(jìn)入文章了解具體內(nèi)容吧2022-03-03
asp.net 枚舉文件里面的數(shù)字綁定到DropDownList里面去
將枚舉文件里面的數(shù)字綁定到DropDownList里面去的實(shí)現(xiàn)方法,大家可以借鑒下。2009-07-07
.Net Core 下使用ZKWeb.System.Drawing實(shí)現(xiàn)驗(yàn)證碼功能(圖形驗(yàn)證碼)
本文介紹.Net Core下用第三方ZKWeb.System.Drawing實(shí)現(xiàn)驗(yàn)證碼功能。非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-11-11

