Java POI 操作Excel(读取/写入)
pom.xml依赖:
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- <version>3.17</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-scratchpad</artifactId>
- <version>3.17</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>3.17</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml-schemas</artifactId>
- <version>3.17</version>
- </dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>23.0</version>
- </dependency>
Java类ExcelUtils代码:
- package com.dzpykj.files.excel;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.poi.EncryptedDocumentException;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
- 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.FillPatternType;
- import org.apache.poi.ss.usermodel.Font;
- import org.apache.poi.ss.usermodel.HorizontalAlignment;
- import org.apache.poi.ss.usermodel.IndexedColors;
- 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.usermodel.WorkbookFactory;
- import org.apache.poi.xssf.usermodel.XSSFCellStyle;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import com.google.common.collect.Maps;
- /**
- * Excel操作工具类
- * @author ChaiXY
- */
- public class ExcelUtils {
- // @Value("${file_base_path}")
- // private static String fileBasePath;//文件的基础路径
- // private static String fileBasePath = System.getProperty("user.dir") + File.separator + "excel" + File.separator;;//文件的基础路径
- public static final String OFFICE_EXCEL_XLS = "xls";
- public static final String OFFICE_EXCEL_XLSX = "xlsx";
- /**
- * 读取指定Sheet也的内容
- * @param filepath filepath 文件全路径
- * @param sheetNo sheet序号,从0开始,如果读取全文sheetNo设置null
- */
- public static String readExcel(String filepath, Integer sheetNo)
- throws EncryptedDocumentException, InvalidFormatException, IOException {
- StringBuilder sb = new StringBuilder();
- Workbook workbook = getWorkbook(filepath);
- if (workbook != null) {
- if (sheetNo == null) {
- int numberOfSheets = workbook.getNumberOfSheets();
- for (int i = 0; i < numberOfSheets; i++) {
- Sheet sheet = workbook.getSheetAt(i);
- if (sheet == null) {
- continue;
- }
- sb.append(readExcelSheet(sheet));
- }
- } else {
- Sheet sheet = workbook.getSheetAt(sheetNo);
- if (sheet != null) {
- sb.append(readExcelSheet(sheet));
- }
- }
- }
- return sb.toString();
- }
- /**
- * 根据文件路径获取Workbook对象
- * @param filepath 文件全路径
- */
- public static Workbook getWorkbook(String filepath)
- throws EncryptedDocumentException, InvalidFormatException, IOException {
- InputStream is = null;
- Workbook wb = null;
- if (StringUtils.isBlank(filepath)) {
- throw new IllegalArgumentException("文件路径不能为空");
- } else {
- String suffiex = getSuffiex(filepath);
- if (StringUtils.isBlank(suffiex)) {
- throw new IllegalArgumentException("文件后缀不能为空");
- }
- if (OFFICE_EXCEL_XLS.equals(suffiex) || OFFICE_EXCEL_XLSX.equals(suffiex)) {
- try {
- is = new FileInputStream(filepath);
- wb = WorkbookFactory.create(is);
- } finally {
- if (is != null) {
- is.close();
- }
- if (wb != null) {
- wb.close();
- }
- }
- } else {
- throw new IllegalArgumentException("该文件非Excel文件");
- }
- }
- return wb;
- }
- /**
- * 获取后缀
- * @param filepath filepath 文件全路径
- */
- private static String getSuffiex(String filepath) {
- if (StringUtils.isBlank(filepath)) {
- return "";
- }
- int index = filepath.lastIndexOf(".");
- if (index == -1) {
- return "";
- }
- return filepath.substring(index + 1, filepath.length());
- }
- private static String readExcelSheet(Sheet sheet) {
- StringBuilder sb = new StringBuilder();
- if(sheet != null){
- int rowNos = sheet.getLastRowNum();// 得到excel的总记录条数
- for (int i = 0; i <= rowNos; i++) {// 遍历行
- Row row = sheet.getRow(i);
- if(row != null){
- int columNos = row.getLastCellNum();// 表头总共的列数
- for (int j = 0; j < columNos; j++) {
- Cell cell = row.getCell(j);
- if(cell != null){
- cell.setCellType(CellType.STRING);
- sb.append(cell.getStringCellValue() + " ");
- // System.out.print(cell.getStringCellValue() + " ");
- }
- }
- // System.out.println();
- }
- }
- }
- return sb.toString();
- }
- /**
- * 读取指定Sheet页的表头
- * @param filepath filepath 文件全路径
- * @param sheetNo sheet序号,从0开始,必填
- */
- public static Row readTitle(String filepath, int sheetNo)
- throws IOException, EncryptedDocumentException, InvalidFormatException {
- Row returnRow = null;
- Workbook workbook = getWorkbook(filepath);
- if (workbook != null) {
- Sheet sheet = workbook.getSheetAt(sheetNo);
- returnRow = readTitle(sheet);
- }
- return returnRow;
- }
- /**
- * 读取指定Sheet页的表头
- */
- public static Row readTitle(Sheet sheet) throws IOException {
- Row returnRow = null;
- int totalRow = sheet.getLastRowNum();// 得到excel的总记录条数
- for (int i = 0; i < totalRow; i++) {// 遍历行
- Row row = sheet.getRow(i);
- if (row == null) {
- continue;
- }
- returnRow = sheet.getRow(0);
- break;
- }
- return returnRow;
- }
- /**
- * 创建Excel文件
- * @param filepath filepath 文件全路径
- * @param sheetName 新Sheet页的名字
- * @param titles 表头
- * @param values 每行的单元格
- */
- public static boolean writeExcel(String filepath, String sheetName, List<String> titles,
- List<Map<String, Object>> values) throws IOException {
- boolean success = false;
- OutputStream outputStream = null;
- if (StringUtils.isBlank(filepath)) {
- throw new IllegalArgumentException("文件路径不能为空");
- } else {
- String suffiex = getSuffiex(filepath);
- if (StringUtils.isBlank(suffiex)) {
- throw new IllegalArgumentException("文件后缀不能为空");
- }
- Workbook workbook;
- if ("xls".equals(suffiex.toLowerCase())) {
- workbook = new HSSFWorkbook();
- } else {
- workbook = new XSSFWorkbook();
- }
- // 生成一个表格
- Sheet sheet;
- if (StringUtils.isBlank(sheetName)) {
- // name 为空则使用默认值
- sheet = workbook.createSheet();
- } else {
- sheet = workbook.createSheet(sheetName);
- }
- // 设置表格默认列宽度为15个字节
- sheet.setDefaultColumnWidth((short) 15);
- // 生成样式
- Map<String, CellStyle> styles = createStyles(workbook);
- // 创建标题行
- Row row = sheet.createRow(0);
- // 存储标题在Excel文件中的序号
- Map<String, Integer> titleOrder = Maps.newHashMap();
- for (int i = 0; i < titles.size(); i++) {
- Cell cell = row.createCell(i);
- cell.setCellStyle(styles.get("header"));
- String title = titles.get(i);
- cell.setCellValue(title);
- titleOrder.put(title, i);
- }
- // 写入正文
- Iterator<Map<String, Object>> iterator = values.iterator();
- // 行号
- int index = 1;
- while (iterator.hasNext()) {
- row = sheet.createRow(index);
- Map<String, Object> value = iterator.next();
- for (Map.Entry<String, Object> map : value.entrySet()) {
- // 获取列名
- String title = map.getKey();
- // 根据列名获取序号
- int i = titleOrder.get(title);
- // 在指定序号处创建cell
- Cell cell = row.createCell(i);
- // 设置cell的样式
- if (index % 2 == 1) {
- cell.setCellStyle(styles.get("cellA"));
- } else {
- cell.setCellStyle(styles.get("cellB"));
- }
- // 获取列的值
- Object object = map.getValue();
- // 判断object的类型
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- if (object instanceof Double) {
- cell.setCellValue((Double) object);
- } else if (object instanceof Date) {
- String time = simpleDateFormat.format((Date) object);
- cell.setCellValue(time);
- } else if (object instanceof Calendar) {
- Calendar calendar = (Calendar) object;
- String time = simpleDateFormat.format(calendar.getTime());
- cell.setCellValue(time);
- } else if (object instanceof Boolean) {
- cell.setCellValue((Boolean) object);
- } else {
- if (object != null) {
- cell.setCellValue(object.toString());
- }
- }
- }
- index++;
- }
- try {
- outputStream = new FileOutputStream(filepath);
- workbook.write(outputStream);
- success = true;
- } finally {
- if (outputStream != null) {
- outputStream.close();
- }
- if (workbook != null) {
- workbook.close();
- }
- }
- return success;
- }
- }
- /**
- * 设置格式
- */
- private static Map<String, CellStyle> createStyles(Workbook wb) {
- Map<String, CellStyle> styles = Maps.newHashMap();
- // 标题样式
- XSSFCellStyle titleStyle = (XSSFCellStyle) wb.createCellStyle();
- titleStyle.setAlignment(HorizontalAlignment.CENTER); // 水平对齐
- titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直对齐
- titleStyle.setLocked(true); // 样式锁定
- titleStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
- Font titleFont = wb.createFont();
- titleFont.setFontHeightInPoints((short) 16);
- titleFont.setBold(true);
- titleFont.setFontName("微软雅黑");
- titleStyle.setFont(titleFont);
- styles.put("title", titleStyle);
- // 文件头样式
- XSSFCellStyle headerStyle = (XSSFCellStyle) wb.createCellStyle();
- headerStyle.setAlignment(HorizontalAlignment.CENTER);
- headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
- headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); // 前景色
- headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 颜色填充方式
- headerStyle.setWrapText(true);
- headerStyle.setBorderRight(BorderStyle.THIN); // 设置边界
- headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
- headerStyle.setBorderLeft(BorderStyle.THIN);
- headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
- headerStyle.setBorderTop(BorderStyle.THIN);
- headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
- headerStyle.setBorderBottom(BorderStyle.THIN);
- headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
- Font headerFont = wb.createFont();
- headerFont.setFontHeightInPoints((short) 12);
- headerFont.setColor(IndexedColors.WHITE.getIndex());
- titleFont.setFontName("微软雅黑");
- headerStyle.setFont(headerFont);
- styles.put("header", headerStyle);
- Font cellStyleFont = wb.createFont();
- cellStyleFont.setFontHeightInPoints((short) 12);
- cellStyleFont.setColor(IndexedColors.BLUE_GREY.getIndex());
- cellStyleFont.setFontName("微软雅黑");
- // 正文样式A
- XSSFCellStyle cellStyleA = (XSSFCellStyle) wb.createCellStyle();
- cellStyleA.setAlignment(HorizontalAlignment.CENTER); // 居中设置
- cellStyleA.setVerticalAlignment(VerticalAlignment.CENTER);
- cellStyleA.setWrapText(true);
- cellStyleA.setBorderRight(BorderStyle.THIN);
- cellStyleA.setRightBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleA.setBorderLeft(BorderStyle.THIN);
- cellStyleA.setLeftBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleA.setBorderTop(BorderStyle.THIN);
- cellStyleA.setTopBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleA.setBorderBottom(BorderStyle.THIN);
- cellStyleA.setBottomBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleA.setFont(cellStyleFont);
- styles.put("cellA", cellStyleA);
- // 正文样式B:添加前景色为浅黄色
- XSSFCellStyle cellStyleB = (XSSFCellStyle) wb.createCellStyle();
- cellStyleB.setAlignment(HorizontalAlignment.CENTER);
- cellStyleB.setVerticalAlignment(VerticalAlignment.CENTER);
- cellStyleB.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
- cellStyleB.setFillPattern(FillPatternType.SOLID_FOREGROUND);
- cellStyleB.setWrapText(true);
- cellStyleB.setBorderRight(BorderStyle.THIN);
- cellStyleB.setRightBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleB.setBorderLeft(BorderStyle.THIN);
- cellStyleB.setLeftBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleB.setBorderTop(BorderStyle.THIN);
- cellStyleB.setTopBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleB.setBorderBottom(BorderStyle.THIN);
- cellStyleB.setBottomBorderColor(IndexedColors.BLACK.getIndex());
- cellStyleB.setFont(cellStyleFont);
- styles.put("cellB", cellStyleB);
- return styles;
- }
- /**
- * 将源文件的内容复制到新Excel文件(可供理解Excel使用,使用价值不大)
- * @param srcFilepath 源文件全路径
- * @param desFilepath 目标文件全路径
- */
- public static void writeExcel(String srcFilepath, String desFilepath)
- throws IOException, EncryptedDocumentException, InvalidFormatException {
- FileOutputStream outputStream = null;
- String suffiex = getSuffiex(desFilepath);
- if (StringUtils.isBlank(suffiex)) {
- throw new IllegalArgumentException("文件后缀不能为空");
- }
- Workbook workbook_des;
- if ("xls".equals(suffiex.toLowerCase())) {
- workbook_des = new HSSFWorkbook();
- } else {
- workbook_des = new XSSFWorkbook();
- }
- Workbook workbook = getWorkbook(srcFilepath);
- if (workbook != null) {
- int numberOfSheets = workbook.getNumberOfSheets();
- for (int k = 0; k < numberOfSheets; k++) {
- Sheet sheet = workbook.getSheetAt(k);
- Sheet sheet_des = workbook_des.createSheet(sheet.getSheetName());
- if (sheet != null) {
- int rowNos = sheet.getLastRowNum();
- for (int i = 0; i <= rowNos; i++) {
- Row row = sheet.getRow(i);
- Row row_des = sheet_des.createRow(i);
- if(row != null){
- int columNos = row.getLastCellNum();
- for (int j = 0; j < columNos; j++) {
- Cell cell = row.getCell(j);
- Cell cell_des = row_des.createCell(j);
- if(cell != null){
- cell.setCellType(CellType.STRING);
- cell_des.setCellType(CellType.STRING);
- cell_des.setCellValue(cell.getStringCellValue());
- }
- }
- }
- }
- }
- }
- }
- try {
- outputStream = new FileOutputStream(desFilepath);
- workbook_des.write(outputStream);
- } finally {
- if (outputStream != null) {
- outputStream.close();
- }
- if (workbook != null) {
- workbook_des.close();
- }
- }
- }
- public static void main(String[] args) {
- }
- }
Java POI 操作Excel(读取/写入)的更多相关文章
- java poi操作excel 添加 锁定单元格保护
Excel的book保护是很常用的,主要是不想让别人修改Excel的时候用.这样能够避免恶意随便修改数据,提高数据的可信度. 下面介绍JAVA POI来实现设置book保护: 使用HSSFSheet类 ...
- java poi操作excel示例代码
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- java poi 操作
Java POI 操作Excel(读取/写入) https://www.cnblogs.com/dzpykj/p/8417738.html Java操作Excel之Poi基本操作 https://my ...
- JAVA的POI操作Excel
1.1Excel简介 一个excel文件就是一个工作簿workbook,一个工作簿中可以创建多张工作表sheet,而一个工作表中包含多个单元格Cell,这些单元格都是由列(Column)行(Row)组 ...
- java使用POI操作excel文件,实现批量导出,和导入
一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...
- java里poi操作excel的工具类(兼容各版本)
转: java里poi操作excel的工具类(兼容各版本) 下面是文件内具体内容,文件下载: import java.io.FileNotFoundException; import java.io. ...
- 使用POI操作Excel时对事先写入模板的公式强制执行
场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式则不自动被调用,必须手动双击该Cell才能生 ...
- java 使用jxl poi 操作excel
java操作excel 创建.修改 xls 文件 JAVA操作Excel文件 Java生成和操作Excel文件 java导出Excel通用方法 Java 实现导出excel表 POI Java PO ...
- Java POI操作Excel注意点
excel的行索引和列索引都是从0开始,而行号和列号都是从1开始 POI·操作excel基本上都是使用索引 XSSFRow对象的 row.getLastCellNum() 方法返回的是当前行最后有效列 ...
随机推荐
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A
Description There are literally dozens of snooker competitions held each year, and team Jinotega tri ...
- jQuery val()方法及valHooks源码解读
val: function( value ) { var hooks, ret, isFunction, elem = this[0]; if ( !arguments.length ) {//无参数 ...
- 如何在spring环境中做单元测试
在测试类的上方加入以下注解 @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:spring.xm ...
- mongodb 上限集合
上限集合是固定大小的循环集合按照插入以支持高性能的创建,读取和删除操作.通过循环,这意味着,当分配给该集合中的固定大小要用尽时,它会开始删除集合中最旧的文件而不提供任何明确的命令. 上限集合限制更新, ...
- vue2.0:(二)、mock数据
什么是mock数据呢?很多情况下,后台的搭建比起前端来说要麻烦的多,所以,常常是前端写好了页面以后后台接口却没有写好,但是在一个项目中,接口调试确实是最浪费时间的,所以,往往前端需要自己模拟数据. 第 ...
- 事件冒泡之cancelBubble和stoppropagation的区别
事实上stoppropagation和cancelBubble的作用是一样的,都是用来阻止浏览器默认的事件冒泡行为. 不同之处在于stoppropagation属于W3C标准,试用于Firefox等浏 ...
- mybatis实现使用原生的sql
1.相应的xml文件中 <select id="selectByCategories" resultType="map" parameterType=&q ...
- Monkey安装和使用介绍
安装步骤1)安装sdk环境在系统环境变量中配置 ANDROID_HOMED:\sdk PATH%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%A ...
- 精仿百思不得姐客户端应用iOS源码
XFBaiSiBuDeJie 高仿百思不得姐客户端 初次学习使用RAC,还不是怎么熟悉,使用的仍是MVC模式,MVVM还在摸索中... 如果大家觉得还不错,请给颗星星支持下~~~ 程序中使用到的库 A ...
- Asp.Net Core 入门(十)—— 模型绑定和验证
模型绑定时将Http请求中的数据映射到控制器操作方法上对应的参数,操作方法中的参数可以是简单类型,如整形,字符串等,也可以是复杂类型,如Product,Order等. Asp.Net Core MVC ...