asp.net xls表格匯入詳細說明與代碼

來源:互聯網
上載者:User

在開發應用中xls檔案匯入到資料庫教程是常用的,下面我們來看這款程式,有需要的朋友可以下載。

using system;
using system.configuration;
using system.data;
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.io;   public partial class _default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {     }               /// <summary>
    /// 上傳excel檔案
    /// </summary>
    /// <param name="inputfile">上傳的控制項名</param>
    /// <returns></returns>
    private string uploadxls(system.web.ui.htmlcontrols.htmlinputfile inputfile)
    {
        string orifilename = string.empty;
        string uploadfilepath = string.empty;
        string modifyfilename = string.empty;
        string fileext = "" ;//副檔名
        int filesize = 0;//檔案大小
        try
        {
            if(inputfile.value != string.empty)
            {
                //得到檔案的大小
                filesize = inputfile.postedfile.contentlength;
                if(filesize == 0 )
                {
                    throw new exception("匯入的excel檔案大小為0,請檢查是否正確!");
                }
                //得到副檔名
                fileext = inputfile.value.substring(inputfile.value.lastindexof(".")+1);
                if(fileext.tolower() != "xls")
                {                                                              throw new exception("你選擇的檔案格式不正確,只能匯入excel檔案!");
                }
                //路徑
                uploadfilepath = server.mappath("~/");
                //新檔案名稱
                modifyfilename = system.guid.newguid().tostring();
                modifyfilename += "."+inputfile.value.substring(inputfile.value.lastindexof(".")+1);
                //判斷是否有該目錄
                system.io.directoryinfo dir = new system.io.directoryinfo(uploadfilepath);                                                   if (!dir.exists)
                {
                    dir.create();
                }
                orifilename = uploadfilepath+modifyfilename;
                //如果存在,刪除檔案
                if(file.exists(orifilename))
                {
                    file.delete(orifilename);
                }
                // 上傳檔案
                inputfile.postedfile.saveas(orifilename);
            }
            else
            {
                throw new exception("請選擇要匯入的excel檔案!");                                   }
        }
        catch(exception ex)
        {
            throw ex;
        }
        return orifilename;
    }           protected void btnimport_click(object sender, system.eventargs e)
    {              try
      {
          string strfilename = this.uploadxls(this.comfile);
          string strconn = "provider=microsoft.jet.oledb.4.0;data source=" + strfilename + ";extended properties='excel 8.0;hdr=no;imex=1'";
          oledbconnection conn = new oledbconnection(strconn);
          oledbdataadapter mycommand = new oledbdataadapter("select * from [sheet1$]", strconn);
          dataset mydataset = new dataset();
          mycommand.fill(mydataset);
          string str = "";
      }
      catch (exception ex)
      {
          throw ex;
      }
      finally
      {
          deletefile(strfilename);
      }       

    }           /// <summary>
    /// 刪除檔案
    /// </summary>
    /// <param name="filename">待刪除的檔案名稱</param>
    private void deletefile(string filename)
    {
        if (filename != string.empty && file.exists(filename))
        {
            file.delete(filename);
        }
    }

前台代碼

<%@ page language="c#" default.asp教程x.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "<a href="http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
    <title>無標題頁</title>
</head>
<body>
    <form id="form1" runat="server">
        請選擇要匯入的檔案:<input id="fileexcel" style="width: 300px" type="file" size="42" name="filephoto" runat="server" />
        <asp:button id="btnimport" text="導 入" css教程class="button" runat="server" onclick="btnimport_click"></asp:button><br />
        <asp:label id="lblmessage" runat="server" font-bold="true" forecolor="red"></asp:label>
    </form>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.