直接看工具类代码吧,

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. About learn《The C programming Language,Second Edition》

    Today,My last week buy C language book arrived. Today,I week earnest study. No matter what difficult ...

  2. 【iOS13问题】修改状态栏的颜色(亲测,有效)

    - (UIStatusBarStyle)preferredStatusBarStyle { if (@available(iOS 13.0, *)) { return UIStatusBarStyle ...

  3. Android Studio的安装及第一次启动时的配置

    Android Studio的安装及第一次启动时的配置 一.下载Android Studio 百度搜索“Android Studio" 点击中文社区进入,选择最新版本下载. 下载后双击安装包 ...

  4. 关于强制IE不使用兼容模式渲染网页

    现在IE11是唯一受微软支持的IE浏览器. IE11有兼容模式,开启后有网页会出错. 在html header标签下加上 <meta http-equiv="X-UA-Compatib ...

  5. 关于web.xml配置的那些事儿

    参考文章:重新认识web.xml

  6. 深入浅出xpath轴定位

    在web自动化里面经常要用到定位,常用的八种定位方式中我最喜欢xpath定位,功能很强大.结合它里面的文本定位.模糊定位.逻辑定位等,基本能搞定所有的元素定位问题. 今天要讨论的是xpath的另一种比 ...

  7. centos安装jdk10

    下载一个jdk10文件到linux : wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=ac ...

  8. 21-django-pure-pagination分页

    一.django-pure-pagination分页 github上的描述(django-pure-pagination provides advanced pagination features a ...

  9. Python 从入门到进阶之路(七)

    之前的文章我们简单介绍了一下 Python 中异常处理,本篇文章我们来看一下 Python 中 is 和 == 的区别及深拷贝和浅拷贝. 我们先来看一下在 Python 中的双等号 == . == 是 ...

  10. Java描述设计模式(02):简单工厂模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景简介 1.引入场景 订餐流程简单描述 1).食品抽象类,规定食品的基础属性操作 2).鱼类,鸡肉类食品类扩展 3).订餐流程类,根 ...