濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法

asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法

熱門標(biāo)簽:400免費(fèi)電話怎么辦理 威海電銷 400電話辦理最優(yōu)質(zhì) 寧夏機(jī)器人電銷 t3出行地圖標(biāo)注怎么做 河北網(wǎng)絡(luò)回?fù)芡夂粝到y(tǒng) 河南語音外呼系統(tǒng)公司 外呼電銷機(jī)器人軟件 關(guān)于宗地圖標(biāo)注技術(shù)規(guī)范

本文實(shí)例講述了asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法。分享給大家供大家參考。具體分析如下:

excel是辦公中非常常用的一個(gè)辦公表格了,但我們在開發(fā)中通常會(huì)需要直接把excel數(shù)據(jù)快速導(dǎo)入到數(shù)據(jù)庫中了,這里整理了一個(gè)asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的例子供各位參考學(xué)習(xí)。

注意:EXCEL中的第一行不能導(dǎo)入。
下面是源碼:IntoExcel.aspx:

復(fù)制代碼 代碼如下:
%@ Page  AutoEventWireup="true" CodeFile="IntoExcel.aspx.cs" Inherits="study_IntoExcel" %> 
 
!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>無標(biāo)題頁/title> 
script language="javascript" type="text/javascript">!-- 
// !CDATA[ 
function check() { 
var k=//S+/.[xls]/; 
if(!k.test(document.getElementById("fileId").value)) 

    alert("只能上次xls格式的文件"); 
    return false; 

return true; 

// -->/script> 
/head> 
body> 
    form id="form1" runat="server"> 
    div> 
    p> 
        asp:FileUpload ID="fileId" runat="server" /> 
        asp:Button ID="Button1" runat="server" Text="上傳" OnClientClick="return check()" onclick="Button1_Click" />/p> 
    /div> 
    /form> 
/body> 
/html>

IntoExcel.aspx.cs
復(fù)制代碼 代碼如下:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Web.Security; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls.WebParts; 
using System.IO; 
using System.Data.OleDb; 
using System.Data.SqlClient; 
using System.Web.UI.WebControls; 
 
public partial class study_IntoExcel : System.Web.UI.Page 

    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
        /// summary> 
        /// 上傳文件 
        /// /summary> 
        /// param name="sender">/param> 
        /// param name="e">/param> 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            string fileName = fileId.FileName; 
            string savePath = Server.MapPath("~/file/"); 
            FileOperatpr(fileName, savePath); 
            fileId.SaveAs(savePath + fileName); 
            DataOperator(fileName, savePath); 
        } 
        /// summary> 
        /// 數(shù)據(jù)操作 
        /// /summary> 
        /// param name="fileName">/param> 
        /// param name="savePath">/param> 
        private void DataOperator(string fileName, string savePath) 
        { 
            string myString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =  " + savePath + fileName + ";Extended Properties=Excel 8.0"; 
            OleDbConnection oconn = new OleDbConnection(myString); 
            oconn.Open(); 
            DataSet ds = new DataSet(); 
            OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", oconn); 
            oda.Fill(ds); 
            oconn.Close(); 
            DataSetOperator(ds,savePath+fileName); 
        } 
        /// summary> 
        /// 數(shù)據(jù)集操作 
        /// /summary> 
        /// param name="ds">/param> 
        private void DataSetOperator(DataSet ds,string filePath) 
        { 
            SqlConnection conn = new SqlConnection("Data Source=SONYSVR;Initial Catalog=IAR_Factory_811;User ID=sa;Password=P@ssword"); 
            conn.Open(); 
            SqlTransaction str = conn.BeginTransaction();//利用事務(wù)處理 防止中斷 
            int k = 0; 
            if (ds.Tables[0].Rows.Count 1) 
            { 
                Response.Write("script>alert('沒有數(shù)據(jù)!')/script>"); 
                return; 
            } 
            try 
            { 
                for (int i = 0; i ds.Tables[0].Rows.Count; i++) 
                { 
                    string strong>a title="sql" target="_blank">sql/a>/strong>Str = "insert into IntoExcel(Tname,Tage,Taddress)values"; 
                    sqlStr +="('"+ ds.Tables[0].Rows[i][0].ToString()+"',"; 
                    sqlStr += ds.Tables[0].Rows[i][1].ToString()+","; 
                    sqlStr +="'" +ds.Tables[0].Rows[i][2].ToString()+"')"; 
                    SqlCommand cmd = new SqlCommand(sqlStr, conn, str); 
                    cmd.Transaction = str; 
                    k += cmd.ExecuteNonQuery(); 
                } 
                str.Commit(); 
            } 
            catch (Exception ex) 
            { 
                Response.Write("發(fā)生異常,數(shù)據(jù)已回滾/n信息/n" + ex.Message); 
                str.Rollback(); 
            } 
            finally 
            { 
                Response.Write("上傳成功" + k + "條"); 
                File.Delete(filePath); 
            } 
        } 
        /// summary> 
        /// 文件操作 
        /// /summary> 
        /// param name="fileName">/param> 
        /// param name="savePath">/param> 
        private void FileOperatpr(string fileName, string savePath) 
        { 
            if (!Directory.Exists(savePath)) 
            { 
                Directory.CreateDirectory(savePath); 
            } 
            if (File.Exists(savePath + fileName)) 
            { 
                File.Delete(savePath + fileName); 
            } 
        } 

 
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + ";Extended Properties='Excel 8.0;HDR=YES
 Provider=Microsoft.Jet.OLEDB.4.0;;//連接驅(qū)動(dòng)
