这个是判断excel单元格式的方式
public static object ChangeEPPlusCell(ExcelRange cell)
{
if (null == cell.Value) return null;
if (cell.Style.Numberformat.Format.IndexOf("yyyy") > -1 || (cell.Style.Numberformat.Format.IndexOf("月") > -1&& cell.Style.Numberformat.Format.IndexOf("日")>-1 ))
{
return cell.GetValue<DateTime>();
}
else
{
return cell.Value;
}
使用EPPlus 链接excel
public static void ReadInvoicedExcel(string pathfile)
{
FileInfo file = new FileInfo(pathfile);
using ( ExcelPackage page= new ExcelPackage(file) )
{
ExcelWorkbook workbook = page.Workbook;
ExcelWorksheet ex = workbook.Worksheets.First();
int row = ex.Dimension.End.Row;
int cell = ex.Dimension.End.Column;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= cell; j++)
{
string cellNUm = Helper.ChangeEPPlusCell(ex.Cells[i, j]).ToString();
}
}
}
}