using (FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs, Encoding.UTF8))
{
byte[] hd4 = br.ReadBytes(4);
string hex = string.Empty;
foreach (var b in hd4)
{
hex += b.ToString("X2"); // 转16进制
}
fs.Position = 0; // 将流位置归零设置至起始点
switch (hex)
{
case "D0CF11E0":
wb = new HSSFWorkbook(fs); // .xls
break;
case "504B0304":
wb = new XSSFWorkbook(fs); // .xlsx
break;
default:
return null;
}
return SheetToDataTable(wb, sheet);
}
}
C# 判断文件后缀名(Excel格式xls、xlsx)
于 2022-11-23 11:15:02 首次发布