直接看工具类代码吧,

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的更多相关文章

  1. C++读写EXCEL文件OLE,java读写excel文件POI 对比

    C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...

  2. java读写excel文件( POI解析Excel)

    package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...

  3. 利用java读写Excel文件

    一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...

  4. java读写excel文件

    近期处理的数据规模比较大,正好又是统计合并的事情,想着借助excel就可以完成了,然后就了解了下java读取excel的事情. 读取的文件主要分两类:xls文件.xlsx文件.xls文件的相关操作用的 ...

  5. Apache POI 读写 Excel 文件

    目录 写入 Excel 文件 读取 Excel 文件 遍历 Excel 文件 需要的 maven 依赖 完整代码 写入 Excel 文件 // 写入 Excel 文件 // ============= ...

  6. 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)

    1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...

  7. 《手把手教你》系列技巧篇(六十七)-java+ selenium自动化测试 - 读写excel文件 - 中篇(详细教程)

    1.简介 前面介绍了POI可以操作excel,也简单的提到另一个操作excle的工具,本篇介绍一个其他的可以操作excel的工具,但是这个工具有一个前提,excel文件版本只能是97-2003版本,如 ...

  8. 《手把手教你》系列技巧篇(六十八)-java+ selenium自动化测试 - 读写excel文件 - 下篇(详细教程)

    1.简介 今天继续操作Excle,小伙伴或者童鞋们是不是觉得宏哥会介绍第三种工具操作Excle,今天不介绍了,有两种就够用了,其实一种就够用了,今天主要是来介绍如何使用不同的数据类型读取Excel文件 ...

  9. java写入excel文件poi

    java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...

随机推荐

  1. PlayJava Day028

    1.网络程序中套接字(Socket)用于将应用程序与端口连接起来 套接字是一个假想的连接装置,就像插插头的设备”插座“,用于连接电器与电线,如下所示 客户端:应用程序 <----> Soc ...

  2. right join 和left join 的区别

    SQL 数据库 right join 和left join 的区别   left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中 ...

  3. [Go] 解决golang.org模块无法下载的问题

    使用GOPROXY环境变量解决proxy.golang.org无法访问问题 在/etc/profile中增加 export GOPROXY=https://goproxy.cn windows下使用 ...

  4. Windows性能监控监视器(perfmon使用)

    一.在命令窗口或运行中执行perfmon.exe,打开性能监视器 二.在用户定义中,即可新建--数据收集器--性能计数器,步骤如下: 三.添加监控Windows服务器的资源类型,例如:内存(Avail ...

  5. 菜鸟刷面试题(一、Java基础篇)

    目录: JDK 和 JRE 有什么区别? == 和 equals 的区别是什么? 两个对象的 hashCode()相同,则 equals()也一定为 true,对吗? final 在 java 中有什 ...

  6. 第一篇:C++之hello world

    1.编辑器:Microsoft Visual C++ 2010,下载安装 2.新建项目 代码: #include <iostream>#include <Windows.h>/ ...

  7. celery配置

    celery配置 celery的官方文档其实相对还是写的很不错的.但是在一些深层次的使用上面却显得杂乱甚至就没有某些方面的介绍, 通过我的一个测试环境的settings.py来说明一些使用celery ...

  8. SpringBoot运行异常时捕获

    一.目录展示 二.FirstController 三.ExceptionHandler 捕获异常类 四.效果展示

  9. IT兄弟连 HTML5教程 CSS3揭秘 CSS常见的样式属性和值2

    3  背景属性 大多数HTML元素都允许控制背景,包括背景颜色.背景图像.背景重复.背景附件.背景位置等属性.常见的控制背景属性.值及描述如表2所示. 表2  CSS中常见的控制背景的属性 除了使用表 ...

  10. Appium+Java 自动化测试系列二:Maven+Testng

    新建Maven项目作为测试项目分为3个步骤: 1.Eclipse安装Testng 插件 2.新建Maven项目 3.引入Testng 一.Eclipse安装Testng插件 TestNG安装可选择在线 ...