.net c# NPOI excel 导出导入

 一、根据模板导出excel数据

//模板路径

string path = “D:\\xxxx.xlsx”;

IWorkbook workbook = null;

//用workbook 以流的形式打开模板excel文件

using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
workbook = new XSSFWorkbook(file);
}

ISheet sheet = workbook.GetSheetAt(0);
IRow row = null;
MemoryStream ms = new MemoryStream();
for (int i = 2; i < list.Count + 2; i++)
{
row = sheet.CreateRow(i);
row.CreateCell(0).SetCellValue((i - 1).ToString());
}
workbook.Write(ms);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name + ".xlsx");

 

二、导出

HSSFWorkbook workbook = new HSSFWorkbook();
     HSSFSheet sheet = workbook.CreateSheet();
 
     //填充表头
     HSSFRow dataRow = sheet.CreateRow(0);
     foreach (DataColumn column in dtSource.Columns)
     {
         dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
     }
 
 
     //填充内容
     for (int i = 0; i < dtSource.Rows.Count; i++)
     {
         dataRow = sheet.CreateRow(i + 1);
         for (int j = 0; j < dtSource.Columns.Count; j++)
         {
             dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
         }
     }
     MemoryStream ms = new MemoryStream();
     workbook.Write(ms);
    ms.Flush();
    ms.Seek(0, SeekOrigin.Begin);
     return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name + ".xlsx");

三、导入

/// <summary>
/// 读取excel表格中的数据,将Excel文件流转化为dataTable数据源
/// 默认第一行为标题
/// </summary>
/// <param name="stream">excel文档文件流</param>
/// <param name="fileType">文档格式</param>
/// <param name="isSuccess">是否转化成功</param>
/// <param name="resultMsg">转换结果消息</param>
/// <returns></returns>
public DataTable ExcelToDataTable(Stream stream, string fileType, out bool isSuccess, out string resultMsg)
{
isSuccess = false;
resultMsg = "Excel文件流成功转化为DataTable数据源";
var excelToDataTable = new DataTable();

try
{
//Workbook对象代表一个工作簿,首先定义一个Excel工作薄
IWorkbook workbook;

//XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式

#region 判断Excel版本

switch (fileType)
{
//.XLSX是07版(或者07以上的)的Office Excel
case ".xlsx":
workbook = new XSSFWorkbook(stream);
break;
//.XLS是03版的Office Excel
case ".xls":
workbook = new HSSFWorkbook(stream);
break;

default:
throw new Exception("Excel文档格式有误");
}

#endregion 判断Excel版本

var sheet = workbook.GetSheetAt(0);
var rows = sheet.GetRowEnumerator();

var headerRow = sheet.GetRow(0);
int cellCount = headerRow.LastCellNum;//最后一行列数(即为总列数)

//获取第一行标题列数据源,转换为dataTable数据源的表格标题名称
for (var j = 0; j < cellCount; j++)
{
var cell = headerRow.GetCell(j);
excelToDataTable.Columns.Add(cell.ToString());
}

//获取Excel表格中除标题以为的所有数据源,转化为dataTable中的表格数据源
for (var i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
var dataRow = excelToDataTable.NewRow();

var row = sheet.GetRow(i);

if (row == null) continue; //没有数据的行默认是null 

for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)//单元格内容非空验证
{
#region NPOI获取Excel单元格中不同类型的数据

//获取指定的单元格信息
var cell = row.GetCell(j);
switch (cell.CellType)
{
//首先在NPOI中数字和日期都属于Numeric类型
//通过NPOI中自带的DateUtil.IsCellDateFormatted判断是否为时间日期类型
case CellType.Numeric when DateUtil.IsCellDateFormatted(cell):
dataRow[j] = cell.DateCellValue;
break;

case CellType.Numeric:
//其他数字类型
dataRow[j] = cell.NumericCellValue;
break;
//空数据类型
case CellType.Blank:
dataRow[j] = "";
break;
//公式类型
case CellType.Formula:
{
if (fileType == ".xls")
{
cell.SetCellType(CellType.String);
dataRow[j] = cell.StringCellValue;
//HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(workbook);
//dataRow[j] = eva.Evaluate(cell).StringValue;
break;
}
else
{
cell.SetCellType(CellType.String);
//XSSFFormulaEvaluator eva = new XSSFFormulaEvaluator(workbook);
dataRow[j] = cell.StringCellValue;
break;
}
}
//布尔类型
case CellType.Boolean:
dataRow[j] = row.GetCell(j).BooleanCellValue;
break;
//错误
case CellType.Error:
dataRow[j] = row.GetCell(j).ErrorCellValue;
break;
//其他类型都按字符串类型来处理(未知类型CellType.Unknown,字符串类型CellType.String)
default:
dataRow[j] = cell.StringCellValue;
break;
}

#endregion NPOI获取Excel单元格中不同类型的数据
}
}
excelToDataTable.Rows.Add(dataRow);
}

isSuccess = true;
}
catch (Exception e)
{
resultMsg = e.Message;
}

return excelToDataTable;
}

 

上一篇:ABP实现文件下载


下一篇:纯js上传exsl文件