java poi 4.1.2 操作excel说明

描述

  • poi是Apache下一款java语言excle高效读写工具,本博客将从读、写等方面做说明,xls(2007前版本)和xlsx(2007版本和此后),使用不同类,讲分开讲解,推荐使用xlsx,行数更多,相同数据,存储更小。

导包

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.FillPatternType;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

excel写入

  • 2003版前模板excel生成
	/**
	 * 创建xls文件 2003前版本
	 * @param name
	 */
	public static void createExcel(String name) {
		try {
			FileOutputStream output = new FileOutputStream(name);
			HSSFWorkbook wkb = new HSSFWorkbook();
			HSSFSheet sheet = wkb.createSheet("节点列表");

			//设置样式
			HSSFCellStyle style = wkb.createCellStyle();
			style.setAlignment(HorizontalAlignment.CENTER);  //水平居中
			style.setVerticalAlignment(VerticalAlignment.CENTER);  //垂直居中
			
		    style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置填充模式,模式为全部前景色
			style.setFillForegroundColor(HSSFColorPredefined.YELLOW.getColor().getIndex());  // 设置前景色为黄色

			//创建行
			HSSFRow row2 = sheet.createRow(0);
			
			//创建单元格
			HSSFCell cell = row2.createCell(0);
			cell.setCellValue("ip");
			cell.setCellStyle(style);
			
			cell = row2.createCell(1);
			cell.setCellValue("端口");
			cell.setCellStyle(style);
			
			cell = row2.createCell(2);
			cell.setCellValue("用户名");
			cell.setCellStyle(style);
			
			cell = row2.createCell(3);
			cell.setCellValue("密码");
			cell.setCellStyle(style);
			
			cell = row2.createCell(4);
			cell.setCellValue("说明");
			cell.setCellStyle(style);
			
			wkb.write(output);
			output.flush();
			output.close();
			wkb.close();
		} catch (FileNotFoundException e) {
			log.error("未发现文件" + e);
		} catch (IOException e) {
			log.error("创建excel读取异常" + e);
		}
	}
  • 生成xlsx文件
	/**
	 * 创建xlsx文件 2007后版本
	 * @param name
	 */
	public static void createExcelXlsx(String name) {
		try {
			FileOutputStream output = new FileOutputStream(name);
			XSSFWorkbook workbook = new XSSFWorkbook();
			XSSFSheet  sheet = workbook.createSheet("节点列表");

			//设置样式
			XSSFCellStyle style = workbook.createCellStyle();
			style.setAlignment(HorizontalAlignment.CENTER);  //水平居中
			style.setVerticalAlignment(VerticalAlignment.CENTER);  //垂直居中

			style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置填充模式,模式为全部前景色
			style.setFillForegroundColor(HSSFColorPredefined.YELLOW.getColor().getIndex());  // 设置前景色为黄色

			//创建行
			XSSFRow  row2 = sheet.createRow(0);
			
			//创建单元格
			XSSFCell  cell = row2.createCell(0);
			cell.setCellValue("ip");
			cell.setCellStyle(style);
			
			cell = row2.createCell(1);
			cell.setCellValue("端口");
			cell.setCellStyle(style);
			
			cell = row2.createCell(2);
			cell.setCellValue("用户名");
			cell.setCellStyle(style);
			
			cell = row2.createCell(3);
			cell.setCellValue("密码");
			cell.setCellStyle(style);
			
			cell = row2.createCell(4);
			cell.setCellValue("说明");
			cell.setCellStyle(style);
			
			workbook.write(output);
			output.flush();
			output.close();
			workbook.close();
		} catch (FileNotFoundException e) {
			log.error("未发现文件" + e);
		} catch (IOException e) {
			log.error("创建excel读取异常" + e);
		}
	}

excel读取

  • 解析excle根据文件名后缀自动判断版本,执行不同实现类,true为新版本,false为旧版本。
	public static boolean checkVersion(String file) {
		if(file.matches("^.+\\.(?i)(xlsx)$")) {
			return true;
		}
		return false;
	}
  • 解析过程
	public static List<String> readExcelSecond(String xlsPath) {
		try {
			List<String> temp = new ArrayList<>();

			FileInputStream fileIn = new FileInputStream(xlsPath);
			Workbook wb = null;
			if (checkVersion(xlsPath))
				wb = new XSSFWorkbook(fileIn);  //xlsx
			else {
				wb = new HSSFWorkbook(fileIn);  //xls
			}

			Sheet sht0 = wb.getSheetAt(0);

			for (Row r : sht0) {
				if (r.getRowNum() >= 1) {
					
					//String 数据
					if ((r.getCell(0) != null) && (r.getCell(0).toString() != "")) {
						System.out.println(r.getCell(0).getStringCellValue().trim());
						temp.add(r.getCell(0).getStringCellValue().trim());
					}
					
					//number数据
					if ((r.getCell(1) != null) && (r.getCell(1).toString() != "")) {
						System.out.println((int) r.getCell(1).getNumericCellValue());
					}
					
					//未知数据类型数据
					if ((r.getCell(1) != null) && (r.getCell(1).toString() != "")) {
						System.out.println(r.getCell(1).toString());
					}
					
				}
			}
			fileIn.close();
			wb.close();
			
			return temp;
		} catch (Exception e) {
			log.error(e.toString(), e);
		}
		return null;
	}

数据类型与多行多列

  • 读取excle时,数字有很多类型设置,如金额、百分数等,展示数据和实际读取不一致,读取为原始数据,
  • 多行多列时,以合并后,第一行、第一列下标读取,后续行列读取为空。
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值