异常将Excel数据导入SQL Server

时间:2012-08-02 21:10:20

标签: c# sql-server excel

我有一个用户上传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连接)
  在System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions)   options,Object poolGroupProviderInfo,DbConnectionPool池,   DbConnection拥有对象)
  在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()
  在adminMeadList.aspx.cs中的admin.admin_uploadMemberList.importExcel(String filePath,String fileName):第91行

OleDB connectionstring:

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

这怎么可能?

1 个答案:

答案 0 :(得分: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;
    }
}

它只是覆盖文件应该 - 但也许有人可以解释为什么我会得到例外。