批量导入数据
说明:
(1)参考第一篇博客,读取excel中数据批量导入数据库,[链接](http://blog.csdn.net/realjh/article/details/78546072)
(2)本篇博客是对批量录入操作进行优化,重点在对excel工作薄中的数据进行获取,相信大家
能够准确拿到excel表中的数据,那么批量录入操作就是轻而易举的事情了,而且还可以对数据
的准确性进行一系列相关的验证等等。
读取Excel工作薄代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//引用的类库命名空间
using System.Data;
using System.IO;
using NPOI.SS.UserModel;//引用创建excel工作薄变量
using NPOI.XSSF.UserModel;//工作薄从文件流中获取excel表中的数据
namespace TestImportFromExcel
{
public class Program
{
static void Main(string[] args)
{
//待读取的excel文件路径
string filepath = @"C:/Users/Administrator/Desktop/测试导入.xlsx";
//读取操作成功与失败的提示消息
string message=string.Empty;
//接收sheet表中属性列名
List<List<string>> ColumnName = new List<List<string>>();
//接收sheet表中所有数据
List<List<string>> ColumnData = new List<List<string>>();
bool act = GetDataFromExcel(filepath,ColumnName,ColumnData,ref message );
Console.ReadKey();
}
/// <summary>
/// 读取exel表的操作结果消息提示
/// </summary>
public static bool GetDataFromExcel(string filePath, List<List<string>> columnName, List<List<string>> columnData,ref string message)
{
//监视数据获取操作是否成功
bool bflag = false;
if (File.Exists(filePath))
{
//首先判断文件是否存在
//创建一个excel工作薄对象
IWorkbook workBook = new XSSFWorkbook(filePath);
if (workBook.NumberOfSheets > 0)
{
#region 创建读取sheet表的对象
//拿到sheet个数:说明此处可以作为判断用户是否修改我们模板的sheet数量
int length = workBook.NumberOfSheets;
//创建一个sheet薄对象
ISheet Sheet = null;
//创建一个行单元对象
IRow Row = null;
//创建一个列单元对象
ICell Cell = null;
#endregion
#region 循环遍历读取每个sheet表的数据
for (int i = 0; i < length; i++)
{
#region 做默认读取第一个sheet工作薄
//控制默认情况下只读取第一个sheet
if (i == 1)
{
//不让读取第二个工作薄,跳出循环
break;
}
#endregion
//拿到第一个sheet工作薄
Sheet = workBook.GetSheetAt(i);
//判断表中有没有待录入的数据记录
int len = Sheet.LastRowNum;
#region 需要默认去掉第一行样例数据
if (len > 0)
{
//接收数据类型中间变量
string value = string.Empty;
#region 读取遍历每行记录,获取数据。v=0表示第一列名称属性
for (int v = 0; v < len; v++)
{
//获取单独的一行数据
Row = Sheet.GetRow(v);
#region 遍历每个小单元格获取单元格内容
//拿到改条记录有多少列
int number = Row.LastCellNum;
string tempVariable = string.Empty;
//中间集合记录excel每个条记录的单元格内容
List<string> tempList = new List<string>();
#region 列名称和列数据分区分开获取
//判断是否是遍历第一个列名称
if (v == 0)
{
//拿到列集合,并获取单元格内容
for (int r = 0; r < number; r++)
{
Cell = Row.GetCell(r);
value = string.Empty;
value = Cell == null ? "Blank" : Cell.CellType.ToString();
#region 匹配单元格类型
switch (Cell.CellType.ToString())
{
case "String":
tempVariable = Cell.StringCellValue;
break;
case "Numeric":
#region 识别是日期还是数值
//判断是日期还是数值类型
if (Cell.DateCellValue.ToString("yyyy-MM-dd HH:ss:mm") == "1900-01-01 00:00:00")
{
//表示数值类型
tempVariable = Cell.NumericCellValue.ToString();
}
else
{
tempVariable = Cell.DateCellValue.ToString("yyyy-MM-dd HH:ss:mm");
}
#endregion
break;
case "Blank":
tempVariable = Cell.StringCellValue;
break;
default:
message = "批量录入操作失败!Excel表中第【" + Row.GetCell(0).StringCellValue + "】记录有不符合规范的数据记录。";
return bflag;
break;
}
#endregion
#region 做sql语句操作需要的数据
tempList.Add(tempVariable);
#endregion
}
//记录整个列名称集合
columnName.Add(tempList);
}
else
{
//清空所有数据
tempList = new List<string>();
//表示遍历列数据(包含样例记录)此处需要考核进行去除样例数据批量录入数据库
//拿到列集合,并获取单元格内容
for (int r = 0; r < number; r++)
{
Cell = Row.GetCell(r);
value = string.Empty;
value = Cell == null ? "Blank" : Cell.CellType.ToString();
#region 匹配单元格类型
switch (value)
{
case "String":
tempVariable = Cell.StringCellValue;
break;
case "Numeric":
#region 识别是日期还是数值
//判断是日期还是数值类型
if (Cell.DateCellValue.ToString("yyyy-MM-dd HH:ss:mm") == "1900-01-01 00:00:00")
{
//表示数值类型
tempVariable = Cell.NumericCellValue.ToString();
}
else
{
tempVariable = Cell.DateCellValue.ToString("yyyy-MM-dd HH:ss:mm");
}
#endregion
break;
case "Blank":
tempVariable =string.Empty;
break;
default:
message = "批量录入操作失败!Excel表中第【" + Row.GetCell(0).StringCellValue + "】记录有不符合规范的数据记录。";
return bflag;
break;
}
#endregion
#region 做sql语句操作需要的数据
tempList.Add(tempVariable);
#endregion
}
//记录到整个数据集合中
columnData.Add(tempList);
}
#endregion
#endregion
}
#endregion
bflag = true;
message = "操作成功!【" + workBook.GetSheetName(i) + "】工作薄中的数据已被获取。";
return bflag;
}
else
{
message = "操作失败!【" + workBook.GetSheetName(i) + "】工作薄中没有待批量录入的数据。";
}
#endregion
}
#endregion
}
else
{
message = "操作失败!该Excel文件中没有sheet工作薄。";
}
}
return bflag;
}
}
}
结语
(1)本篇博客是对上一篇批量录入获取excel表中数据方法的重写和优化。解决方案后续上传。
(2)欢迎博友提出意见和建议,相互学习,共同进步。
(3)Demo下载地址:http://download.csdn.net/download/realjh/10166995