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

            }

  • 相关阅读:
    Call指令和Ret指令讲解06 零基础入门学习汇编语言53
    【CSDN2012年度博客之星】喜欢本博客的读者,投票赠送《visual C++2010开发权威指南》电子稿感谢支持 ~(截至到2012年12月30日)
    本人新书< Visual C#2010开发权威指南>出版感谢大家一如既往的支持感谢CSDN总裁蒋涛以及他率领的CSDN团队提供的支持!
    在Windows Azure虚拟机上的SQL Server新教程
    Windows Azure Active Directory处理2000 亿个身份验证连接全球的人、 数据和设备
    宣布降低Windows Azure Storage的定价
    本人新书< Windows CE 7开发实战详解>出版感谢大家一如既往的支持感谢CSDN总裁蒋涛以及他率领的CSDN团队提供的支持!
    基于VC++2012图形化解决皇后问题
    3个 Windows Azure SQL Reporting开发的最佳做法
    现实世界的 Windows Azure: IT 公司提高其旗舰产品,为更多客户提供云解决方案
  • 原文地址:https://www.cnblogs.com/lgxll/p/2545455.html
Copyright © 2011-2022 走看看