资源简介

asp.net 导入excel时,处理合并表头、复杂表头、多行表头 1.解决复杂表头的Excel导入。可以解决任何复杂的表头。 2.导入时,显示请稍后。。。提醒框,完毕后会自动隐藏 3.全面扫描Excel数据,将所有异常详细信息写入Excel,返回客户端,提供用户下载。方便修正错误数据。 4.支持大批量数据导入,经本人测试,3千条数据,只需短短2-5秒钟。 5.为正在此烦恼的朋友们提供参考。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace ImportExcel_Demo
{
    public partial class ImportExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender EventArgs e)
        {
            //添加显示提醒信息框
            this.btn_Add.Attributes.Add(“onclick“ “javascript:document.getElementById(‘runing‘).style.visibility=‘visible‘“);

        }

        #region 隐藏提醒信息框
        protected override void OnPreRenderComplete(EventArgs e)
        {
            runing.style.Add(“visibility“ “hidden“);
        }
        #endregion

        #region 导入处理
        protected void btn_Add_Click(object sender EventArgs e)
        {
            #region 上传提醒
            if (this.fileExcel.PostedFile.FileName.Length == 0)
            {
                excel_msg.Text = “导入返回信息:没有选择文件!“;
                this.excel_msg.ForeColor = System.Drawing.Color.Red;
                return;
            }

            //记录导入耗时
            DateTime time = DateTime.Now;

            Unitily.UploadFile_in upfile = new Unitily.UploadFile_in();
            upfile.UserId = “S9527“;
            upfile.Upload_Type = 0;

            upfile.Save(this.fileExcel.PostedFile);
            #endregion

            try
            {
                //删除多余的表头行,注意删除的顺序必须为倒序
                DeleteRow(upfile.FilePath “Sheet1“ “0“);

                string connectionString = ConfigurationSettings.AppSettings[“ConnectionString“].Replace(“True“ “False“);
                string constr = @“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=“ + upfile.FilePath + “;Extended Properties=‘Excel 8.0;HDR=Yes;IMEX=1‘“;

                string sqlstr = “select * from [Sheet1$]“;

                #region OleDb导入
                using (OleDbConnection sourceConnection = new OleDbConnection(constr))
                {
                    sourceConnection.Open();

                    OleDbCommand ocomm = new OleDbCommand(sqlstr sourceConnection);
                    OleDbDataReader reader = ocomm.ExecuteReader();

                    using (SqlConnection destinationConnection = new SqlConnection(connectionString))
                    {
                        destinationConnection.Open();

                        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
                        {
                            bulkCopy.BatchSize = 1000;//每次传输的行数
                            bulkCopy.BulkCopyTimeout = 3600;

                            //文件Excel中的列与表tbl_cell_gh_temp中的列进行对应   
                            //第一个参数为Excel中的字段   
                            //第二个参数为tbl_cell_gh_temp中的字段

                            bulkCopy.ColumnMappings.Add(“city_id“ “city_id“);
                          

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-02-24 10:08  极速导入Excel\
     文件     2290176  2014-02-22 15:15  极速导入Excel\Aspose.Cells.dll
     文件       53760  2014-02-22 14:38  极速导入Excel\Excel测试数据.xls
     目录           0  2014-02-22 17:47  极速导入Excel\ImportExcel_Demo\
     目录           0  2014-02-22 17:48  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\
     文件         938  2014-02-22 11:45  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo.sln
     文件       29184  2014-02-22 17:31  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo.suo
     目录           0  2014-02-22 11:45  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\App_Data\
     目录           0  2014-02-22 17:47  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\bin\
     文件     2290176  2014-02-22 15:15  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\bin\Aspose.Cells.dll
     文件       31744  2014-02-22 17:19  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\bin\ImportExcel_Demo.dll
     文件       77312  2014-02-22 17:19  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\bin\ImportExcel_Demo.pdb
     目录           0  2014-02-22 17:47  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\Images\
     文件        1215  2014-02-22 17:21  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\Images\load_info_flag.gif
     文件        2969  2014-02-22 12:00  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\Images\saveing.gif
     文件        3388  2014-02-22 17:21  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\ImportExcel.aspx
     文件       12558  2014-02-22 17:19  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\ImportExcel.aspx.cs
     文件        2888  2014-02-22 14:00  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\ImportExcel.aspx.designer.cs
     文件       46572  2014-02-22 17:31  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\ImportExcel_Demo.csproj
     文件        1170  2014-02-22 17:31  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\ImportExcel_Demo.csproj.user
     目录           0  2014-02-22 17:47  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\Models\
     文件      119573  2014-02-22 17:17  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\Models\SQLHelper.cs
     目录           0  2014-02-22 17:47  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\obj\
     目录           0  2014-02-22 17:47  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\obj\Debug\
     文件       70832  2014-02-22 17:21  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         504  2014-02-22 17:13  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\obj\Debug\ImportExcel_Demo.csproj.FileListAbsolute.txt
     文件       31744  2014-02-22 17:19  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\obj\Debug\ImportExcel_Demo.dll
     文件       77312  2014-02-22 17:19  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\obj\Debug\ImportExcel_Demo.pdb
     目录           0  2014-02-22 11:45  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\obj\Debug\TempPE\
     目录           0  2014-02-22 17:47  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\Properties\
     文件        1333  2014-02-22 11:45  极速导入Excel\ImportExcel_Demo\ImportExcel_Demo\Properties\AssemblyInfo.cs
............此处省略802个文件信息

评论

共有 条评论