HSSF道路:

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两种方式的更多相关文章

  1. POI操作Excel详解,HSSF和XSSF两种方式

    package com.tools.poi.lesson1; import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  2. POI操作Excel导入和导出

    Apache的POI组件是Java操作Microsoft Office办公套件的强大API,当中对Word,Excel和PowperPoint都有支持,当然使用较多的还是Excel.由于Word和Po ...

  3. 自己的包poi操作Excel工具

    在前面的文章<使用poi读写Excel>中分享了一下poi操作Excel的简单演示样例.这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完毕的功能是:读取Excel.汇总E ...

  4. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  5. poi操作excel的基本用法

    这周公司要用excel作为数据存储格式做一个文具申请的功能,感觉以前本来很简单的功能变复杂了不少,但是还是记录一下一些excel的基本用法. 写在最前面:这里只介绍一些excel的基本存储方式(读,写 ...

  6. POI操作Excel(xls、xlsx)

    阿帕奇官网:http://poi.apache.org/ POI3.17下载:http://poi.apache.org/download.html#POI-3.17 POI操作Excel教程(易百教 ...

  7. java使用POI操作excel文件,实现批量导出,和导入

    一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...

  8. java里poi操作excel的工具类(兼容各版本)

    转: java里poi操作excel的工具类(兼容各版本) 下面是文件内具体内容,文件下载: import java.io.FileNotFoundException; import java.io. ...

  9. 利用Apache POI操作Excel

    最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中.简单回顾一下之前学过的用java操作Excel. 1.maven配置Apache POI pom.xml中配置POIjar ...

随机推荐

  1. C# 抽象类其中创建一个静态方法

    </pre><pre name="code" class="csharp"><span style="font-size ...

  2. hdu 3683 Gomoku (模拟、搜索)

    Gomoku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. 《Java并发编程实战》第二章 线程安全 札记

    一个.什么是线程安全 编写线程安全的代码 其核心是管理国事访问的操作. 共享,可变的状态的訪问 - 前者表示多个线程訪问, 后者声明周期内发生改变. 线程安全性 核心概念是正确性.某个类的行为与其规范 ...

  4. Android开发在路上:少去踩坑,多走捷径(转)

    最近一朋友提了几个Android问题让我帮忙写个小分享,我觉得对新人还是挺有帮助的,所以有了这个小分享. 1. 目前, Android APP开发完成后,通常需要在哪些机型上进行测试? 2. 目前, ...

  5. springMvc的注解注入方式

    springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...

  6. 使用批处理文件命令行方式快速启动和停止IIS、SqlServer

    原文:使用批处理文件命令行方式快速启动和停止IIS.SqlServer 虽然现在内存便宜了,但是自己还是嫌自己的512M内存太小,没办法,后台运行的东西太多了,有很多都是有用的没法关闭的.IIS和SQ ...

  7. 单节点伪分布式Hadoop配置

    本文所用软件版本: VMware-workstation-full-11.1.0 jdk-6u45-linux-i586.bin ubuntukylin-14.04-desktop-i386.iso ...

  8. Dubbo与Zookeeper、SpringMVC整合和利用(负载均衡、容错)

    互联网发展,扩大了网站应用程序的大小.传统的垂直应用架构已经无法应付.分布式服务架构和流量计算架构势在必行,Dubbo是一个分布式服务框架.在这样的情况下诞生的.如今核心业务抽取出来.作为独立的服务, ...

  9. Nio得知3——该示范基地:多路复用器模式

    Reactor模式和NIO 本文可以看作是Doug Lea Scalable IO in Java一文的翻译. 当前分布式计算 Web Services盛行天下,这些网络服务的底层都离不开对socke ...

  10. php_DWZ-JUI中碰到的问题解决方法详解(thinkphp+dwz)

    原文:php_DWZ-JUI中碰到的问题解决方法详解(thinkphp+dwz) 折腾了两天,dwz删除后,数据不能自动刷新,解决方案,直接看图  . 1. 删除.修改状态后无法刷新记录: 在dwz. ...