Data Source=" + savePath + "; // 數(shù)據(jù)庫地址
Extended Properties='Excel 8.0; // 連接的是Excel8.0
HDR=YES;// 有兩個(gè)值:YES/ NO, 這2個(gè)值,說了你是否能直接讀列名,NO,只可以讀下標(biāo)
IMEX=1;//解決數(shù)字與字符混合時(shí),識(shí)別不正常的情況.

這個(gè)讀入數(shù)據(jù)庫的方式不是最佳的,應(yīng)該用office組件
select * from [Sheet1$] //引用EXCLE文件中sheet1工作表的內(nèi)容
OleDB控件用的是OleDb的驅(qū)動(dòng)程序,可以訪問各種數(shù)據(jù)庫  
 
數(shù)據(jù)庫中的字段:
復(fù)制代碼 代碼如下:
create table IntoExcel 

    Tid int identity(1,1) primary key, 
    Tname varchar(50), 
    Tage int, 
    Taddress varchar(200), 
     
)

SQL控件用的是專用的驅(qū)動(dòng)程序,能高效的訪問SQL Server數(shù)據(jù)庫
SQLConnection只能訪問SQL Server,而OleDbConnection則可以訪問所有數(shù)據(jù)庫。  
如果只是訪問SQL Server的話,SQL比OleDb更快。

希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • asp.net core集成CKEditor實(shí)現(xiàn)圖片上傳功能的示例代碼
  • asp.net core webapi文件上傳功能的實(shí)現(xiàn)
  • ASP.NET Core單文件和多文件上傳并保存到服務(wù)端的方法
  • asp.net利用ashx文件實(shí)現(xiàn)文件的上傳功能
  • asp.net大文件上傳解決方案實(shí)例代碼
  • asp.net上傳Excel文件并讀取數(shù)據(jù)的實(shí)現(xiàn)方法
  • ASP.NET Core中使用EPPlus導(dǎo)入出Excel文件的完整步驟
  • ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件實(shí)例
  • ASP.NET之Excel下載模板、導(dǎo)入、導(dǎo)出操作
  • asp.net實(shí)現(xiàn)將Excel中多個(gè)sheet數(shù)據(jù)導(dǎo)入到SQLSERVER中的方法
  • asp.net實(shí)現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表的方法
  • Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
  • ASP.NET下將Excel表格中的數(shù)據(jù)規(guī)則的導(dǎo)入數(shù)據(jù)庫思路分析及實(shí)現(xiàn)
  • ASP.NET 上傳文件導(dǎo)入Excel的示例

標(biāo)簽:樂山 淮北 廣元 吉林 賀州 咸寧 固原 池州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法》,本文關(guān)鍵詞  asp.net,中,EXCEL,數(shù)據(jù),導(dǎo)入,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    苗栗县| 老河口市| 南平市| 康保县| 读书| 乌兰县| 红河县| 龙门县| 耒阳市| 辉南县| 沾益县| 嘉荫县| 荣成市| 前郭尔| 高州市| 望奎县| 资溪县| 文昌市| 廊坊市| 乳山市| 随州市| 中西区| 关岭| 赞皇县| 右玉县| 尤溪县| 永康市| 许昌县| 芜湖市| 瑞安市| 炎陵县| 平泉县| 衡水市| 潮安县| 金湖县| 台中市| 晴隆县| 灵武市| 建湖县| 嵊泗县| 常州市|