NPOI操作excel—读增删改
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
下述对象对excel2013等文件不适用,但基本的方法是一样的,可以借鉴
后续代码均是XSSFWorkbook对象
工作本HSSFWorkbook
构造方法,无参表示创建一个新的工作本,可以接收一个流用于打开一个现有的工作本
方法CreateSheet(索引):创建指定索引的sheet对象
方法GetSheetAt(索引):根据索引获取sheet对象
方法CreateCellStyle():创建单元格样式对象
方法CreateFont():创建字体对象
方法Write(stream):将工作本输出到流中
工作表HSSFSheet
方法CreateRow(索引):创建指定索引的行
方法GetRow(索引):根据索引获取行
方法AddMergedRegion():设置合并区域,参数包括开始行索引、开始列索引、结束行索引、结束列索引
方法SetColumnWidth(索引,宽度):设置指定列的宽度,单位是一个字符宽度的256分之1
说明:一个汉字占用两个字符
属性FirstRowNum、LastRowNum:获取第一行、最后一行的索引值
行HSSFRow
方法CreateCell(索引):创建指定索引的行
方法GetCell(索引):根据索引获取单元格
属性HeightInPoints:指定或设置高度
单元格HSSFCell
方法SetCellValue():设置单元格中的值
属性CellValue:获取单元格中指定类型的值,如果类型不匹配则抛异常
属性CellStyle:获取或设置单元格样式
单元格样式HSSFCellStyle
方法SetFont(字体对象):设置字体样式
属性Alignment:水平对齐,1左,2中,3右
字体对象HSSFFont
属性FontHeightInPoints:获取或设置字体大小
属性Boldweight:获取或设置字体加粗
XSSFWorkbook (POI API Documentation)
http://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFWorkbook.html
软件包org.apache.poi.xssf.usermodel
http://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/package-summary.html
string path = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "/UserInfo.xlsx";
AddOneRow.AddRow(user,path);
这里excel文件放在项目根目录下,传入的是相对路径
public class User
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
public string gender { get; set; }
public string address { get; set; }
public string nationality { get; set; }
public string phone { get; set; }
}
读取excel,结果放入List<User>
public List<User> ReadExcel()
{
List<User> list = new List<User>();
//读取文件
using (FileStream stream = new FileStream(Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "/UserInfo.xlsx", FileMode.Open))
{
//创建workbook
XSSFWorkbook workbook = new XSSFWorkbook(stream);
//读取sheet
NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
//读取数据
int rowIndex = 1;
NPOI.SS.UserModel.IRow row = sheet.GetRow(rowIndex++);
while (row != null)
{
//读取一行中的对象
User u = new User();
if (row.GetCell(0) != null)
{
u.id = (int)row.GetCell(0).NumericCellValue;
}
if (row.GetCell(1) != null)
{
row.GetCell(1).SetCellType(NPOI.SS.UserModel.CellType.String);
u.name = row.GetCell(1).StringCellValue;
}
if (row.GetCell(2) != null)
{
u.age = (int)row.GetCell(2).NumericCellValue;
}
if (row.GetCell(3) != null)
{
row.GetCell(3).SetCellType(NPOI.SS.UserModel.CellType.String);
u.gender = row.GetCell(3).StringCellValue;
}
if (row.GetCell(4) != null)
{
row.GetCell(4).SetCellType(NPOI.SS.UserModel.CellType.String);
u.nationality = row.GetCell(4).StringCellValue;
}
if (row.GetCell(5) != null)
{
row.GetCell(5).SetCellType(NPOI.SS.UserModel.CellType.String);
u.phone = row.GetCell(5).StringCellValue;
}
if (row.GetCell(6) != null)
{
row.GetCell(6).SetCellType(NPOI.SS.UserModel.CellType.String);
u.address = row.GetCell(6).StringCellValue;
}
list.Add(u);
row = sheet.GetRow(rowIndex++);
}
return list;
}
}
添加一行
public static void AddRow(User user,string path)
{
XSSFWorkbook workbook;
//读取文件
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
//创建workbook
workbook = new XSSFWorkbook(stream);
//读取sheet
NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
//获取新行索引
int index = sheet.LastRowNum + 1; ;
//添加新行
NPOI.SS.UserModel.IRow row = sheet.CreateRow(index);
row.CreateCell(0).SetCellValue(user.id);
row.CreateCell(1).SetCellValue(user.name);
row.CreateCell(2).SetCellValue(user.age);
row.CreateCell(3).SetCellValue(user.gender);
row.CreateCell(4).SetCellValue(user.nationality);
row.CreateCell(5).SetCellValue(user.phone);
row.CreateCell(6).SetCellValue(user.address);
}
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
workbook.Write(fs);
fs.Close();
fs.Dispose();
}
}
修改一行
public static void UpdateRow(User user,string path)
{
XSSFWorkbook workbook;
//读取文件
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
//创建workbook
workbook = new XSSFWorkbook(stream);
//读取sheet
NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
//获取行索引,并删除
for (int i = 1; ; i++)
{
int ids = (int)sheet.GetRow(i).GetCell(0).NumericCellValue;
if (ids == user.id)
{
//这里可以保证name age gender不为空,但其余不能保证该单元格create过
sheet.GetRow(i).GetCell(1).SetCellValue(user.name);
sheet.GetRow(i).GetCell(2).SetCellValue(user.age);
sheet.GetRow(i).GetCell(3).SetCellValue(user.gender);
if (sheet.GetRow(i).GetCell(4) != null)
{
sheet.GetRow(i).GetCell(4).SetCellValue(user.nationality);
}
else
{
sheet.GetRow(i).CreateCell(4).SetCellValue(user.nationality);
}
if (sheet.GetRow(i).GetCell(5) != null)
{
sheet.GetRow(i).GetCell(5).SetCellValue(user.phone);
}
else
{
sheet.GetRow(i).CreateCell(5).SetCellValue(user.phone);
}
if (sheet.GetRow(i).GetCell(6) != null)
{
sheet.GetRow(i).GetCell(6).SetCellValue(user.address);
}
else
{
sheet.GetRow(i).CreateCell(6).SetCellValue(user.address);
}
break;
}
}
}
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
workbook.Write(fs);
fs.Close();
fs.Dispose();
}
}
删除一行
public static void DeleteRow(int id,string path)
{
XSSFWorkbook workbook;
//读取文件
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
//创建workbook
workbook = new XSSFWorkbook(stream);
//读取sheet
NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
//获取行索引,并删除
for (int i = 1; ; i++)
{
int ids = (int)sheet.GetRow(i).GetCell(0).NumericCellValue;
if (ids == id)
{
if (sheet.LastRowNum != i)
{
sheet.RemoveRow(sheet.GetRow(i));
sheet.ShiftRows(i + 1, sheet.LastRowNum, -1);
}
else
{
sheet.RemoveRow(sheet.GetRow(i));
}
break;
}
}
}
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
workbook.Write(fs);
fs.Close();
fs.Dispose();
}

浙公网安备 33010602011771号