我有一个用户上传 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""";
怎么会这样?