zoukankan      html  css  js  c++  java
  • .net 导入Excel 2003版

    using System.Data.OleDb;

    using System.IO;

    .aspx 代码

      <td colspan="5" align="right">
                    <input id="upWOLost" runat="server" type="file" />
                </td>
                <td>
                    <asp:Button ID="btnupfile" runat="server" Text="导入" CssClass="button_green" OnClick="btnupfile_Click" />
                </td>

    .CS代码

       protected void btnupfile_Click(object sender, EventArgs e)
            {
                   

     if (upWOLost.PostedFile.FileName == "")
            {

                Common.Lib.MessageBox.Show(this.Page, "请选择EXCEL文件");
                return;
            }

            string filePath = upWOLost.PostedFile.FileName;//输入到upFile的所有字符,包括文件路径和文件名、文件扩展名  
            string fileName = Path.GetFileName(filePath);
            string fileExtension = Path.GetExtension(fileName);

            if (fileExtension.ToLower() != ".xls" && fileExtension.ToLower() != ".xlsx")
            {
                Common.Lib.MessageBox.Show(this.Page, "文件类型错误,请选择文件格式为[xls]或[xlsx]的文件!");
                return;
            }

            #region=========上传导入文件到服务器==========

            string rootPath = Server.MapPath("../UserFiles");
            //注意:可能要修改你的文件夹的匿名写入权限。
            string newFileName = DateTime.Now.Year.ToString()
                + DateTime.Now.Month.ToString()
                + DateTime.Now.Day.ToString()
                + DateTime.Now.Hour.ToString()
                + DateTime.Now.Minute.ToString()
                + DateTime.Now.Second.ToString()
                + DateTime.Now.Millisecond.ToString()
                + fileExtension;
            upWOLost.PostedFile.SaveAs(rootPath + @"\" + newFileName);

            #endregion

            #region==========获取EXCEL数据并删除Excel文件============
            //string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
            //            + "Data Source=" + rootPath + "\\" + newFileName + ";"
            //            + "Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            //string sConnectionString = "Provider=Microsoft.ace.oledb.12.0;Data Source=" + rootPath
            //    + "\\" + newFileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";

            string sConnectionString = "";
            if (fileExtension.ToLower() == ".xls")
            {
                sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
                           + "Data Source=" + rootPath + "\\" + newFileName + ";"
                           + "Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            }
            if (fileExtension.ToLower() == ".xlsx")
            {
                sConnectionString = "Provider=Microsoft.ace.oledb.12.0;Data Source=" + rootPath
                     + "\\" + newFileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
            }

            //建立EXCEL的连接
            OleDbConnection objConn = new OleDbConnection(sConnectionString);
            objConn.Open();
            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
            OleDbDataAdapter objAdapter = new OleDbDataAdapter();
            objAdapter.SelectCommand = objCmdSelect;
            DataSet objDataset = new DataSet();

            objAdapter.Fill(objDataset, "XLData");
            objConn.Close();//关闭EXCEL的连接

            //刪除文件
            System.IO.FileInfo file = new System.IO.FileInfo(rootPath + "\\" + newFileName);
            if (file.Exists)
            {
                file.Delete();
            }

            DataTable dt = objDataset.Tables[0];
            if (dt.Rows.Count == 0)
            {
                Common.Lib.MessageBox.Show(this.Page, "错误:无导入数据!");
                return;
            }

            }

  • 相关阅读:
    Updates were rejected because the tip of your current branch is behind 问题出现解决方案
    git初始化本地项目及关联github远程库
    git项目提交后执行添加忽略操作
    HTML5基础总结
    图表Echarts的使用
    百度API使用--javascript api进行多点定位
    HtmlAgilityPach基本使用方法
    Redis可以做哪些事儿?
    Asp.Net将Session保存在数据库中
    css基础学习
  • 原文地址:https://www.cnblogs.com/lgxll/p/2545455.html
Copyright © 2011-2022 走看看