91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

進度條在.net導入Excel時的應用實例分析

發布時間:2021-09-02 11:25:44 來源:億速云 閱讀:141 作者:chen 欄目:開發技術

這篇文章主要介紹“進度條在.net導入Excel時的應用實例分析”,在日常操作中,相信很多人在進度條在.net導入Excel時的應用實例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”進度條在.net導入Excel時的應用實例分析”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

本文實例講述了進度條在.net導入Excel時的應用,分享給大家供大家參考。具體實現方法如下:

在程序開發過程中,往往會涉及到將Excel表格導入到數據庫中的需求,而當excel表格內容很多的時候,我們往往會很難去捕捉它的執行過程進度和一些錯誤信息,此時我們便可以通過以下方法去解決這些難題,具體實現過程分析如下:

一、建立一個web應用程序,在程序中首先創建一個html文件命名為ProgressBar,文件內容如下:

復制代碼 代碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
    //開始處理
    function BeginTrans(msg) {
        WriteText(msg);
    }
    //設置進度條進度
    function SetPorgressBar(msg, pos) {
        ProgressBar.style.width = pos + "%";
        WriteText(msg + " 已完成" + pos + "%");
    }
    //處理結束
    function EndTrans(msg) {
        if (msg == "")
            WriteText("完成。");
        else
            WriteText(msg);
    }
    //設置時間信息
    function SetTimeInfo(msg) {
        WriteText(msg);
    }
    // 更新文本顯示信息
    function WriteText(str) {
        var strTag = '<font face="Verdana, Arial, Helvetica" size="2" color="#ea9b02"><B>' + str + '</B></font>';
        document.getElementById("Msg2").innerHTML = strTag;
    }
</script>
</head>
<body>
<table align="center" >
    <tr ><td></td></tr>
    <tr>
        <td>
            <div id="ProgressBarSide" >
                <div id="ProgressBar" align="center" ></div>
            </div>
        </td>
        <td>
        <div id="Msg2" ></div>
        </td>
    </tr>
    <tr ><td></td></tr>
</table>
</body>
</html>


二、創建一個aspx頁面,前后端代碼分別如下:

復制代碼 代碼如下:

//1.這里為了簡便,我只寫出了前端頁面中的body體部分供參考:
<form id="forms" runat = "server">
<table align="center" >
    <tr ><td></td></tr>
<tr>
       <td align="center" > Excel文件</td>
       <td >
       <asp:FileUpload ID="fuGlossaryXls" runat="server"/>
       <asp:Label ID="Label2" runat="server" Font-Bold="True" ForeColor="Red" Text="不能為空"
                                                    Visible="False"></asp:Label></td>
                                                    <td>
        <asp:Button ID="Button1" runat="server" CssClass="mybotton" Text="導入" Width="60px" onclick="Button1_Click"/></td>
</tr>
</table>
</form>
//2.后端部分代碼如下:
 //這里是激發導入按鈕點擊事件
        protected void Button1_Click(object sender, EventArgs e)
        {
            string cfilename = this.fuGlossaryXls.FileName;//獲取準備導入的文件名稱
            if (cfilename == "")
            {
                Label2.Visible = true;
                return;
            }
            else
            {
                Label2.Visible = false;
            }
            //////////////顯示進度/////////////////////////////////////////////////////////////////////////////
            DateTime startTime = System.DateTime.Now;
            DateTime endTime = System.DateTime.Now;

            // 根據 ProgressBar.htm 顯示進度條界面
            string templateFileName = Path.Combine(Server.MapPath("."), "ProgressBar.htm");
            StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("gb2312"));
            string html = reader.ReadToEnd();
            reader.Close();
            Response.Write(html);
            Response.Flush();
            System.Threading.Thread.Sleep(1000);

            string jsBlock;
            // 處理完成
            jsBlock = "<script>BeginTrans('正在加載數據,請耐心等待...');</script>";
            Response.Write(jsBlock);
            Response.Flush();

             string fileName = fuGlossaryXls.PostedFile.FileName.Substring(fuGlossaryXls.PostedFile.FileName.LastIndexOf("\\") + 1);//獲取準備導入文件的文件名
             string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1);//獲取準備導入文件的后綴名
            
             System.Threading.Thread.Sleep(200);

             int maxrows = 0;//用來記錄需要加載的數據總行數
             bool err = false;//用來記錄加載狀態
             int errcount = 0;//用來記錄加載錯誤行數
             if (fuGlossaryXls.HasFile)//判斷當前是否有選取文件
             {
                 if (suffix == "xlsx")
                 {
                     DataTable dt = ExcelImport(fileName);
                     for (int i = 0; i < dt.Rows.Count; i++)
                     {
                         maxrows++;
                     }
                     //////////拓展////////////////////////////////////////////////////////
                     //DataView myView = new DataView(dt);
                     //myView.RowFilter = "name is not null";
                     //int t = myView.Count;//獲取滿足RowFilter 條件的數據行
                     //////////拓展////////////////////////////////////////////////////////
                     string sqlconnect = "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456;";//本地數據庫鏈接
                     SqlConnection conn = new SqlConnection(sqlconnect);
                     SqlTransaction myTrans = null;
                     try
                     {
                         SqlCommand cmd = new SqlCommand(null, conn);
                         conn.Open();
                         myTrans = conn.BeginTransaction();
                         cmd.Transaction = myTrans;
                         cmd.CommandText = "delete from test";
                         cmd.ExecuteNonQuery();//首先執行清除表內容操作
                         for (int j = 0; j < dt.Rows.Count; j++)//循環向數據庫中插入excel數據
                         {
                             if (string.IsNullOrEmpty(dt.Rows[j][0].ToString()))
                             {
                                 jsBlock = "<script>EndTrans('第" + j.ToString() + "行數據寫入錯誤。');</script>";
                                 Response.Write(jsBlock);
                                 Response.Flush();
                                 err = true;
                                 errcount++;
                             }
                             else
                             {
                                 cmd.CommandText = string.Format("insert into test values('{0}','{1}','{2}','{3}')", dt.Rows[j][0], dt.Rows[j][1], dt.Rows[j][2], dt.Rows[j][3]);
                                 cmd.ExecuteNonQuery();//逐行向表中插入數據,注意字段的對應
                             }
                             System.Threading.Thread.Sleep(1000);
                             float cposf = 0;
                             cposf = 100 * (j + 1) / maxrows;
                             int cpos = (int)cposf;
                             jsBlock = "<script>SetPorgressBar('已加載到第" + (j + 1).ToString() + "條','" + cpos.ToString() + "');</script>";
                             Response.Write(jsBlock);
                             Response.Flush();
                         }
                         myTrans.Commit();//提交
                     }
                     catch (Exception ex)
                     {
                         myTrans.Rollback();//回滾
                         ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + ex.Message + "');</script>");
                     }
                     finally
                     {
                         conn.Dispose();
                         conn.Close();//關閉數據庫連接
                     }
                 }
                 else
                 {
                     ClientScript.RegisterStartupScript(GetType(), "", "alert('請選擇Excel文件!');", true);
                 }
             }
             else
             {
                 ClientScript.RegisterStartupScript(GetType(), "", "alert('請選擇要導入的Excel!');", true);
             }
             if (!err)//加載中并沒有出現錯誤
             {
                 // 處理完成
                 jsBlock = "<script>EndTrans('處理完成。');</script>";
                 Response.Write(jsBlock);
                 Response.Flush();
             }
             else
             {
                 jsBlock = "<script>EndTrans('共有"+maxrows.ToString()+"條數據需要加載,其中 有"+errcount.ToString()+"條數據錄入錯誤!');</script>";
                 Response.Write(jsBlock);
                 Response.Flush();
             }
             System.Threading.Thread.Sleep(1000);

             endTime = DateTime.Now;//錄入完成所用時間
             TimeSpan ts1 = new TimeSpan(startTime.Ticks);
             TimeSpan ts2 = new TimeSpan(endTime.Ticks);
             TimeSpan ts = ts2.Subtract(ts1).Duration(); //取開始時間和結束時間兩個時間差的絕對值
             String spanTime = ts.Hours.ToString() + "小時" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒";
             jsBlock = "<script>SetTimeInfo('加載完成,共用時" + spanTime + "');</script>";
             Response.Write(jsBlock);
             Response.Flush();

        }
        public DataTable ExcelImport(string fileName) //建立Excel表鏈接,返回Excel表數據
        {
                //EXCEL 的連接串
                string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=C:\\Documents and Settings\\Administrator\\桌面\\" + fileName + ";" +
                "Extended Properties='Excel 8.0;IMEX=1';";
                //string sConnectionString = "Microsoft.ACE.OLEDB.4.0;" +
                //"Data Source=C:\\Documents and Settings\\Administrator\\桌面\\" + fileName + ";" +
                //"Extended Properties='Excel 8.0;IMEX=1';";
                OleDbConnection objConn = new OleDbConnection(sConnectionString);//建立EXCEL的連接

//說明:程序運行到這里的時候有時會出錯“未在本地計算機上注冊“Microsoft.ACE.OLEDB.12.0”提供程序”,此時大多數情況下我們只需要去http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe下載一個AccessDatabaseEngine.exe安裝即可,原因在于你的office沒有安裝ACCESS組件
                objConn.Open();
                OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
                objAdapter1.SelectCommand = objCmdSelect;
                DataSet objDataset1 = new DataSet();
                objAdapter1.Fill(objDataset1, "XLData");
                DataTable dt = objDataset1.Tables[0];
                //DataView myView = new DataView(dt);
                objConn.Close();//關閉EXCEL的連接
                return dt;
}

三、項目執行過程中的效果圖展示如下:

進度條在.net導入Excel時的應用實例分析
 
進度條在.net導入Excel時的應用實例分析
 
進度條在.net導入Excel時的應用實例分析
 
進度條在.net導入Excel時的應用實例分析
 
進度條在.net導入Excel時的應用實例分析
 
進度條在.net導入Excel時的應用實例分析
 
進度條在.net導入Excel時的應用實例分析
進度條在.net導入Excel時的應用實例分析

這個是程序測試中使用的excel表格實例。

到此,關于“進度條在.net導入Excel時的應用實例分析”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

拉萨市| 安新县| 湘西| 淅川县| 马公市| 吐鲁番市| 昭通市| 泊头市| 江川县| 滨州市| 军事| 聊城市| 宁陕县| 萨迦县| 德州市| 剑河县| 定兴县| 喀喇沁旗| 凤山县| 泾源县| 垣曲县| 土默特左旗| 绥滨县| 江阴市| 孝感市| 衡东县| 龙南县| 云霄县| 乌兰察布市| 铜山县| 涿鹿县| 哈巴河县| 赤城县| 习水县| 扶沟县| 阿城市| 银川市| 青神县| 肇源县| 小金县| 巴里|