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 ...
随机推荐
- Jerome: Vulnhub Walkthrough
nmap 扫描探测: ╰─ nmap -p1-65535 -sV -A -O -sT 10.10.202.135Starting Nmap 7.70 ( https://nmap.org ) at 2 ...
- KVO-键值监听
键值监听,就是可以监听对象某个属性值的变化: 首先,在工程中,新建一个Person的类 @interface Person : NSObject @property (nonatomic, copy) ...
- golang.org 安装脚本
#!/usr/bin/env bash cd $GOPATH; #创建 $GOPATH/src/golang.org/x 目录 mkdir -p $GOPATH/src/golang.org/x; e ...
- MySQL删除大表时潜在的问题(drop table,truncate table)
来源于:https://www.cnblogs.com/CtripDBA/p/11465315.html,侵删,纯截图,避免吸引流量之嫌 case1,删除大表时,因为清理自适应hash索引占用的内容导 ...
- Where is the kernel documentation?; Ubuntu 上如何安装 linux 内核文档;fedora 上如何安装linux内核文档?
有时候,linux内核文档对我们很重要,我们可以在linux系统中安装,并及时查看: 参考链接:https://askubuntu.com/questions/841043/where-is-the- ...
- PHP捕获异常register_shutdown_function和error_get_last的使用
register_shutdown_function 注册一个会在php中止时执行的函数,注册一个 callback ,它会在脚本执行完成或者 exit() 后被调用. error_get_last ...
- C语言程序设计100例之(24):数制转换
例24 数制转换 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入格式 共三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用 ...
- redis的embstr编码
问题来了 今天在看书籍<Redis设计与实现>的时候,在8.2字符串对象里面写到 如果字符串对象保存的是一个字符串值, 并且这个字符串值的长度大于 39 字节, 那么字符串对象将使用一个简 ...
- Python 接口自动化常用方法封装
#!/usr/bin/env python # -*- coding:utf-8 -*- # ************************************* # @Time : 2019/ ...
- Java题库——Chapter5 方法
1)Suppose your method does not return any value, which of the following keywords can be used as a re ...