欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.Net动态读取Excel文件最简方法

程序员文章站 2023-12-18 14:09:10
注意:页面分别拖拽一个fileupload、button1、label1、gridview控件,并新建一个uploadedexcel文件夹 default.aspx.cs...

注意:页面分别拖拽一个fileupload、button1、label1、gridview控件,并新建一个uploadedexcel文件夹

default.aspx.cs代码:

using system;
using system.collections.generic;
using system.data;
using system.data.oledb;
using system.io;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;

namespace webapplication2
{
  public partial class webform1 : system.web.ui.page
  {
    protected void page_load(object sender, eventargs e)
    {
       delete();
    }

    protected void button1_click(object sender, eventargs e)
    {
      oledbconnection conn = new oledbconnection();
      oledbcommand cmd = new oledbcommand();
      oledbdataadapter da = new oledbdataadapter();
      dataset ds = new dataset();
      string query = null;
      string connstring = "";
      string strfilename = datetime.now.tostring("ddmmyyyy_hhmmss");
      //string strfilename = path.getfilenamewithoutextension(fileupload1.postedfile.filename);
      string strfiletype = path.getextension(fileupload1.filename).tostring().tolower();
      if (strfiletype == ".xls" || strfiletype == ".xlsx")
      {
        fileupload1.saveas(server.mappath("~/uploadedexcel/" + strfilename + strfiletype));
      }
      else
      {
        return;
      }
      string strnewpath = server.mappath("~/uploadedexcel/" + strfilename + strfiletype);
      if (strfiletype.trim() == ".xls")
      {
        connstring = "provider=microsoft.jet.oledb.4.0;data source=" + strnewpath + ";extended properties=\"excel 8.0;hdr=yes;imex=2\"";
      }
      else if (strfiletype.trim() == ".xlsx")
      {
        connstring = "provider=microsoft.ace.oledb.12.0;data source=" + strnewpath + ";extended properties=\"excel 12.0;hdr=yes;imex=2\"";
      }
      query = "select * from [sheet1$]";
      conn = new oledbconnection(connstring);
      if (conn.state == connectionstate.closed)
      {
        conn.open();
      }
      try
      {
        cmd = new oledbcommand(query, conn);
        da = new oledbdataadapter(cmd);
        ds = new dataset();
        da.fill(ds);
        gridview1.datasource = ds.tables[0];
        gridview1.databind();
        label1.text = "读取成功";
      }
      catch (exception ex)
      {
        label1.text = "读取失败";
        response.write(ex);
      }
      finally
      {
        da.dispose();
        conn.close();
        conn.dispose();
      }
    }
    //定时任务
    private void delete()
    {
      directoryinfo di = new directoryinfo(server.mappath("/uploadedexcel/"));
      fileinfo[] fi = di.getfiles("*." + "*");
      datetime dtnow = datetime.now;
      foreach (fileinfo tmpfi in fi)
      {
        timespan ts = dtnow.subtract(tmpfi.lastwritetime);
        if (ts.milliseconds > 100)
        {
          tmpfi.attributes = fileattributes.normal;
          tmpfi.delete();
        }
      }
    }
  }
}

注意:fileupload控件并不能直接获取到文件的绝对路径(ie6及以下除外),只能通过上传到服务器再进行数据加载,然后再删除

上一篇:

下一篇: