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;
            }

            }

  • 相关阅读:
    python列表作为默认参数的问题
    python 强制停止线程
    cProfile 分析python运行时间
    python global全局变量 模块通信问题
    ajax请求数据get、post
    vue中加载three.js全景图
    vue中加载three.js的gltf模型
    vue-cli2.x与vue-cli3.x的搭建
    cesium加载gltf模型
    vue/cli3引入cesium
  • 原文地址:https://www.cnblogs.com/lgxll/p/2545455.html
Copyright © 2011-2022 走看看