在用ASP.NET做企业管理系统的时候很多情况会用到批量导入的功能,微软虽然提供了自带的读取表格的类库,但有时候并不好用;很多时候我们是要借助第三方的库文件来实现;
在这里我用的是NOPI来实现的,之前很多只能读取Excel2003,经过优化后可以读Excel2003和2007以上的版本,也就是.xlsx和.xls结尾的区别;
此方法主要实现的功能是读取Excel表格到DataTable中,具体实现步骤如下:
1.先下载NOPI库文件,在我百度云里面;
链接:http://pan.baidu.com/s/1c2dWasw 密码:3902
2.根据电脑系统的版本选择32和64位的库文件在项目中添加引用
3.在项目中的一个位置处新建一个公共的类来具体实现
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.SS.Util;
namespace WebApplication1.ExcelHelper
{
public class ExcelToDataTable : IDisposable
{
private string fileName = null; //文件名
private static IWorkbook workbook = null;
private static FileStream fs = null;
private bool disposed;
public ExcelToDataTable(string fileName)
{
this.fileName = fileName;
disposed = false;
}
#region 将excel中的数据导入到DataTable中
/// <summary>
/// 将excel中的数据导入到DataTable中
/// </summary>
/// <param name="fileNameurl">表名</param>
/// <param name="sheetName">excel工作薄sheet的名称</param>
/// <returns>返回的DataTable</returns>
public static DataTable ExcelToDataTableHelper(string fileNameurl, string sheetName)
{
return ExcelToDataTableHelper(fileNameurl, sheetName, 1);
}
/// <summary>
/// 将excel中的数据导入到DataTable中
/// </summary>
/// <param name="fileNameurl">表明</param>
/// <param name="sheetName">excel工作薄sheet的名称</param>
/// <param name="ColumnNum">第几行是表头</param>
/// <returns>返回的DataTable</returns>
public static DataTable ExcelToDataTableHelper(string fileNameurl, string sheetName, int ColumnNum)
{
ISheet sheet = null;
DataTable data = new DataTable();
int startRow = 0;
try
{
fs = new FileStream(fileNameurl, FileMode.Open, FileAccess.Read);
if (fileNameurl.IndexOf(".xlsx") > 0) // 2007版本
workbook = new XSSFWorkbook(fs);
else if (fileNameurl.IndexOf(".xls") > 0) // 2003版本
workbook = new HSSFWorkbook(fs);
if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow;
// firstRow = sheet.GetRow(0);
if (ColumnNum > 0)
{
firstRow = sheet.GetRow(ColumnNum - 1);
}
else
{
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 && cellValue != "")
{
DataColumn column = new DataColumn(cellValue);
data.Columns.Add(column);
}
}
}
cellCount = data.Columns.Count;
//找出第几行是列名
startRow = ColumnNum;
//startRow = firstDataNum-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) //同理,没有数据的单元格都默认是null
{
//dataRow[j] = row.GetCell(j).ToString();
//读取Excel格式,根据格式读取数据类型
ICell cell = row.GetCell(j);
dataRow[j] = parseExcel(cell);
}
}
data.Rows.Add(dataRow);
}
}
return data;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
return null;
}
finally
{
if (fs != null)
fs.Close();
}
}
//格式转换
private static String parseExcel(ICell cell)
{
string result = "";
switch (cell.CellType)
{
case CellType.Formula:
HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(workbook);
result = e.Evaluate(cell).StringValue;
break;
case CellType.Numeric:// 数字类型
if (HSSFDateUtil.IsCellDateFormatted(cell))
{// 处理日期格式、时间格式
string sdf = "";
if (cell.CellStyle.DataFormat == HSSFDataFormat
.GetBuiltinFormat("h:mm"))
{
sdf = "HH:mm";
}
else
{// 日期
sdf = "yyyy-MM-dd";
}
DateTime date = cell.DateCellValue;
result = date.ToString(sdf);
}
else if (cell.CellStyle.DataFormat == 58)
{
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
string sdf = "yyyy-MM-dd";
double value = cell.NumericCellValue;
DateTime date = new DateTime(1899, 12, 30); // 起始时间
date = date.AddDays(value);
result = date.ToString(sdf);
}
else
{
result = cell.NumericCellValue.ToString();
}
break;
case CellType.String:// String类型
result = cell.StringCellValue;
break;
case CellType.Blank:
result = "";
break;
default:
result = "";
break;
}
return result;
}
/// <summary>
/// 获取Exce工作薄名称
/// </summary>
/// <param name="fileNameurl"></param>
/// <returns></returns>
public static List<string> GetSheetNames(string fileNameurl)
{
using (FileStream sr = new FileStream(fileNameurl, FileMode.OpenOrCreate))
{
//根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档
HSSFWorkbook workbook = new HSSFWorkbook(sr);
int x = workbook.Workbook.NumSheets;
List<string> sheetNames = new List<string>();
for (int i = 0; i < x; i++)
{
sheetNames.Add(workbook.Workbook.GetSheetName(i));
}
return sheetNames;
}
}
//资源释放
public void Dispose()
{
//释放资源
Dispose(true);
//告诉垃圾回收器不要调用指定对象的Dispose方法
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (fs != null)
fs.Close();
}
fs = null;
disposed = true;
}
}
#endregion
}
}
4.然后直接在后面页面进行调用即可,即可