Java导入、导出excel

Excel是由四个元素组成的分别是:WorkBook(工作簿)、Sheet(工作表)、Row(行)、Cell(单元格),其中包含关系是从左至右,一个WorkBook可以包含多个Sheet,一个Sheet又是由多个Row组成,一个Row是由多个Cell组成。

1.1 创建Excel的元素

1)创建WokrBook

Workbook workbook = new XSSFWorkbook();

2)创建Sheet

 Sheet sheet = workbook.createSheet();
<<===================>>

设置sheet的名称

 Sheet sheet = workbook.createSheet("sheet名称");

3)创建行Row

Row row = sheet.createRow(0);

4)创建单元格Cell

Cell cell = row.createCell(0, CellType.STRING);

可以指定单元格的类型,支持的类型有下面7种:

_NONE(-1),
NUMERIC(0),
STRING(1),
//公式
FORMULA(2),
BLANK(3),
//布尔
BOOLEAN(4),
ERROR(5);

1.2 样式和字体

创建样式:

CellStyle cellStyle = workbook.createCellStyle();

1)左右垂直居中

//左右居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 设置垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

2)字体加粗、颜色

创建加粗样式并设置到CellStyle 中:

Font font = workbook.createFont();
//字体颜色为红色
font.setColor(IndexedColors.RED.getIndex());
//字体加粗
font.setBold(true);
cellStyle.setFont(font);


指定Cell单元格使用该样式:

cell.setCellStyle(style);

3)调整列宽和高

Sheet sheet = workbook.createSheet();
//自动调整列的宽度来适应内容
sheet.autoSizeColumn(int column); 
// 设置列的宽度
sheet.setColumnWidth(220 * 256);   
autoSizeColumn()
setColumnWidth()
宽度 = 字符个数 * 256
20 * 256

4)倾斜、下划线

Font font = workbook.createFont();
font.setItalic(boolean italic); 设置倾斜
font.setUnderline(byte underline); 设置下划线

1.3 进阶用法

1)合并单元格

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet(fileName);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));

CellRangeAddress()方法四个参数分别是fristRow:起始行、lastRow:结束行、fristCol:起始列、lastCol:结束列。

如果你想合并从第一行到第二行从一列到第十列的单元格(一共合并20格),那么就是CellRangeAddress(0,1,0,10)

2)字段必填

//创建数据验证
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
//创建要添加校验的单元格对象
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 10);
//创建必填校验规则
DataValidationConstraint constraint = dvHelper.createCustomConstraint("NOT(ISBLANK(A1))");
//设置校验
DataValidation validation = dvHelper.createValidation(constraint, addressList);
//校验不通过 提示
validation.setShowErrorBox(true);
sheet.addValidationData(validation);

CellRangeAddressList()方法传递四个参数,分别是:fristRow:起始行、lastRow:结束行、fristCol:起始列、lastCol:结束列。CellRangeAddressList(0, 0, 0, 10)表示的就是给第一行从第一列开始到第十列一共十个单元格添加数据校验。

3)添加公式

SUM:求和函数

//创建SUM公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell sumCell = row.createCell(0);
sumCell.setCellFormula("SUM(A1:A10)");
//计算SUM公式结果
Cell sumResultCell = row.createCell(1);
sumResultCell.setCellValue(evaluator.evaluate(sumCell).getNumberValue());

AVERAGE:平均数函数

//创建AVERAGE公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell averageCell = row.createCell(0);
averageCell.setCellFormula("AVERAGE(A1:A10)");

//计算AVERAGE公式结果
Cell averageResultCell = row.createCell(1);
averageResultCell.setCellValue(evaluator.evaluate(averageCell).getNumberValue());

COUNT:计数函数

//创建COUNT公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell countCell = row.createCell(0);
countCell.setCellFormula("COUNT(A1:A10)");

//计算COUNT公式结果
Cell countResultCell = row.createCell(1);
countResultCell.setCellValue(evaluator.evaluate(countCell).getNumberValue());

IF:条件函数

//创建IF公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell ifCell = row.createCell(0);
ifCell.setCellFormula("IF(A1>B1,\"Yes\",\"No\")");

//计算IF公式结果
Cell ifResultCell = row.createCell(1);
ifResultCell.setCellValue(evaluator.evaluate(ifCell).getStringValue());

CONCATENATE:连接函数

//创建CONCATENATE公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell concatenateCell = row.createCell(0);
concatenateCell.setCellFormula("CONCATENATE(A1,\" \",B1)");

//计算CONCATENATE公式结果
Cell concatenateResultCell = row.createCell(1);
concatenateResultCell.setCellValue(evaluator.evaluate(concatenateCell).getStringValue());

4)下拉选择

//下拉值
private List<String> grade = Arrays.asList("高", "中", "低");
(此处省略n行代码)
Sheet sheet = workbook.createSheet("sheet");
DataValidation dataValidation = this.addPullDownConstraint(i, sheet, grade );
sheet.addValidationData(dataValidation);

5)设置单元格的数据类型

数字格式

// 设置单元格样式 - 数字格式
CellStyle numberCellStyle = workbook.createCellStyle();
numberCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0.00"));
//指定单元格
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(numberCellStyle);

日期格式

// 设置单元格样式 - 日期格式
CellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
//指定单元格
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(dateCellStyle);
 
 
posted @   KLAPT  阅读(247)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示