java的poi技术读,写Excel[2003-2007,2010]
在上一篇blog:java的poi技术读取Excel[2003-2007,2010] 中介绍了关于java中的poi技术读取excel的相关操作
读取excel和MySQL相关: java的poi技术读取Excel数据到MySQL
你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息
使用JXL技术 :java的jxl技术导入Excel
本文主要讲的是java中poi读和写excel(版本是2003-2007,2010)
项目结构:
所用到的excel
运行效果:
Processing...lib/student_info.xls
No. : 201401, name : Hongten, age : 22.0, score : 98.0
No. : 201402, name : Hanyuan, age : 21.0, score : 96.0
No. : 201403, name : Tom, age : 20.0, score : 94.0
No. : 201404, name : HanKet, age : 21.0, score : 90.0
No. : 201405, name : Jone, age : 22.0, score : 42.0
No. : 201406, name : Kite, age : 23.0, score : 59.0
No. : 201407, name : Lucy, age : 25.0, score : 60.0
No. : 201408, name : Mark, age : 23.0, score : 89.0
No. : 201409, name : Jeson, age : 24.0, score : 79.0
No. : 201410, name : Steven, age : 23.0, score : 59.0
No. : 201411, name : Tide, age : 22.0, score : 88.0
No. : 201412, name : Deli, age : 22.0, score : 89.0
No. : 201413, name : Goerme, age : 21.0, score : 98.0
No. : 201414, name : Som, age : 23.0, score : 95.0
======================================
Processing...lib/student_info.xlsx
No. : 201401, name : Hongten, age : 22.0, score : 98.0
No. : 201402, name : Hanyuan, age : 21.0, score : 96.0
No. : 201403, name : Tom, age : 20.0, score : 94.0
No. : 201404, name : HanKet, age : 21.0, score : 90.0
No. : 201405, name : Jone, age : 22.0, score : 42.0
No. : 201406, name : Kite, age : 23.0, score : 59.0
No. : 201407, name : Lucy, age : 25.0, score : 60.0
No. : 201408, name : Mark, age : 23.0, score : 89.0
No. : 201409, name : Jeson, age : 24.0, score : 79.0
No. : 201410, name : Steven, age : 23.0, score : 59.0
No. : 201411, name : Tide, age : 22.0, score : 88.0
No. : 201412, name : Deli, age : 22.0, score : 89.0
No. : 201413, name : Goerme, age : 21.0, score : 98.0
No. : 201414, name : Som, age : 23.0, score : 95.0
======================================
write data to file : lib/student_info_2003-2007.xls
write data to file : lib/student_info_2010.xlsx
======================================
Processing...lib/student_info_2003-2007.xls
No. : 201401, name : Hongten, age : 22.0, score : 98.0
No. : 201402, name : Hanyuan, age : 21.0, score : 96.0
No. : 201403, name : Tom, age : 20.0, score : 94.0
No. : 201404, name : HanKet, age : 21.0, score : 90.0
No. : 201405, name : Jone, age : 22.0, score : 42.0
No. : 201406, name : Kite, age : 23.0, score : 59.0
No. : 201407, name : Lucy, age : 25.0, score : 60.0
No. : 201408, name : Mark, age : 23.0, score : 89.0
No. : 201409, name : Jeson, age : 24.0, score : 79.0
No. : 201410, name : Steven, age : 23.0, score : 59.0
No. : 201411, name : Tide, age : 22.0, score : 88.0
No. : 201412, name : Deli, age : 22.0, score : 89.0
No. : 201413, name : Goerme, age : 21.0, score : 98.0
No. : 201414, name : Som, age : 23.0, score : 95.0
======================================
Processing...lib/student_info_2010.xlsx
No. : 201401, name : Hongten, age : 22.0, score : 98.0
No. : 201402, name : Hanyuan, age : 21.0, score : 96.0
No. : 201403, name : Tom, age : 20.0, score : 94.0
No. : 201404, name : HanKet, age : 21.0, score : 90.0
No. : 201405, name : Jone, age : 22.0, score : 42.0
No. : 201406, name : Kite, age : 23.0, score : 59.0
No. : 201407, name : Lucy, age : 25.0, score : 60.0
No. : 201408, name : Mark, age : 23.0, score : 89.0
No. : 201409, name : Jeson, age : 24.0, score : 79.0
No. : 201410, name : Steven, age : 23.0, score : 59.0
No. : 201411, name : Tide, age : 22.0, score : 88.0
No. : 201412, name : Deli, age : 22.0, score : 89.0
No. : 201413, name : Goerme, age : 21.0, score : 98.0
No. : 201414, name : Som, age : 23.0, score : 95.0
=================================================
源码部分:
=================================================
/Excel2010/src/com/b510/excel/client/Client.java
/**
*
*/
package com.b510.excel.client; import java.util.List; import com.b510.excel.common.Common;
import com.b510.excel.util.ExcelUtil;
import com.b510.excel.vo.Student; /**
* @author Hongten
* @created 2014-5-21
*/
public class Client { public static void main(String[] args) throws Exception {
String read_excel2003_2007_path = Common.STUDENT_INFO_XLS_PATH;
String read_excel2010_path = Common.STUDENT_INFO_XLSX_PATH;
// read the 2003-2007 excel
List<Student> list = new ExcelUtil().readExcel(read_excel2003_2007_path);
if (list != null) {
for (Student student : list) {
System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
}
}
System.out.println("======================================");
// read the 2010 excel
List<Student> list1 = new ExcelUtil().readExcel(read_excel2010_path);
if (list1 != null) {
for (Student student : list1) {
System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
}
}
System.out.println("======================================");
String write_excel2003_2007_path = Common.STUDENT_INFO_XLS_OUT_PATH;
String write_excel2010_path = Common.STUDENT_INFO_XLSX_OUT_PATH;
new ExcelUtil().writeExcel(list, write_excel2003_2007_path);
new ExcelUtil().writeExcel(list, write_excel2010_path);
System.out.println("======================================"); // read the 2003-2007 excel
List<Student> list2 = new ExcelUtil().readExcel(write_excel2003_2007_path);
if (list != null) {
for (Student student : list2) {
System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
}
}
System.out.println("======================================");
// read the 2010 excel
List<Student> list3 = new ExcelUtil().readExcel(write_excel2010_path);
if (list1 != null) {
for (Student student : list3) {
System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
}
}
}
}
/Excel2010/src/com/b510/excel/common/Common.java
/**
*
*/
package com.b510.excel.common; /**
* @author Hongten
* @created 2014-5-21
*/
public class Common { public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx"; public static final String EMPTY = "";
public static final String POINT = ".";
public static final String LIB_PATH = "lib";
public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2003_POSTFIX;
public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2010_POSTFIX;
public static final String STUDENT_INFO_XLS_OUT_PATH = "lib/student_info_2003-2007.xls";
public static final String STUDENT_INFO_XLSX_OUT_PATH = "lib/student_info_2010.xlsx";
public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
public static final String PROCESSING = "Processing...";
public static final String WRITE_DATA = "write data to file : "; }
/Excel2010/src/com/b510/excel/util/ExcelUtil.java
/**
*
*/
package com.b510.excel.util; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.b510.excel.common.Common;
import com.b510.excel.vo.Student; /**
* @author Hongten
* @created 2014-5-20
*/
public class ExcelUtil { public void writeExcel(List<Student> list, String path) throws Exception {
if (list == null) {
return;
} else if (path == null || Common.EMPTY.equals(path)) {
return;
} else {
String postfix = Util.getPostfix(path);
if (!Common.EMPTY.equals(postfix)) {
if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
writeXls(list, path);
} else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
writeXlsx(list, path);
}
}else{
System.out.println(path + Common.NOT_EXCEL_FILE);
}
}
} /**
* read the Excel file
* @param path the path of the Excel file
* @return
* @throws IOException
*/
public List<Student> readExcel(String path) throws IOException {
if (path == null || Common.EMPTY.equals(path)) {
return null;
} else {
String postfix = Util.getPostfix(path);
if (!Common.EMPTY.equals(postfix)) {
if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
return readXls(path);
} else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
return readXlsx(path);
}
} else {
System.out.println(path + Common.NOT_EXCEL_FILE);
}
}
return null;
} /**
* Read the Excel 2010
* @param path the path of the excel file
* @return
* @throws IOException
*/
public List<Student> readXlsx(String path) throws IOException {
System.out.println(Common.PROCESSING + path);
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow != null) {
student = new Student();
XSSFCell no = xssfRow.getCell(0);
XSSFCell name = xssfRow.getCell(1);
XSSFCell age = xssfRow.getCell(2);
XSSFCell score = xssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
return list;
} /**
* Read the Excel 2003-2007
* @param path the path of the Excel
* @return
* @throws IOException
*/
public List<Student> readXls(String path) throws IOException {
System.out.println(Common.PROCESSING + path);
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
student = new Student();
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
HSSFCell age = hssfRow.getCell(2);
HSSFCell score = hssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
return list;
} @SuppressWarnings("static-access")
private String getValue(XSSFCell xssfRow) {
if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfRow.getBooleanCellValue());
} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
return String.valueOf(xssfRow.getNumericCellValue());
} else {
return String.valueOf(xssfRow.getStringCellValue());
}
} @SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
} public void writeXls(List<Student> list, String path) throws Exception {
if (list == null) {
return;
}
int countColumnNum = list.size();
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet("studentSheet");
// option at first row.
HSSFRow firstRow = sheet.createRow(0);
HSSFCell[] firstCells = new HSSFCell[countColumnNum];
String[] options = { "no", "name", "age", "score" };
for (int j = 0; j < options.length; j++) {
firstCells[j] = firstRow.createCell(j);
firstCells[j].setCellValue(new HSSFRichTextString(options[j]));
}
//
for (int i = 0; i < countColumnNum; i++) {
HSSFRow row = sheet.createRow(i + 1);
Student student = list.get(i);
for (int column = 0; column < options.length; column++) {
HSSFCell no = row.createCell(0);
HSSFCell name = row.createCell(1);
HSSFCell age = row.createCell(2);
HSSFCell score = row.createCell(3);
no.setCellValue(student.getNo());
name.setCellValue(student.getName());
age.setCellValue(student.getAge());
score.setCellValue(student.getScore());
}
}
File file = new File(path);
OutputStream os = new FileOutputStream(file);
System.out.println(Common.WRITE_DATA + path);
book.write(os);
os.close();
} public void writeXlsx(List<Student> list, String path) throws Exception {
if (list == null) {
return;
}
//XSSFWorkbook
int countColumnNum = list.size();
XSSFWorkbook book = new XSSFWorkbook();
XSSFSheet sheet = book.createSheet("studentSheet");
// option at first row.
XSSFRow firstRow = sheet.createRow(0);
XSSFCell[] firstCells = new XSSFCell[countColumnNum];
String[] options = { "no", "name", "age", "score" };
for (int j = 0; j < options.length; j++) {
firstCells[j] = firstRow.createCell(j);
firstCells[j].setCellValue(new XSSFRichTextString(options[j]));
}
//
for (int i = 0; i < countColumnNum; i++) {
XSSFRow row = sheet.createRow(i + 1);
Student student = list.get(i);
for (int column = 0; column < options.length; column++) {
XSSFCell no = row.createCell(0);
XSSFCell name = row.createCell(1);
XSSFCell age = row.createCell(2);
XSSFCell score = row.createCell(3);
no.setCellValue(student.getNo());
name.setCellValue(student.getName());
age.setCellValue(student.getAge());
score.setCellValue(student.getScore());
}
}
File file = new File(path);
OutputStream os = new FileOutputStream(file);
System.out.println(Common.WRITE_DATA + path);
book.write(os);
os.close();
}
}
/Excel2010/src/com/b510/excel/util/Util.java
/**
*
*/
package com.b510.excel.util; import com.b510.excel.common.Common; /**
* @author Hongten
* @created 2014-5-21
*/
public class Util { /**
* get postfix of the path
* @param path
* @return
*/
public static String getPostfix(String path) {
if (path == null || Common.EMPTY.equals(path.trim())) {
return Common.EMPTY;
}
if (path.contains(Common.POINT)) {
return path.substring(path.lastIndexOf(Common.POINT) + 1, path.length());
}
return Common.EMPTY;
}
}
/Excel2010/src/com/b510/excel/vo/Student.java
/**
*
*/
package com.b510.excel.vo; /**
* Student
*
* @author Hongten
* @created 2014-5-18
*/
public class Student {
/**
* id
*/
private Integer id;
/**
* 学号
*/
private String no;
/**
* 姓名
*/
private String name;
/**
* 学院
*/
private String age;
/**
* 成绩
*/
private float score; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getNo() {
return no;
} public void setNo(String no) {
this.no = no;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public float getScore() {
return score;
} public void setScore(float score) {
this.score = score;
} }
源码下载:http://pan.baidu.com/s/1eQpEf0u
========================================================
More reading,and english is important.
I'm Hongten
大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten
========================================================
java的poi技术读,写Excel[2003-2007,2010]的更多相关文章
- Java Struts2读取Excel 2003/2007/2010例子
Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版. Apache POI ...
- java使用poi.3.10读取excel 2003 (xls格式)
最近在做一个Excel导入数据库的案例,整理文档出来供大家参考. 1.下载 最新的 poi http://poi.apache.org/download.html 2.解压 把相关jar包引进项 ...
- java的poi技术写Excel的Sheet
在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-20 ...
- java的poi技术读取Excel数据到MySQL
这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...
- java的poi技术读取和导入Excel实例
本篇文章主要介绍了java的poi技术读取和导入Excel实例,报表输出是Java应用开发中经常涉及的内容,有需要的可以了解一下. 报表输出是Java应用开发中经常涉及的内容,而一般的报表往往缺乏通用 ...
- java的poi技术读取和导入Excel
项目结构: http://www.cnblogs.com/hongten/gallery/image/111987.html 用到的Excel文件: http://www.cnblogs.com/h ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- java的poi技术读取Excel数据
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
随机推荐
- 在.net中使用GAC
转自:http://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的dll的 ...
- 安装 Ruby, Rails 运行环境 常见的错误
安装部署ruby on rails 的环境时并不是想的那么顺利 这个是我遇到的问题及解决的方式 参考安装博客: (1) https://ruby-china.org/wiki/install_ruby ...
- java8入门 错误:找不到或者无法加载主类
如果你也遇上的这个问题,但是如果你的Java版本不是6以上,这个解决方案可能就不适合你... 最近在跟着李兴华老湿的视频<<编程开发入门Java 8>>的学习Java... 但 ...
- Excel—身份证生日提取
一.只有18位的身份证号码 如: 身份证号 330682199302264000 41120019890823729X 231081199002256839 131101198203154666 36 ...
- tyvj1863 [Poetize I]黑魔法师之门
背景 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源.然而在与Violet星球的战争中,由于Z副官的愚蠢,地球的领袖applepi被邪恶的黑魔法师Vani囚禁在了Violet星球.为了 ...
- 评价cnblogs.com的用户体验
我们作为大学计算机科学与技术专业的本科生,使用博客园主要是用来搜索和阅读技术资料,并且使用它来编辑发表博客.我们偏向于搜索便捷,界面友好的网站风格.我们对cnblogs的期望是更便捷精准,更便于编辑. ...
- mount img
直接挂载img文件有时会有 mount:您必须指定文件系统类型 的错误,但加 -t ext2 等类型还是没用. 这是因为img文件包含了mbr引导导致的问题.解决方法如下: $sudo fdis ...
- SSI指令
1.SSI定义 SSI是英文Server Side Includes的缩写, 即“服务器端包含”或“服务器端嵌入”技术. SSI在HTML文件中,可以通过注释行调用的命令或指针,是一种基于服务器端的网 ...
- linux mingling
grep 文本搜索工具 -i 忽略大小写 -v 不显示匹配行 -c 显示符合条件的行数值 文本搜索支持正则表达式 1 2 3 cat /etc/passwd | grep root // 显示包含ro ...
- win7使用自带资源管理器来登陆FTP
使用windows自带的资源管理器登陆FTP,其实很简单,打开计算机,在地址栏直接输入ftp://ftp.hostname即可. 但是这种方式需要每次输入ip地址,还要输入用户名和密码,比较麻烦.可能 ...