该文件的扩展名为.xls,但是另存为电子表格XML 2003 .xml // ExcelDatataReader,可以很好地读取文件,但是当它通过ExcelReaderFactory时,它表示该文件具有无效的文件签名
如果(flupdExcel.HasFile) {
字符串扩展名= Path.GetExtension(flupdExcel.PostedFile.FileName);
if (Extension == ".xlsx" || Extension == ".xls" || Extension == ".xml")
{
string filepath = Server.MapPath("~\\Upload");
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
try
{
if (userPostedFile.ContentLength > 0)
{
userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName));
string path = Path.GetFileName(userPostedFile.FileName);
string filepaths = Server.MapPath("~\\Upload" + "\\" + path);
///当读取表示无效文件签名的文件时,文件扩展名为.xls,但是文件类型为.xml
FileStream stream = File.Open(filepaths, FileMode.Open, FileAccess.Read);
using (var dr = ExcelReaderFactory.CreateReader(stream, null))
{
do
{
if (dr.Name.ToUpper() == "INBOD")
{
while (dr.Read())
{
dr.GetValue(0);
}
}
} while (dr.NextResult());
DataSet result = dr.AsDataSet(new ExcelDataSetConfiguration
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration
{
UseHeaderRow = true
}
});
saveInBod(result);
savedataPresion(result);
}
File.Delete(filepaths);
}
}
catch (Exception Ex)
{
lblmensaje.Text += "Error: <br>" + Ex.Message;
}
}
}