读取Excel到DataTable,epplus和npoi方式

读取Excel到DataTable,epplus和npoi方式



/// <summary>
///将指定的Excel的文件转换成DataTable (Excel的第一个sheet)
/// </summary>
/// <param name="fullFielPath">文件的绝对路径</param>
/// <returns></returns>
public DataTable WorksheetToTable(string fullFielPath)
{
try
{
FileInfo existingFile = new FileInfo(fullFielPath);

ExcelPackage package = new ExcelPackage(existingFile);
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];//选定 指定页

return WorksheetToTable(worksheet);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将worksheet转成datatable
/// </summary>
/// <param name="worksheet">待处理的worksheet</param>
/// <returns>返回处理后的datatable</returns>
public DataTable WorksheetToTable(ExcelWorksheet worksheet)
{
//获取worksheet的行数
int rows = worksheet.Dimension.End.Row;
//获取worksheet的列数
int cols = worksheet.Dimension.End.Column;

DataTable dt = new DataTable(worksheet.Name);
DataRow dr = null;
for (int i = 1; i <= rows; i++)
{
if (i > 1)
dr = dt.Rows.Add();

for (int j = 1; j <= cols; j++)
{
//默认将第一行设置为datatable的标题
if (i == 1)
dt.Columns.Add(GetString(worksheet.Cells[i, j].Value));
//剩下的写入datatable
else
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
}
return dt;
}
private string GetString(object obj)
{
try
{
return obj.ToString();
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 将excel中的数据导入到DataTable中
/// </summary>
/// <param name="sheetName">excel工作薄sheet的名称</param>
/// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
/// <returns>返回的DataTable</returns>
public DataTable ExcelToDataTable(string fileName, string sheetName, bool isFirstRowColumn)
{
ISheet sheet = null;
DataTable data = new DataTable();
int startRow = 0;
try
{
IWorkbook workbook = null;
FileStream fs = null;
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
workbook = new HSSFWorkbook(fs); //XSSFWorkbook(fs);
else if (fileName.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 = sheet.GetRow(0);
int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数

if (isFirstRowColumn)
{
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;
}
else
{
startRow = sheet.FirstRowNum;
}

//最后一列的标号
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();
int hasValCount = 0;
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
if (row.GetCell(j) != null && !string.IsNullOrEmpty(row.GetCell(j).ToString().Trim())) //同理,没有数据的单元格都默认是null
{
dataRow[j] = row.GetCell(j).ToString();
hasValCount++;
}
}
if (hasValCount > 0)
data.Rows.Add(dataRow);
}
}

return data;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
return null;
}
}

博文最后更新时间:


评论

  • 暂无评论

发表评论

博客统计

访问量:5096834

博文总数:750 评论总数:909179

原创126 翻译20 转载604