0

我有一个用户上传 Excel 文档的页面。该文件包含会员信息,提交后文件内容应替换SQL Server表中包含的会员数据。

在我添加fileUploader代码导致异常之后,情况如何。

if (fileUploader.HasFile)
{
     string contentType = fileUploader.PostedFile.ContentType;
     // Excel 2007 || Excel 2003
     if (contentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") || contentType.Equals("application/vnd.ms-excel"))
     {
        hidePanels();
        Members members = new Members();
        if (members.getAllNotExportedMembers().Count > 0)
        {
           //error message
        }
        else
        {
           bool backupSucceed =
                        members.backUpDatabase(Server.MapPath("~/backup-db") + @"\db-backup-" +
                                               DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".bak");
           if (backupSucceed)
           {
               bool fileUploaded = Helper.saveFile(fileUploader.FileBytes,
                                                            Server.MapPath("~/admin/kar/"),
                                                            fileUploader.FileName, 1, User.Identity.Name);
                if (fileUploaded)
                {
                     bool truncateSucceed = members.truncateTable();
                     if (truncateSucceed)
                     {
                        importExcel(Server.MapPath("~/admin/kar/"), "2012.xlsx");
                     }
                }
            }
        }
     }
}

如果我将所有内容都注释掉,除了:

bool truncateSucceed = members.truncateTable();
if (truncateSucceed)
{
    importExcel(Server.MapPath("~/admin/kar/"), "2012.xlsx");
    showSuccesPanel("Success");
}

它可以正常工作并且不会导致异常。

例外:

System.Data.OleDb.OleDbException (0x80004005):外部表不是预期的格式。
在 System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
在 System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions 选项,对象 poolGroupProviderInfo,DbConnectionPool 池,DbConnection owningObject)
在 System.Data.ProviderBase.DbConnectionFactory。 CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
在 System.Data.OleDb.OleDbConnection.Open()
at admin.admin_uploadMemberList.importExcel(String filePath, String fileName) in uploadMemberList.aspx.cs:line 91

OleDB 连接字符串:

string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + fileName
+ @";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";

怎么会这样?

4

1 回答 1

0

好吧,我找到了导致问题的原因。这是由 saveFile 方法引起的 - 如果我正在上传一个文件并且已经存在同名文件 - 抛出异常。

public static bool saveFile(this Byte[] fileBytes, string filePath, string fileName, int category, string uploadedBy)
{
    try
    {
        FileStream fileStream = new FileStream(filePath + "/" + fileName, FileMode.Create, FileAccess.ReadWrite);
        fileStream.Write(fileBytes, 0, fileBytes.Length);
        fileStream.Dispose();
        fileStream.Close();
        FileUpload fileUpload = new FileUpload();
        return fileUpload.createFileUpload(fileName, category, filePath, uploadedBy, DateTime.Now);
    }
    catch (Exception ex)
    {
        mLog.logMessage(ex, HttpContext.Current.Request.Url.ToString(), 1);
        return false;
    }
}

它只是按照应有的方式覆盖文件-但也许有人可以解释为什么我会遇到异常。

于 2012-08-02T22:27:50.193 回答