网上很多教程,但基本都是把EXCEL压缩后再解压读取XML文件的信息。这样并不是太友好。我这写一下自己用的方法,该方法仅在一行只有一列图片的情况下使用成功,多列请修改相应代码;
本次案例已读取单元格图片为例,先贴完整代码:
/// <summary>
/// 读取excel某一个表格 ,默认第一行为列名
/// </summary>
/// <param name="fileName">excel文档路径</param>
/// <param name="Index">工作区序号</param>
/// <returns></returns>
public static DataTable Import(string fileName, int Index)
{
DataTable data = new DataTable();
int startRow = 0;
try
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
IWorkbook workbook = null;
IFormulaEvaluator formulaEvaluator = null;
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
{
workbook = new XSSFWorkbook(fs);
formulaEvaluator = new XSSFFormulaEvaluator(workbook);
}
else if (fileName.IndexOf(".xls") > 0)// 2003版本
{
workbook = new HSSFWorkbook(fs);
formulaEvaluator = new HSSFFormulaEvaluator(workbook);
}
ISheet sheet = workbook.GetSheetAt(Index);
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
ICell cell = firstRow.GetCell(i);
if (cell != null)
{
string cellValue = cell.StringCellValue;
if (cellValue != null)
{
DataColumn column = new DataColumn(cellValue);
data.Columns.Add(column);
}
}
}
startRow = sheet.FirstRowNum + 1;
//最后一列的标号
int rowCount = sheet.LastRowNum;
for (int i = startRow; i <= rowCount; ++i)
{
IRow row = sheet.GetRow(i);
if (row == null) continue; //没有数据的行默认是null
DataRow dataRow = data.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
if (row.GetCell(j) != null && row.GetCell(j).ToString().Trim() != string.Empty)//没有数据的单元格都默认是null
{
if (row.GetCell(j).CellType == CellType.Formula)
{
var formulaValue = formulaEvaluator.Evaluate(row.GetCell(j));
if (formulaValue.CellType == CellType.Numeric)
{
dataRow[j] = formulaValue.NumberValue;
}
else if (formulaValue.CellType == CellType.String)
{
dataRow[j] = formulaValue.StringValue.Replace('"', char.MinValue);
}
}
else
dataRow[j] = row.GetCell(j).ToString().Replace('"', char.MinValue);
}
}
data.Rows.Add(dataRow);
}
}
return data;
}
catch (Exception ex)
{
return null;
}
}
核心为: var Pictures = workbook.GetAllPictures():获取表格的所有图片;然后在循环读取表格的行数据,再循环读取行单元格;下列代码是循环读取单元格的内容。
利用的特性是NPOI读取图片时会抛出异常。我们只用捕获异常,在catch代码块中处理Pictures获取的图片信息即可,因为是循环读取。所以是能确定图片在哪一行的。(注意:GetAllPictures方法只能获取不同的图片,相同的图片只能读到一个)
try
{
switch (row.GetCell(j).CellType)
{
case CellType.Unknown:
dataRow[j] = row.GetCell(j).ToString().Replace('"', char.MinValue);
break;
case CellType.String:
dataRow[j] = row.GetCell(j).ToString().Replace('"', char.MinValue);
break;
case CellType.Numeric:
dataRow[j] = row.GetCell(j).ToString().Replace('"', char.MinValue);
break;
case CellType.Formula:
var formulaValue = formulaEvaluator.Evaluate(row.GetCell(j));
if (formulaValue.CellType == CellType.Numeric)
{
dataRow[j] = formulaValue.NumberValue;
}
else if (formulaValue.CellType == CellType.String)
{
dataRow[j] = formulaValue.StringValue.Replace('"', char.MinValue);
}
break;
case CellType.Blank:
dataRow[j] = row.GetCell(j).ToString().Replace('"', char.MinValue);
break;
case CellType.Boolean:
dataRow[j] = row.GetCell(j).ToString().Replace('"', char.MinValue);
break;
case CellType.Error:
dataRow[j] = row.GetCell(j).ToString().Replace('"', char.MinValue);
// 正常处理逻辑
break;
default:
// 其他类型的处理逻辑
break;
}
}
catch (Exception)//当抛出异常说明是图片
{
if (Pictures.Count > 0)
{
var image = (NPOI.XSSF.UserModel.XSSFPictureData)Pictures[0];
var newpath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Images", $"{row.GetCell(4)}.{image.PictureType}");//设置图片路径 // 确保目录存在
var directoryPath = Path.GetDirectoryName(newpath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.WriteAllBytes(newpath, image.Data);
Pictures.RemoveAt(0);
dataRow[j] = newpath;
}
}
end....大家有更好的方法欢迎评论区留个入口。