usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;usingNPOI.HSSF.UserModel;usingNPOI.SS.UserModel;usingNPOI.XSSF.UserModel;public classExcelHelper
{public classx2003
{#region Excel2003
///
///将Excel文件中的数据读出到DataTable中(xls)///
///
///
public static DataTable ExcelToTableForXLS(stringfile)
{
DataTable dt= newDataTable();using (FileStream fs = newFileStream(file, FileMode.Open, FileAccess.Read))
{
HSSFWorkbook hssfworkbook= newHSSFWorkbook(fs);
ISheet sheet= hssfworkbook.GetSheetAt(0);//表头
IRow header =sheet.GetRow(sheet.FirstRowNum);
List columns = new List();for (int i = 0; i < header.LastCellNum; i++)
{object obj = GetValueTypeForXLS(header.GetCell(i) asHSSFCell);if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" +i.ToString()));//continue;
}elsedt.Columns.Add(newDataColumn(obj.ToString()));
columns.Add(i);
}//数据
for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
{
DataRow dr=dt.NewRow();bool hasValue = false;foreach (int j incolumns)
{
dr[j]= GetValueTypeForXLS(sheet.GetRow(i).GetCell(j) asHSSFCell);if (dr[j] != null && dr[j].ToString() != string.Empty)
{
hasValue= true;
}
}if(hasValue)
{
dt.Rows.Add(dr);
}
}
}returndt;
}///
///将DataTable数据导出到Excel文件中(xls)///
///
///
public static void TableToExcelForXLS(DataTable dt, stringfile)
{
HSSFWorkbook hssfworkbook= newHSSFWorkbook();
ISheet sheet= hssfworkbook.CreateSheet("Test");//表头
IRow row = sheet.CreateRow(0);for (int i = 0; i < dt.Columns.Count; i++)
{
ICell cell=row.CreateCell(i);
cell.SetCellValue(dt.Columns[i].ColumnName);
}//数据
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row1= sheet.CreateRow(i + 1);for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell=row1.CreateCell(j);
cell.SetCellValue(dt.Rows[i][j].ToString());
}
}//转为字节数组
MemoryStream stream = newMemoryStream();
hssfworkbook.Write(stream);var buf =stream.ToArray();//保存为Excel文件
using (FileStream fs = newFileStream(file, FileMode.Create, FileAccess.Write))
{
fs.Write(buf,0, buf.Length);
fs.Flush();
}
}///
///获取单元格类型(xls)///
///
///
private static objectGetValueTypeForXLS(HSSFCell cell)
{if (cell == null)return null;switch(cell.CellType)
{case CellType.Blank: //BLANK:
return null;case CellType.Boolean: //BOOLEAN:
returncell.BooleanCellValue;case CellType.Numeric: //NUMERIC:
returncell.NumericCellValue;case CellType.String: //STRING:
returncell.StringCellValue;case CellType.Error: //ERROR:
returncell.ErrorCellValue;case CellType.Formula: //FORMULA:
default:return "=" +cell.CellFormula;
}
}#endregion}public classx2007
{#region Excel2007
///
///将Excel文件中的数据读出到DataTable中(xlsx)///
///
///
public static DataTable ExcelToTableForXLSX(stringfile)
{
DataTable dt= newDataTable();using (FileStream fs = newFileStream(file, FileMode.Open, FileAccess.Read))
{
XSSFWorkbook xssfworkbook= newXSSFWorkbook(fs);
ISheet sheet= xssfworkbook.GetSheetAt(0);//表头
IRow header =sheet.GetRow(sheet.FirstRowNum);
List columns = new List();for (int i = 0; i < header.LastCellNum; i++)
{object obj = GetValueTypeForXLSX(header.GetCell(i) asXSSFCell);if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" +i.ToString()));//continue;
}elsedt.Columns.Add(newDataColumn(obj.ToString()));
columns.Add(i);
}//数据
for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
{
DataRow dr=dt.NewRow();bool hasValue = false;foreach (int j incolumns)
{
dr[j]= GetValueTypeForXLSX(sheet.GetRow(i).GetCell(j) asXSSFCell);if (dr[j] != null && dr[j].ToString() != string.Empty)
{
hasValue= true;
}
}if(hasValue)
{
dt.Rows.Add(dr);
}
}
}returndt;
}///
///将DataTable数据导出到Excel文件中(xlsx)///
///
///
public static void TableToExcelForXLSX(DataTable dt, stringfile)
{
XSSFWorkbook xssfworkbook= newXSSFWorkbook();
ISheet sheet= xssfworkbook.CreateSheet("Test");//表头
IRow row = sheet.CreateRow(0);for (int i = 0; i < dt.Columns.Count; i++)
{
ICell cell=row.CreateCell(i);
cell.SetCellValue(dt.Columns[i].ColumnName);
}//数据
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row1= sheet.CreateRow(i + 1);for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell=row1.CreateCell(j);
cell.SetCellValue(dt.Rows[i][j].ToString());
}
}//转为字节数组
MemoryStream stream = newMemoryStream();
xssfworkbook.Write(stream);var buf =stream.ToArray();//保存为Excel文件
using (FileStream fs = newFileStream(file, FileMode.Create, FileAccess.Write))
{
fs.Write(buf,0, buf.Length);
fs.Flush();
}
}///
///获取单元格类型(xlsx)///
///
///
private static objectGetValueTypeForXLSX(XSSFCell cell)
{if (cell == null)return null;switch(cell.CellType)
{case CellType.Blank: //BLANK:
return null;case CellType.Boolean: //BOOLEAN:
returncell.BooleanCellValue;case CellType.Numeric: //NUMERIC:
returncell.NumericCellValue;case CellType.String: //STRING:
returncell.StringCellValue;case CellType.Error: //ERROR:
returncell.ErrorCellValue;case CellType.Formula: //FORMULA:
default:return "=" +cell.CellFormula;
}
}#endregion}public static DataTable GetDataTable(stringfilepath)
{var dt = new DataTable("xls");if (filepath.Last()=='s')
{
dt=x2003.ExcelToTableForXLS(filepath);
}else{
dt=x2007.ExcelToTableForXLSX(filepath);
}returndt;
}
}
以前用到的代码,只能帮到这了