本帖内容来自网络+自己稍作整理,已找不到原贴,侵删

个人比较习惯用NPOI操作excel,方便易理解。在宇宙第一IDE(笑)——VS2017中插入NPOI就很方便:

首先安装NPOI:

然后在.cs文件中加入如下引用:

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;

XSSF是用于.xlsx(2007以后版本)

HSSF是用于.xls(2007以前版本)

同时我的代码中要用到Datatable,用于存储表格数据

读写文件需要IO

using System.Data;
using System.IO

接下来是读写excel的代码:

首先从excel中读入数据存入datatable并返回:

        /// <summary>
/// Excel导入成DataTble
/// </summary>
/// <param name="file">导入路径(包含文件名与扩展名)</param>
/// <returns></returns>
public static DataTable ExcelToTable(string file)
{
DataTable dt = new DataTable();
IWorkbook workbook;
string fileExt = Path.GetExtension(file).ToLower();
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(fs); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(fs); } else { workbook = null; }
if (workbook == null) { return null; }
ISheet sheet = workbook.GetSheetAt(); //表头
IRow header = sheet.GetRow(sheet.FirstRowNum);
List<int> columns = new List<int>();
for (int i = ; i < header.LastCellNum; i++)
{
object obj = GetValueType(header.GetCell(i));
if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
}
else
dt.Columns.Add(new DataColumn(obj.ToString()));
columns.Add(i);
}
//数据
for (int i = sheet.FirstRowNum + ; i <= sheet.LastRowNum; i++)
{
DataRow dr = dt.NewRow();
bool hasValue = false;
foreach (int j in columns)
{
dr[j] = GetValueType(sheet.GetRow(i).GetCell(j));
if (dr[j] != null && dr[j].ToString() != string.Empty)
{
hasValue = true;
}
}
if (hasValue)
{
dt.Rows.Add(dr);
}
}
}
return dt;
}

同时支持.xlsx和.xls

上面代码用到了GetValueType函数:

        /// <summary>
/// 获取单元格类型
/// </summary>
/// <param name="cell">目标单元格</param>
/// <returns></returns>
private static object GetValueType(ICell cell)
{
if (cell == null)
return null;
switch (cell.CellType)
{
case CellType.Blank:
return null;
case CellType.Boolean:
return cell.BooleanCellValue;
case CellType.Numeric:
return cell.NumericCellValue;
case CellType.String:
return cell.StringCellValue;
case CellType.Error:
return cell.ErrorCellValue;
case CellType.Formula:
default:
return "=" + cell.CellFormula;
}
}

最后是datatable写入excel(仅适用于.xlsx)文件:

        /// <summary>
/// Datable导出成Excel(xlsx)
/// </summary>
/// <param name="dt"></param>
/// <param name="file">导出路径(包括文件名与扩展名)</param>
public static void TableToExcel(DataTable dt, string file)
{
IWorkbook workbook;
string fileExt = Path.GetExtension(file).ToLower();if (workbook == null) { return; } ISheet sheet = string.IsNullOrEmpty(dt.TableName) ? workbook.CreateSheet("sheet0") : workbook.CreateSheet(dt.TableName);
//表头
IRow row = sheet.CreateRow();
for (int i = ; i < dt.Columns.Count; i++)
{
ICell cell = row.CreateCell(i);
cell.SetCellValue(dt.Columns[i].ColumnName);
} //数据
for (int i = ; i < dt.Rows.Count; i++)
{
IRow row1 = sheet.CreateRow(i + );
for (int j = ; j < dt.Columns.Count; j++)
{
ICell cell = row1.CreateCell(j);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
}
//转为字节数组
MemoryStream stream = new MemoryStream();
workbook.Write(stream);
var buf = stream.ToArray(); //保存为Excel文件
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Write))
{
fs.Write(buf, , buf.Length);
fs.Flush();
} }

其中:

using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Write))

这一行,FileMode.open会在已有的文件中加入你所create的sheet,适用FileMode.create会创新新文件,几遍已有文件,也会删掉该文件。

这是写入.xls文件的代码

        /// <summary>
