从IIS托管的asp应用程序上传excel文件而不上传完整的行

时间:2017-03-29 13:20:31

标签: c# asp.net excel iis

我开发了C#web应用程序,将excel文件上传到SQL服务器。目标excel文件有近265000 +行。  当我从localhost上传它时,所有行都会上传。我使用IIS在另一台计算机上托管了该应用程序,并尝试上传只有19238行正在上传。

文件大小为16Mb。

这里我复制了示例代码块,但它没用。

 private void bulkUpload()
    {
        btnUpload.Enabled = false;
        string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(ExcelFileUpload.PostedFile.FileName);
        ExcelFileUpload.SaveAs(excelPath);

        string conString = string.Empty;
        string extension = Path.GetExtension(ExcelFileUpload.PostedFile.FileName);
        switch (extension)
        {
            case ".xls": //Excel 97-03
                conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                break;
            case ".xlsx": //Excel 07 or higher
                conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                break;

        }

        conString = string.Format(conString, excelPath);
        using (OleDbConnection excel_con = new OleDbConnection(conString))
        {
            excel_con.Open();
            string Sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
            DataTable dtExcelData = new DataTable();


            using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + Sheet1 + "]", excel_con))
            {
                oda.Fill(dtExcelData);
            }
            excel_con.Close();

            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                {
                    //Set the database table name
                    sqlBulkCopy.DestinationTableName = "dbo.MBQ_All_1";

                    sqlBulkCopy.ColumnMappings.Add("Uniq", "Uniq");
                    sqlBulkCopy.ColumnMappings.Add("StoreClass", "StoreClass");
                    sqlBulkCopy.ColumnMappings.Add("Store", "Store");
                    sqlBulkCopy.ColumnMappings.Add("code", "code");
                    sqlBulkCopy.ColumnMappings.Add("ExtendedDescription", "ExtendedDescription");
                    sqlBulkCopy.ColumnMappings.Add("ITEM_CLASS", "ITEM_CLASS");
                    sqlBulkCopy.ColumnMappings.Add("SUPPLIER", "SUPPLIER");
                    sqlBulkCopy.ColumnMappings.Add("Brands", "Brands");
                    sqlBulkCopy.ColumnMappings.Add("Min", "Min");
                    sqlBulkCopy.ColumnMappings.Add("Max", "Max");

                    con.Open();
                    sqlBulkCopy.WriteToServer(dtExcelData);
                    con.Close();
                    Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Your list uploaded successfully');", true);
                    lblExcelMsg.Text = "Your list uploaded successfully";
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

使用Excel连接字符串添加IMEX = 1并立即上传所有行。