Java读写Excel文件,利用POI
直接看工具类代码吧,
package com.example.demo.util; import com.example.demo.entity.ExcelDataVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; /**
* Excel文档工具类
*/
public class ExcelReader {
//文件写入需要注意
/**
* 把数据写入到Excel文件
* @param fileName 自动生成的Excel文件的全路径文件名称
* @param excelDataVo 要写入到Excel文件中的数据
*/
public static void writeExcel(String fileName, List<ExcelDataVO> excelDataVo) throws IOException {
Workbook workbook = null;
Sheet sheet = null;
Row row = null;
Cell cell = null; //创建Excel文件
File excelFile = new File(fileName.trim()); //创建Excel工作薄
if (excelFile.getName().endsWith("xlsx")) {
workbook = new XSSFWorkbook();
} else {
workbook = new HSSFWorkbook();
} //创建Excel表单
sheet = workbook.createSheet(); //设置列宽,宽度为256的整数倍
sheet.setColumnWidth(1, 5120);
sheet.setColumnWidth(2, 3840);
sheet.setColumnWidth(3, 2560);
sheet.setColumnWidth(4, 2560);
sheet.setColumnWidth(5, 5120); //设置默认行高(默认为300)
sheet.setDefaultRowHeight((short) 512); //设置合并单元格
CellRangeAddress titleCellAddresses = new CellRangeAddress(1,2,1,5);
sheet.addMergedRegion(titleCellAddresses); //创建标题行
row = sheet.createRow(1);
cell = row.createCell(1, CellType.STRING);
cell.setCellStyle(getTitleCellStyle(workbook));
cell.setCellValue("User信息表格"); //设置合并单元格的边框,这个需要放在创建标题行之后
setRegionBorderStyle(BorderStyle.THIN, titleCellAddresses, sheet); //创建表头行
row = sheet.createRow(3);
cell = row.createCell(1, CellType.STRING);
cell.setCellStyle(getHeaderCellStyle(workbook));
cell.setCellValue("ID");
cell = row.createCell(2, CellType.STRING);
cell.setCellStyle(getHeaderCellStyle(workbook));
cell.setCellValue("姓名");
cell = row.createCell(3, CellType.STRING);
cell.setCellStyle(getHeaderCellStyle(workbook));
cell.setCellValue("性别");
cell = row.createCell(4, CellType.STRING);
cell.setCellStyle(getHeaderCellStyle(workbook));
cell.setCellValue("年龄");
cell = row.createCell(5, CellType.STRING);
cell.setCellStyle(getHeaderCellStyle(workbook));
cell.setCellValue("生日"); //创建表体行
for(int i = 0; i < excelDataVo.size(); i++) {
row = sheet.createRow(i + 4);
cell = row.createCell(1, CellType.NUMERIC);
cell.setCellStyle(getBodyCellStyle(workbook));
cell.setCellValue(excelDataVo.get(i).getItemId());
cell = row.createCell(2, CellType.STRING);
cell.setCellStyle(getBodyCellStyle(workbook));
cell.setCellValue(excelDataVo.get(i).getItemName());
cell = row.createCell(3, CellType.STRING);
cell.setCellStyle(getBodyCellStyle(workbook));
cell.setCellValue(excelDataVo.get(i).getStorename());
} //把Excel工作薄写入到Excel文件
FileOutputStream os = new FileOutputStream(excelFile);
workbook.write(os);
os.flush();
os.close();
} /**
* 从Excel文件读取数据
* @param fileName 要读取的Excel文件的全路径文件名称
* @return 从Excel文件中批量导入的用户数据
*/
public static String[] readExcel(String fileName) throws IOException {
Workbook workbook = null;
Sheet sheet = null;
Row row = null; //读取Excel文件
File excelFile = new File(fileName.trim());
InputStream is = new FileInputStream(excelFile); //获取Excel工作薄
if (excelFile.getName().endsWith("xlsx")) {
workbook = new XSSFWorkbook(is);
} else {
workbook = new HSSFWorkbook(is);
}
if (workbook == null) {
System.err.println("Excel文件有问题,请检查!");
return null;
}
String[] lines = new String[1000000];
//获取Excel表单
sheet = workbook.getSheetAt(0);
int rowNum = 1;
for(; rowNum <= sheet.getLastRowNum(); rowNum++) {
//获取一行
row = sheet.getRow(rowNum);
ExcelDataVO edv = new ExcelDataVO();
System.out.println("获取一行"+row.getCell(0));
System.out.println("获取一行"+row.getCell(1));
System.out.println("获取一行"+row.getCell(2));
lines[rowNum]+=row.getCell(0)+"#"+row.getCell(1)+"#"+row.getCell(2);
if(lines[rowNum]==null && lines[rowNum].equals("")){
break;
}
}
String[] lines2 = new String[rowNum];
for(int i=0;i<rowNum;i++){
lines2[i] = lines[i];
System.out.println(lines2[i]);
}
return lines2;
} /**
* 设置合并单元格的边框
* @param style 要设置的边框的样式
* @param cellAddresses 要设置的合并的单元格
* @param sheet 要设置的合并的单元格所在的表单
*/
private static void setRegionBorderStyle(BorderStyle style, CellRangeAddress cellAddresses, Sheet sheet) {
RegionUtil.setBorderTop(style, cellAddresses, sheet);
RegionUtil.setBorderBottom(style, cellAddresses, sheet);
RegionUtil.setBorderLeft(style, cellAddresses, sheet);
RegionUtil.setBorderRight(style, cellAddresses, sheet);
} /**
* 设置普通单元格的边框
* @param style 要设置的边框的样式
* @param cellStyle 单元格样式对象
*/
private static void setCellBorderStyle(BorderStyle style, CellStyle cellStyle) {
cellStyle.setBorderTop(style);
cellStyle.setBorderBottom(style);
cellStyle.setBorderLeft(style);
cellStyle.setBorderRight(style);
} /**
* 设置标题单元格样式
* @param workbook 工作薄对象
* @return 单元格样式对象
*/
private static CellStyle getTitleCellStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle(); //设置字体
Font font = workbook.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 24);
font.setColor((short) 10);
cellStyle.setFont(font); //设置文字居中显示
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); return cellStyle;
} /**
* 设置表头单元格样式
* @param workbook 工作薄对象
* @return 单元格样式对象
*/
private static CellStyle getHeaderCellStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle(); //设置字体
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 20);
font.setBold(true);
cellStyle.setFont(font); //设置文字居中显示
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //设置单元格的边框
setCellBorderStyle(BorderStyle.THIN, cellStyle); return cellStyle;
} /**
* 设置表体单元格样式
* @param workbook 工作薄对象
* @return 单元格样式对象
*/
private static CellStyle getBodyCellStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle(); //设置字体
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
cellStyle.setFont(font); //设置文字居中显示
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //设置单元格的边框
setCellBorderStyle(BorderStyle.THIN, cellStyle); return cellStyle;
} /**
* 获取单元格的值的字符串
* @param cell 单元格对象
* @return cell单元格的值的字符串
*/
private static String getStringValue(Cell cell) {
if (cell == null) {
return null;
}
CellType cellType = cell.getCellType();
switch (cellType) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
double value = cell.getNumericCellValue();
return String.valueOf(Math.round(value));
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
default:
return null;
}
} }
Java读写Excel文件,利用POI的更多相关文章
- C++读写EXCEL文件OLE,java读写excel文件POI 对比
C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...
- java读写excel文件( POI解析Excel)
package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...
- 利用java读写Excel文件
一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...
- java读写excel文件
近期处理的数据规模比较大,正好又是统计合并的事情,想着借助excel就可以完成了,然后就了解了下java读取excel的事情. 读取的文件主要分两类:xls文件.xlsx文件.xls文件的相关操作用的 ...
- Apache POI 读写 Excel 文件
目录 写入 Excel 文件 读取 Excel 文件 遍历 Excel 文件 需要的 maven 依赖 完整代码 写入 Excel 文件 // 写入 Excel 文件 // ============= ...
- 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)
1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...
- 《手把手教你》系列技巧篇(六十七)-java+ selenium自动化测试 - 读写excel文件 - 中篇(详细教程)
1.简介 前面介绍了POI可以操作excel,也简单的提到另一个操作excle的工具,本篇介绍一个其他的可以操作excel的工具,但是这个工具有一个前提,excel文件版本只能是97-2003版本,如 ...
- 《手把手教你》系列技巧篇(六十八)-java+ selenium自动化测试 - 读写excel文件 - 下篇(详细教程)
1.简介 今天继续操作Excle,小伙伴或者童鞋们是不是觉得宏哥会介绍第三种工具操作Excle,今天不介绍了,有两种就够用了,其实一种就够用了,今天主要是来介绍如何使用不同的数据类型读取Excel文件 ...
- java写入excel文件poi
java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...
随机推荐
- DispatchProxy实现动态代理及AOP
DispatchProxy类是DotnetCore下的动态代理的类,源码地址:Github,官方文档:MSDN.主要是Activator以及AssemblyBuilder来实现的(请看源码分析),园子 ...
- spring单元测试下模拟rabbitmq
gradle添加引用 compile 'org.springframework.boot:spring-boot-starter-amqp' testCompile 'com.github.fridu ...
- java后台树形结构展示---懒加载
一.数据库设计 二.实体类:entity import com.joyoung.cloud.security.common.validatedGroup.Add;import com.joyoung. ...
- LeetCode刷题191203 --回溯算法
虽然不是每天都刷,但还是不想改标题,(手动狗头 题目及解法来自于力扣(LeetCode),传送门. 算法(78): 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: ...
- SQL Server查看login所授予的具体权限
在SQL Server数据库中如何查看一个登录名(login)的具体权限呢,如果使用SSMS的UI界面查看登录名的具体权限的话,用户数据库非常多的话,要梳理完它所有的权限,操作又耗时又麻烦,个人十分崇 ...
- oracle 查询两个字段值相同的记录
select A.* from tb_mend_enrol A, (select A.Typeid, A.address from tb_mend_enrol A group by A.Typeid, ...
- nginx 校验及重启
#查询nginx所在路径 [centos find 查询文件](https://www.cnblogs.com/codeWorldCodeHeart/p/12049262.html) #校验如下 /u ...
- Linux_更改时区和利用Crontab同步时间
一.更改时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 二.Crontab时间同步 crontab -e #crontab编辑 */5 ...
- 第2次作业-titanic数据集练习
一.读入titanic.xlsx文件,按照教材示例步骤,完成数据清洗. titanic数据集包含11个特征,分别是: Survived:0代表死亡,1代表存活Pclass:乘客所持票类,有三种值(1, ...
- Rsync常见问题汇总
rsync服务端开启的iptables防火墙 客户端的错误现象 No route to host 错误演示过程 [root@nfs01 tmp]# rsync -avz /etc/hosts rsy ...