Java POI Excel 导入导出
这个东西很容易懂,不是特别难,难就难在一些复杂的计算和Excel格式的调整上。
近期写了一个小列子,放上来便于以后使用。
POI.jar下载地址:http://mirror.bit.edu.cn/apache/poi/release/bin/poi-bin-3.17-20170915.zip
Entity 实体类
package com.test2; import java.util.Date; public class User { private int id;
private String username;
private String password;
private String sex;
private int age;
private Date birth; public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public Date getBirth() {
return birth;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(int age) {
this.age = age;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", sex=" + sex + ", age=" + age
+ ", birth=" + birth + "]";
} public User() {};
public User(int id, String username, String password, String sex, int age, Date birth) {
super();
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
this.age = age;
this.birth = birth;
} }
Excel 导出:
package com.test2; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress; public class ExportExcel { public static void main(String[] args) {
try {
// 创建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 5);
HSSFCellStyle headStyle = getStyle(workbook,(short) 15);
HSSFCellStyle colStyle = getStyle(workbook,(short) 12); //新建一个excel页签
HSSFSheet createSheet = workbook.createSheet("用户信息列表");
// 将合并表格的对象添加页签中
createSheet.addMergedRegion(cellRangeAddress);
// 设置单元格的默认宽度
createSheet.setDefaultColumnWidth(25); // 创建一行
HSSFRow row0 = createSheet.createRow(0);
HSSFCell cell0 = row0.createCell(0);
// 添加标题样式
cell0.setCellStyle(headStyle);
// 添加标题
cell0.setCellValue("用户信息列表"); //设置列的标题
String [] titles = {"id","用户名","密码","年龄","性别","生日"};
HSSFRow row1 = createSheet.createRow(1);
// 循环往excel中添加列标题
for (int i = 0; i < titles.length; i++) {
HSSFCell cell1 = row1.createCell(i);
cell1.setCellStyle(colStyle);
cell1.setCellValue(titles[i]);
} List<User> userList = new ArrayList<User>(); userList.add(new User(1,"zhangsan1","123","男",21,new Date()));
userList.add(new User(2,"zhangsan2","456","男",21,new Date()));
userList.add(new User(3,"zhangsan3","789","女",21,new Date()));
userList.add(new User(4,"zhangsan4","000","男",21,new Date())); for (int i = 0; i < userList.size(); i++) {
//创建第三行
HSSFRow row2 = createSheet.createRow(i + 2); HSSFCell cell_0 = row2.createCell(0);
cell_0.setCellValue(userList.get(i).getId()); HSSFCell cell_1 = row2.createCell(1);
cell_1.setCellValue(userList.get(i).getUsername()); HSSFCell cell_2 = row2.createCell(2);
cell_2.setCellValue(userList.get(i).getPassword()); HSSFCell cell_3 = row2.createCell(3);
cell_3.setCellValue(userList.get(i).getAge()); HSSFCell cell_4 = row2.createCell(4);
cell_4.setCellValue(userList.get(i).getSex()); HSSFCell cell_5 = row2.createCell(5);
cell_5.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(userList.get(i).getBirth())); } OutputStream os = new FileOutputStream("E:/test.xls");
workbook.write(os);
os.close();
} catch (IOException e) {
e.printStackTrace();
} } public static HSSFCellStyle getStyle(HSSFWorkbook workbook,short fontSize) {
// 创建样式对象
HSSFCellStyle createCellStyle = workbook.createCellStyle();
//水平居中
createCellStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
createCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置边框
createCellStyle.setBorderBottom(BorderStyle.THIN);
createCellStyle.setBorderLeft(BorderStyle.THIN);
createCellStyle.setBorderRight(BorderStyle.THIN);
createCellStyle.setBorderTop(BorderStyle.THIN); // 创建一个字体对象
HSSFFont createFont = workbook.createFont();
// 设置字体的大小
createFont.setFontHeightInPoints(fontSize);
// 设置字体类型
createFont.setFontName("微软雅黑");
// 设置字体的颜色
createFont.setColor(HSSFColor.RED.index); //将字体放置到样式中
createCellStyle.setFont(createFont);
return createCellStyle;
} }
以下是导出Excel的结果图:
这里补充下合并单元格的知识点:
/*合并单元格需要用到CellRangeAddress对象
CellRangeAddress对象需要传入四个参数 列如:CellRangeAddress(param1,param2,param3,param4);
参数分别代表如下:
param1:开始行
param2:结束行
param3:开始列
param4:结束列
例如:
合并第一行和第二行,此合并只是合并了第一列,第二列、第三列...等后面的列的一二行并没有合并
给合并的单元格赋值是需要注意行号变化
注意:记住行号和列号是从0起始
*/
sheet.addMergedRegion(new CellRangeAddress(0,1,0,0));
下面粘贴一段合并单元格行和列的综合代码:
public static void main(String[] args) {
try {
// 创建标题栏
String[] titles = new String[] {"公司名称","项目名称","地址名称"};
String[] strDate = new String[] {"2018-04-29","2018-04-30","2018-05-01","2018-05-02","2018-05-03"}; // 创建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//拿到标题样式和单元格样式
HSSFCellStyle headStyle = getStyle(workbook,(short) 15);
HSSFCellStyle colStyle = getStyle(workbook,(short) 12); //新建一个excel页签
HSSFSheet createSheet = workbook.createSheet("地址信息报表");
// 将合并表格的对象添加页签中
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, (6+strDate.length));
createSheet.addMergedRegion(cellRangeAddress);
//循环合并1至3列的的一二行
for (int i = 0; i < 3; i++) {
createSheet.addMergedRegion(new CellRangeAddress(1, 2, i, i));
}
createSheet.addMergedRegion(new CellRangeAddress(1, 1, 3, 3+strDate.length));
// 设置单元格的默认宽度
createSheet.setDefaultColumnWidth(25); // 创建一行
HSSFRow row0 = createSheet.createRow(0);
HSSFCell cell0 = row0.createCell(0);
// 添加标题样式
cell0.setCellStyle(headStyle);
// 添加标题
cell0.setCellValue("地址信息报表"); //设置列的标题
HSSFRow row1 = createSheet.createRow(1);
// 循环往excel中添加列标题
for (int i = 0; i < titles.length; i++) {
HSSFCell cell1 = row1.createCell(i);
cell1.setCellStyle(colStyle);
cell1.setCellValue(titles[i]);
}
HSSFCell dateCell = row1.createCell(titles.length);
dateCell.setCellStyle(colStyle);
dateCell.setCellValue("2018-04-29 / 2018-05-03"); HSSFRow row2 = createSheet.createRow(2);
// 循环往excel中添加列标题
for (int i = 0; i < strDate.length; i++) {
HSSFCell cell1 = row2.createCell(i + titles.length);
cell1.setCellStyle(colStyle);
cell1.setCellValue(strDate[i]);
} OutputStream os = new FileOutputStream("E:/test.xls");
workbook.write(os);
os.close();
} catch (Exception e){
e.printStackTrace();
} }
结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABSUAAACaCAIAAADQN7q2AAAeh0lEQVR4nO3dX28jd73HcT8VnoavI3gmts41F0ZCOrR3qeDCgAQVQoi7EynnotIKC6kq2iVNWKjo0dLEbmJsKw5xsC1Ttiu62iXL71w49oznvz2255vPvF+K1DRxZif+/MaZj3/zp+Kcc86dnZ25KAcHB1m+nvy/euKerkhPnjx58uTJthY+m83+8Ic/nJ2dPX/+fDQa/ec//wk/5u3bt19++eXZ2dnf//737P9upDdv3vT7/c+ijEajnAuP9Pr165/+9KeNRuO9994bj8dv3rx5+fLl69evnz179t577zUajWfPnjnn/vnPfyYvx/80rpUXAAAAAGxFZf6fyEJysJDwrcBjIr+oZ6f9LXnhX3zxxdnZ2dnZWb/ff/fuXdzD3r59e35+/tlnn/3rX//Kv0rtdvvPf/6z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1FNsco8iBTPWSqh0z1kKkeMtVDpnpsZhrVtzMcTO7o24psjlHkQaZ6yFQPmeohUz1kqodM9djMNKpvZ5repm8LsjlGkQeZ6iFTPWSqh0z1kKkeMtVjM9P4+4GloW/rsTlGkQeZ6iFTPWSqh0z1kKkeMtVjM1P6Njw2xyjyIFM9ZKqHTPWQqR4y1UOmemxmWpnNZtPpdDKZjMfju7u70Wh0e3t7c3MzHA6vr68Hg0G/3+/1et1u9+rq6vLystPptNvti4uL4+PjcwAAAAAAEOX/AUJ/SUQX9wk6AAAAAElFTkSuQmCC" alt="" width="1164" height="136" />
Excel导入:
package com.test2; import java.io.FileInputStream;
import java.io.InputStream;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; /**
*
* @author zhangxiang
*
*/
public class ImportExcel { public static void main(String[] args) { try {
//创建输入流对象
InputStream inputStream = new FileInputStream("E:/test.xls");
// 创建工作波对象
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
// 创建user集合对象用于存储Excel导入的信息
List<User> userList = new ArrayList<User>(); // 是否能拿到excel页签
for (int i = 0; i < workbook.getNumberOfSheets(); i++) { HSSFSheet sheetAt = workbook.getSheetAt(i);
// 不存在就结束循环
if(null == sheetAt) {
continue;
} // 是否存在row
for (int j = 0; j < sheetAt.getPhysicalNumberOfRows(); j++) { // 拿到第 j+2 行 前两行是标题
HSSFRow row = sheetAt.getRow(2+j);
// 不存在row就结束循环
if(null == row) {
continue;
} // 存储一行的每个单元格拿到的值
User user = new User(); // 拿到第一个单元格的值 单元格从0开始
HSSFCell cell0 = row.getCell(0);
Float f = Float.parseFloat(cell0.toString());
user.setId(f.intValue()); // 第二个单元格的值
HSSFCell cell1 = row.getCell(1);
user.setUsername(cell1.toString()); //第三个
HSSFCell cell2 = row.getCell(2);
user.setPassword(cell2.toString()); //第四个
HSSFCell cell3 = row.getCell(3);
Float a = Float.parseFloat(cell3.toString());
user.setAge(a.intValue()); // 第五个
HSSFCell cell4 = row.getCell(4);
user.setSex(cell4.toString()); //第六个
HSSFCell cell5 = row.getCell(5);
user.setBirth(new SimpleDateFormat("yyyy-MM-dd").parse(cell5.toString())); userList.add(user);
}
}
// 输出信息 查看是否正确
System.out.println(userList.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
以下是导入Excel 的部分打印结果图:
以上代码直接就可以运行!!
Java POI Excel 导入导出的更多相关文章
- JAVA实现Excel导入/导出【转】
JAVA实现Excel导入/导出[转] POI的下载与安装 请到网站http://www.apache.org/dyn/closer.cgi/poi/右击超链接2.5.1.zip下载压缩包poi-bi ...
- java简易excel导入导出工具(封装POI)
Octopus 如何导入excel 如何导出excel github项目地址 Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文 ...
- poi excel导入导出
pom <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artif ...
- java jxl excel 导入导出的 总结(建立超链接,以及目录sheet的索引)
最近项目要一个批量导出功能,而且要生成一个单独的sheet页,最后后面所有sheet的索引,并且可以点击进入连接.网上搜索了一下,找到一个方法,同时把相关的excel导入导出操作记录一下!以便以后使用 ...
- java中excel导入\导出工具类
1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...
- 关于java的Excel导入导出之easypoi
导入easypoi相关jar包,这里的easypoi-base的包也可以不倒入,因为easypoi-web中有依赖easypoi-base会自动导入的 <!-- https://mvnrepos ...
- POI Excel 导入导出重点
HSSF是指2007年以前的,XSSF是指2007年版本以上的 这个还是比较好用的,这些总结来自Apache的官方向导的点点滴滴 详细的请参考http://poi.apache.org/spreads ...
- java poi excel导入模板设置下拉框
import org.apache.poi.hssf.usermodel.DVConstraint; import org.apache.poi.hssf.usermodel.HSSFCell; im ...
- java poi Excel导入 整数浮点数转换问题解决
/** * 获取单元格数据 */ protected static String getCellValue(Cell cell) { String cellValu ...
随机推荐
- ios集合
Foundation framework中用于收集cocoa对象(NSObject对象)的三种集合分别是: NSArray 用于对象有序集合(数组) NSSet 用于对象无序集合 (集合) NSDic ...
- 【Codeforces】 Round #374 (Div. 2)
Position:http://codeforces.com/contest/721 我的情况 开始还是rank1,秒出C.(11:00机房都走光了,我ma到11:05才走,只打了一个小时) 结果.. ...
- HTML5浏览器嵌入窗口程序解决方案
浏览器嵌入窗口程序一直以来就是WEB应用程序的首选方案,这种方案只需要实现一个主窗口,并提供一些接口供脚本调用,内部的界面和逻辑处理全部用html,css,javascript去实现.我最早看到的相关 ...
- 【POJ 1328】 Radar Installation
[题目链接] http://poj.org/problem?id=1328 [算法] 每个雷达都位于笛卡尔坐标系的x轴上,因此,对于每个岛屿,我们都可以用勾股定理算出它的有效管辖区域 那么,问题就被转 ...
- java调用restful接口的方法
Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法如下: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring的RestTemplate
- PCB genesis方槽加内角槽孔实现方法
一.为什么方槽孔加内角孔 如下图,客户来的方槽或Slot槽有内角尺寸要求,通常直接钻一个Slot槽孔内角是不能满足客户要求的,这时我们做CAM的需采用小钻刀进行处理.加内角孔或内角槽的方式进行处理了. ...
- Gold Coins
http://poj.org/problem?id=2000 #include<stdio.h> ; int main() { int coin[N]; ,j,k; j = ; k = ; ...
- [ Luogu Contest 10364 ] TG
\(\\\) \(\#A\) 小凯的数字 给出两个整数\(L,R\),从\(L\)到\(R\)按顺序写下来,求生成整数对\(9\)取模后的答案. 例如\(L=8,R=12\),生成的数字是\(8910 ...
- 升级Xcode或 MacOS编译iOS出现resource fork, Finder information, or similar detritus not allowed
很久没有在网上留下足迹了,冒个泡吧 最近升级了Xcode,编译之前的一个项目是出现问题,问题结尾如下: resource fork, Finder information, or similar de ...
- 学习笔记-java IO流总结 转载
1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...