ASP.NET Excel導入SQLserver 數據庫中 編輯:吳立星
上次修改時間:2009年8月2日
本次修改時間: 2009年9月7日
修改人: 吳立星
一 .aspx 頁
<%@ Page Lang ge="C#" AutoEventWireup="tr" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html P LIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona l.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Excel導入SQL數據庫</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<div>
<table style="width: 576px; border-collapse: separate; text-align: center">
<tr>
<td colspan="3">
Excel導入SQL數據庫</td>
</tr>
<tr>
<td colspan="3" style="text-align: left">
<asp:FileUpload ID="FileUpload1" runat="server" Width="305px" />
<asp:Button ID="Button1" runat="server" Text="導入SQL" /></td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server" Height="133px" Width="100%">
</asp:GridView>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td style="width: 189px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
二. .cs 後臺文件
using System.Configuration;
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;
p lic partial class _Default : System.Web.UI.Page
{
string strConn = "Data Source=.;DataBase=hhaspx;Uid=sa;Pwd=19831014"; //鏈接SQL數據庫
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from hhaspx_gz", cn);
DataSet ds = new DataSet();
sda.Fill(ds, "hhaspx_gz");
this.GridView1.DataSource = ds.Tables["hhaspx_gz"];
this.GridView1.DataKeyNames = new string[] { "hhaspx_id" };
this.GridView1.DataBind();
}
/**/
/// <summary>
/// 查詢EXCEL電子表格添加到DATASET
/// </summary>
/// <param name="filenameurl">服務器路徑</param>
/// <param name="table">表名</param>
///
/// <returns>更多內容請訪問:www.hhaspx.com</returns>
/// 紅河ASP.NET(C#)學習網包含ASP.NET 教程,c#教程,Photoshop,ps教程,網頁製作,圖片素材,SEO優化
p lic DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
odda.Fill(ds, table);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == false)//HasFile用來檢查FileUpload是否有指定文件
{
Response.Write("<script>alert('請您選擇Excel文件')</script> ");
return;//當無文件時,返回
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName). ToString().ToLower();//System.IO.Path.GetExtension獲得文件的擴展名
if (IsXls != ".xls")
{
Response.Write("<script>alert('只可以選擇Excel文件')</script>");
return;//當選擇的不是Excel文件時,返回
}
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName; //獲取Execle文件名 DateTime日期函數
string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 獲得虛擬服務器相對路徑
FileUpload1.SaveAs(savePath); //SaveAs 將上傳的文件內容保存在服務器上
DataSet ds = ExecleDs(savePath, filename); //調用自定義方法
DataRow[] dr = ds.Tables[0].Select(); //定義一個DataRow數組
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel表爲空表,無數據!')</script>"); //當Excel表爲空時,對用戶進行提示
}
else
{
for (int i = 0; i < dr.Length; i++)
{
string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名稱不能變,否則就會出錯】
本次修改時間: 2009年9月7日
修改人: 吳立星
一 .aspx 頁
<%@ Page Lang ge="C#" AutoEventWireup="tr" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html P LIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona l.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Excel導入SQL數據庫</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<div>
<table style="width: 576px; border-collapse: separate; text-align: center">
<tr>
<td colspan="3">
Excel導入SQL數據庫</td>
</tr>
<tr>
<td colspan="3" style="text-align: left">
<asp:FileUpload ID="FileUpload1" runat="server" Width="305px" />
<asp:Button ID="Button1" runat="server" Text="導入SQL" /></td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server" Height="133px" Width="100%">
</asp:GridView>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td style="width: 189px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
二. .cs 後臺文件
using System.Configuration;
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;
p lic partial class _Default : System.Web.UI.Page
{
string strConn = "Data Source=.;DataBase=hhaspx;Uid=sa;Pwd=19831014"; //鏈接SQL數據庫
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from hhaspx_gz", cn);
DataSet ds = new DataSet();
sda.Fill(ds, "hhaspx_gz");
this.GridView1.DataSource = ds.Tables["hhaspx_gz"];
this.GridView1.DataKeyNames = new string[] { "hhaspx_id" };
this.GridView1.DataBind();
}
/**/
/// <summary>
/// 查詢EXCEL電子表格添加到DATASET
/// </summary>
/// <param name="filenameurl">服務器路徑</param>
/// <param name="table">表名</param>
///
/// <returns>更多內容請訪問:www.hhaspx.com</returns>
/// 紅河ASP.NET(C#)學習網包含ASP.NET 教程,c#教程,Photoshop,ps教程,網頁製作,圖片素材,SEO優化
p lic DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
odda.Fill(ds, table);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == false)//HasFile用來檢查FileUpload是否有指定文件
{
Response.Write("<script>alert('請您選擇Excel文件')</script> ");
return;//當無文件時,返回
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName). ToString().ToLower();//System.IO.Path.GetExtension獲得文件的擴展名
if (IsXls != ".xls")
{
Response.Write("<script>alert('只可以選擇Excel文件')</script>");
return;//當選擇的不是Excel文件時,返回
}
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName; //獲取Execle文件名 DateTime日期函數
string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 獲得虛擬服務器相對路徑
FileUpload1.SaveAs(savePath); //SaveAs 將上傳的文件內容保存在服務器上
DataSet ds = ExecleDs(savePath, filename); //調用自定義方法
DataRow[] dr = ds.Tables[0].Select(); //定義一個DataRow數組
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel表爲空表,無數據!')</script>"); //當Excel表爲空時,對用戶進行提示
}
else
{
for (int i = 0; i < dr.Length; i++)
{
string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名稱不能變,否則就會出錯】
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.