POI操作Excel详解,HSSF和XSSF两种方式
- package com.tools.poi.lesson1;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- 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;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import com.tools.poi.bean.Student;
- public class ExcelUtilWithHSSF {
- public static void main(String[] args) {
- try {
- getExcelAsFile("aaa");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // try {
- // CreateExcelDemo1();
- // } catch (ParseException e) {
- // e.printStackTrace();
- // }
- }
- /**
- * 得到Excel,并解析内容
- * @param file
- * @throws FileNotFoundException
- * @throws IOException
- */
- public static void getExcelAsFile(String file) throws FileNotFoundException, IOException{
- //1.得到Excel常用对象
- // POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/test.xls"));
- POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
- //2.得到Excel工作簿对象
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- //3.得到Excel工作表对象
- HSSFSheet sheet = wb.getSheetAt(0);
- //总行数
- int trLength = sheet.getLastRowNum();
- //4.得到Excel工作表的行
- HSSFRow row = sheet.getRow(0);
- //总列数
- int tdLength = row.getLastCellNum();
- //5.得到Excel工作表指定行的单元格
- HSSFCell cell = row.getCell((short)1);
- //6.得到单元格样式
- CellStyle cellStyle = cell.getCellStyle();
- for(int i=0;i<trLength;i++){
- //得到Excel工作表的行
- HSSFRow row1 = sheet.getRow(i);
- for(int j=0;j<tdLength;j++){
- //得到Excel工作表指定行的单元格
- HSSFCell cell1 = row1.getCell(j);
- /**
- * 为了处理:Excel异常Cannot get a text value from a numeric cell
- * 将所有列中的内容都设置成String类型格式
- */
- if(cell1!=null){
- cell1.setCellType(Cell.CELL_TYPE_STRING);
- }
- //获得每一列中的值
- System.out.print(cell1.getStringCellValue()+"\t\t\t");
- }
- System.out.println();
- }
- }
- /**
- * 创建Excel,并写入内容
- */
- public static void CreateExcel(){
- //1.创建Excel工作薄对象
- HSSFWorkbook wb = new HSSFWorkbook();
- //2.创建Excel工作表对象
- HSSFSheet sheet = wb.createSheet("new Sheet");
- //3.创建Excel工作表的行
- HSSFRow row = sheet.createRow(6);
- //4.创建单元格样式
- CellStyle cellStyle =wb.createCellStyle();
- // 设置这些样式
- cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
- cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
- cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
- cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- //5.创建Excel工作表指定行的单元格
- row.createCell(0).setCellStyle(cellStyle);
- //6.设置Excel工作表的值
- row.createCell(0).setCellValue("aaaa");
- row.createCell(1).setCellStyle(cellStyle);
- row.createCell(1).setCellValue("bbbb");
- //设置sheet名称和单元格内容
- wb.setSheetName(0,"第一张工作表");
- //设置单元格内容 cell.setCellValue("单元格内容");
- // 最后一步,将文件存到指定位置
- try
- {
- FileOutputStream fout = new FileOutputStream("E:/students.xls");
- wb.write(fout);
- fout.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- /**
- * 创建Excel的实例
- * @throws ParseException
- */
- public static void CreateExcelDemo1() throws ParseException{
- List list = new ArrayList();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
- Student user1 = new Student(1, "张三", 16,true, df.parse("1997-03-12"));
- Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));
- Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));
- list.add(user1);
- list.add(user2);
- list.add(user3);
- // 第一步,创建一个webbook,对应一个Excel文件
- HSSFWorkbook wb = new HSSFWorkbook();
- // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
- HSSFSheet sheet = wb.createSheet("学生表一");
- // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
- HSSFRow row = sheet.createRow((int) 0);
- // 第四步,创建单元格,并设置值表头 设置表头居中
- HSSFCellStyle style = wb.createCellStyle();
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
- HSSFCell cell = row.createCell((short) 0);
- cell.setCellValue("学号");
- cell.setCellStyle(style);
- cell = row.createCell((short) 1);
- cell.setCellValue("姓名");
- cell.setCellStyle(style);
- cell = row.createCell((short) 2);
- cell.setCellValue("年龄");
- cell.setCellStyle(style);
- cell = row.createCell((short) 3);
- cell.setCellValue("性别");
- cell.setCellStyle(style);
- cell = row.createCell((short) 4);
- cell.setCellValue("生日");
- cell.setCellStyle(style);
- // 第五步,写入实体数据 实际应用中这些数据从数据库得到,
- for (int i = 0; i < list.size(); i++)
- {
- row = sheet.createRow((int) i + 1);
- Student stu = (Student) list.get(i);
- // 第四步,创建单元格,并设置值
- row.createCell((short) 0).setCellValue((double) stu.getId());
- row.createCell((short) 1).setCellValue(stu.getName());
- row.createCell((short) 2).setCellValue((double) stu.getAge());
- row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");
- cell = row.createCell((short) 4);
- cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
- .getBirthday()));
- }
- // 第六步,将文件存到指定位置
- try
- {
- FileOutputStream fout = new FileOutputStream("E:/students.xls");
- wb.write(fout);
- fout.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
XSSF方式:
- package com.tools.poi.lesson1;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- 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;
- import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.ss.usermodel.WorkbookFactory;
- import org.apache.poi.ss.util.WorkbookUtil;
- import com.tools.poi.bean.Student;
- public class ExcelUtilWithXSSF {
- public static void main(String[] args) {
- try {
- getExcelAsFile("d:/FTP/系统报表.xls");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InvalidFormatException e) {
- e.printStackTrace();
- }
- // try {
- // CreateExcelDemo1();
- // } catch (ParseException e) {
- // e.printStackTrace();
- // }
- }
- /**
- * 得到Excel,并解析内容 对2007及以上版本 使用XSSF解析
- * @param file
- * @throws FileNotFoundException
- * @throws IOException
- * @throws InvalidFormatException
- */
- public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException{
- // //1.得到Excel常用对象
- // POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
- // //2.得到Excel工作簿对象
- // HSSFWorkbook wb = new HSSFWorkbook(fs);
- InputStream ins = null;
- Workbook wb = null;
- ins=new FileInputStream(new File(file));
- //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);
- wb = WorkbookFactory.create(ins);
- ins.close();
- //3.得到Excel工作表对象
- Sheet sheet = wb.getSheetAt(0);
- //总行数
- int trLength = sheet.getLastRowNum();
- //4.得到Excel工作表的行
- Row row = sheet.getRow(0);
- //总列数
- int tdLength = row.getLastCellNum();
- //5.得到Excel工作表指定行的单元格
- Cell cell = row.getCell((short)1);
- //6.得到单元格样式
- CellStyle cellStyle = cell.getCellStyle();
- for(int i=5;i<trLength;i++){
- //得到Excel工作表的行
- Row row1 = sheet.getRow(i);
- for(int j=0;j<tdLength;j++){
- //得到Excel工作表指定行的单元格
- Cell cell1 = row1.getCell(j);
- /**
- * 为了处理:Excel异常Cannot get a text value from a numeric cell
- * 将所有列中的内容都设置成String类型格式
- */
- if(cell1!=null){
- cell1.setCellType(Cell.CELL_TYPE_STRING);
- }
- if(j==5&&i<=10){
- cell1.setCellValue("1000");
- }
- //获得每一列中的值
- System.out.print(cell1+" ");
- }
- System.out.println();
- }
- //将修改后的数据保存
- OutputStream out = new FileOutputStream(file);
- wb.write(out);
- }
- /**
- * 创建Excel,并写入内容
- */
- public static void CreateExcel(){
- //1.创建Excel工作薄对象
- HSSFWorkbook wb = new HSSFWorkbook();
- //2.创建Excel工作表对象
- HSSFSheet sheet = wb.createSheet("new Sheet");
- //3.创建Excel工作表的行
- HSSFRow row = sheet.createRow(6);
- //4.创建单元格样式
- CellStyle cellStyle =wb.createCellStyle();
- // 设置这些样式
- cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
- cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
- cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
- cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- //5.创建Excel工作表指定行的单元格
- row.createCell(0).setCellStyle(cellStyle);
- //6.设置Excel工作表的值
- row.createCell(0).setCellValue("aaaa");
- row.createCell(1).setCellStyle(cellStyle);
- row.createCell(1).setCellValue("bbbb");
- //设置sheet名称和单元格内容
- wb.setSheetName(0,"第一张工作表");
- //设置单元格内容 cell.setCellValue("单元格内容");
- // 最后一步,将文件存到指定位置
- try
- {
- FileOutputStream fout = new FileOutputStream("E:/students.xls");
- wb.write(fout);
- fout.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- /**
- * 创建Excel的实例
- * @throws ParseException
- */
- public static void CreateExcelDemo1() throws ParseException{
- List list = new ArrayList();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
- Student user1 = new Student(1, "张三", 16,true, df.parse("1997-03-12"));
- Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));
- Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));
- list.add(user1);
- list.add(user2);
- list.add(user3);
- // 第一步,创建一个webbook,对应一个Excel文件
- HSSFWorkbook wb = new HSSFWorkbook();
- // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
- HSSFSheet sheet = wb.createSheet("学生表一");
- // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
- HSSFRow row = sheet.createRow((int) 0);
- // 第四步,创建单元格,并设置值表头 设置表头居中
- HSSFCellStyle style = wb.createCellStyle();
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
- HSSFCell cell = row.createCell((short) 0);
- cell.setCellValue("学号");
- cell.setCellStyle(style);
- cell = row.createCell((short) 1);
- cell.setCellValue("姓名");
- cell.setCellStyle(style);
- cell = row.createCell((short) 2);
- cell.setCellValue("年龄");
- cell.setCellStyle(style);
- cell = row.createCell((short) 3);
- cell.setCellValue("性别");
- cell.setCellStyle(style);
- cell = row.createCell((short) 4);
- cell.setCellValue("生日");
- cell.setCellStyle(style);
- // 第五步,写入实体数据 实际应用中这些数据从数据库得到,
- for (int i = 0; i < list.size(); i++)
- {
- row = sheet.createRow((int) i + 1);
- Student stu = (Student) list.get(i);
- // 第四步,创建单元格,并设置值
- row.createCell((short) 0).setCellValue((double) stu.getId());
- row.createCell((short) 1).setCellValue(stu.getName());
- row.createCell((short) 2).setCellValue((double) stu.getAge());
- row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");
- cell = row.createCell((short) 4);
- cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
- .getBirthday()));
- }
- // 第六步,将文件存到指定位置
- try
- {
- FileOutputStream fout = new FileOutputStream("E:/students.xls");
- wb.write(fout);
- fout.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
注意:
修改Excel中的某个内容:
- cell1.setCellValue("1000");
保存修改后的Excel文件:
- OutputStream out = new FileOutputStream(file);
- wb.write(out);
POI操作Excel详解,HSSF和XSSF两种方式的更多相关文章
- POI操作Excel详细解释,HSSF和XSSF两种方式
HSSF道路: package com.tools.poi.lesson1; import java.io.FileInputStream; import java.io.FileNotFoundEx ...
- POI操作Excel详解,读取xls和xlsx格式的文件
package org.ian.webutil; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...
- Linux 下Redis集群安装部署及使用详解(在线和离线两种安装+相关错误解决方案)
一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,其中主要包括在联网的Linux环境和脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网 ...
- Selector、shape详解,注意这两种图像资源都以XML方式存放在drawable不带分辨率的文件夹中
Selector.shape详解(一) Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> ...
- POI操作Excel导入和导出
Apache的POI组件是Java操作Microsoft Office办公套件的强大API,当中对Word,Excel和PowperPoint都有支持,当然使用较多的还是Excel.由于Word和Po ...
- poi操作excel的基本用法
这周公司要用excel作为数据存储格式做一个文具申请的功能,感觉以前本来很简单的功能变复杂了不少,但是还是记录一下一些excel的基本用法. 写在最前面:这里只介绍一些excel的基本存储方式(读,写 ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- 自己的包poi操作Excel工具
在前面的文章<使用poi读写Excel>中分享了一下poi操作Excel的简单演示样例.这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完毕的功能是:读取Excel.汇总E ...
- POI操作Excel(xls、xlsx)
阿帕奇官网:http://poi.apache.org/ POI3.17下载:http://poi.apache.org/download.html#POI-3.17 POI操作Excel教程(易百教 ...
随机推荐
- 【iMooc】全面解析java注解
在慕课上学习了一个关于java注解的课程,下面是笔记以及一些源码. Annotation——注解 1.JDK中的注解 JDK中包括下面三种注解: @Override:标记注解(marker annot ...
- Jmeter使用笔记之组件的作用域
以前一直使用loadrunner,最近入职新公司后需要使用jmeter,这里把使用过程中出现的一些问题进行总结,同时会和自己使用loadrunner的情况相比较,以后也会不断总结,GO! 一.组件的作 ...
- [转帖] wordpress 的安装过程
https://blog.csdn.net/qq_34364668/article/details/78116473 下载Wordpress wget http://wordpress.org/lat ...
- EntityFramework中Json序列化的循环引用问题解决--Newtonsoft.Json
1.在使用EF时,由于数据库主外键关联,将对象进行Json序列化时会遇到循环引用的问题 //EF 中由于数据库主外键关联,对象的序列化经常出现循环引用问题 //使用.Net 自带的序列化工具,序列化出 ...
- 一本通1656Combination
1656:Combination 时间限制: 1000 ms 内存限制: 524288 KB [题目描述] 原题来自:BZOJ 2982 LMZ 有 n 个不同的基友,他每天晚上要选 ...
- 【题解】 bzoj1260: [CQOI2007]涂色paint (区间dp)
bzoj1260,懒得复制,戳我戳我 Solution: 这种题目我不会做qwq,太菜了 区间打牌(dp) 用f[l][r]表示从l到r最少需要染几次色. 状态转移方程: 1.\(f[l][r]=mi ...
- AtCoder Grand Contest 006
AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- <meta http-equiv="X-UA-Compatible" content="IE=7" />意思是将IE8用IE7进行渲染,使网页在IE8下正常
X-UA-Compatible是针对ie8新加的一个设置,对于ie8之外的浏览器是不识别的,这个区别与content="IE=7"在无论页面是否包含<!DOCTYPE> ...
- SQL记录-PLSQL变量与常量文字
PL/SQL变量 变量是只不过是一个给定的存储区域,程序可以操纵的名称.PL/SQL每个变量具有一个特定的数据类型,它决定了大小和变量的存储器的值,可以说存储器和设置操作可以施加到可变内被存储的范 ...