/// 将datatable写入到excel(xls)
/// </summary>
/// <param name="dt">datatable</param>
/// <param name="filepath">写入的文件路径</param>
/// <returns></returns>
public static bool DataTableToExcel(DataTable dt, string filepath)
{
bool result = false;
IWorkbook workbook = null;
FileStream fs = null;
IRow row = null;
ISheet sheet = null;
ICell cell = null;
try
{
if (dt != null && dt.Rows.Count > )
{
workbook = new HSSFWorkbook();
sheet = workbook.CreateSheet("Sheet0");//创建一个名称为Sheet0的表
int rowCount = dt.Rows.Count;//行数
int columnCount = dt.Columns.Count;//列数 int cellnum; //设置列头
row = sheet.CreateRow();//excel第一行设为列头
for (int c = ; c < columnCount; c++)
{
cell = row.CreateCell(c);
cell.SetCellValue(dt.Columns[c].ColumnName);
} //设置每行每列的单元格,
for (int i = ; i < rowCount; i++)
{
row = sheet.CreateRow(i + );
for (int j = ; j < columnCount; j++)
{
cell = row.CreateCell(j);//excel第二行开始写入数据
//cell.SetCellValue(dt.Rows[i][j].ToString()); //保存单元格格式为数字
if (j < )
{
cell.SetCellValue(dt.Rows[i][j].ToString());
}
else
{
//cell.SetCellValue(int.Parse(dt.Rows[i][j].ToString()));
if (dt.Rows[i][j] is DBNull)
{
cell.SetCellValue(dt.Rows[i][j].ToString());
}
else
{
cellnum = Convert.ToInt32(dt.Rows[i][j].ToString());
cell.SetCellValue(cellnum);
}
}
}
}
if (System.IO.File.Exists(filepath))
{
if (MessageBox.Show("该文件已存在!确定覆盖吗?", "WARNING", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
File.Delete(filepath);
}
else
{
return false;
} }
using (fs = File.OpenWrite(filepath))
{
workbook.Write(fs);//向打开的这个xls文件中写入数据
result = true;
}
}
return result;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
if (fs != null)
{
fs.Close();
}
return false;
}
}

最后,虽然不常用,但是关键时刻很有用的,写入文件时设置每个单元格数据类型的代码:

        /// <summary>
/// 设置单元格数据类型
/// </summary>
/// <param name="cell">目标单元格</param>
/// <param name="obj">数据值</param>
/// <returns></returns>
public static void SetCellValue(ICell cell, object obj)
{
if (obj.GetType() == typeof(int))
{
cell.SetCellValue((int)obj);
}
else if (obj.GetType() == typeof(double))
{
cell.SetCellValue((double)obj);
}
else if (obj.GetType() == typeof(IRichTextString))
{
cell.SetCellValue((IRichTextString)obj);
}
else if (obj.GetType() == typeof(string))
{
cell.SetCellValue(obj.ToString());
}
else if (obj.GetType() == typeof(DateTime))
{
cell.SetCellValue((DateTime)obj);
}
else if (obj.GetType() == typeof(bool))
{
cell.SetCellValue((bool)obj);
}
else
{
cell.SetCellValue(obj.ToString());
}
}

最新文章

  1. dedecms 采集规则过滤与替换
  2. WinFrom 公共控件 Listview 的使用
  3. php token的生成
  4. pyspider 简单应用之快速问医生药品抓取(一)
  5. .net mvc中json的时间格式
  6. Inno setup中定制安装路径
  7. Java设计模式5:原型模式
  8. PHP加载另一个文件类的方法
  9. HDU5671Matrix(矩阵行列交换)
  10. When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
  11. 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符
  12. VB 活动添加item元素
  13. openstack组件手动部署整合
  14. oracle 之 COMMENT
  15. hdu5176(并查集)
  16. ezjailserver备份和恢复方法
  17. 3891: [Usaco2014 Dec]Piggy Back
  18. 深入探讨List&lt;&gt;中的一个姿势。
  19. ELK入门使用-与springboot集成
  20. IdentityModel 中文文档(v1.0.0) 目录

热门文章

  1. 【AGC005 F】Many Easy Problems
  2. kafka常见命令
  3. qa角色记一次测试过程回溯
  4. [uboot] (番外篇)uboot relocation介绍(转)
  5. package+explorer不显示项目的问题
  6. Spring中 aop的 xml配置(简单示例)
  7. qt5-帮助文档的说明
  8. Junit 4测试框架使用
  9. sparkStreaming(2.1.0)示范代码
  10. C# DataGridView 更改类型 重绘