You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.5 KiB
46 lines
1.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using ExcelDataReader;
|
|
|
|
namespace Common.Helper.Excel
|
|
{
|
|
public class ExcelHelper
|
|
{
|
|
static ExcelHelper() { }
|
|
|
|
private ExcelHelper() { }
|
|
public static ExcelHelper Instance { get; } = new ExcelHelper();
|
|
|
|
/// <summary>
|
|
/// 读取excel的帮助类 必须传该excel有几列
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <param name="columnCount"></param>
|
|
/// <returns></returns>
|
|
public List<List<string>> Read(string filePath,int columnCount)
|
|
{
|
|
var excelContent = new List<List<string>>();
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
using var stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read);
|
|
using var reader = ExcelReaderFactory.CreateReader(stream);
|
|
do
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
|
|
var cellContent = new List<string>();
|
|
for (var i = 0; i < columnCount; i++)
|
|
{
|
|
cellContent.Add(reader[i]==null?"": reader[i].ToString());
|
|
}
|
|
if(!cellContent.All(string.IsNullOrEmpty))
|
|
excelContent.Add(cellContent);
|
|
}
|
|
} while (reader.NextResult());
|
|
return excelContent;
|
|
}
|
|
}
|
|
}
|
|
|