基于POI的Excel导入导出(JAVA实现)
今天做了个excel的导入导出功能,在这记录下。
首先现在相关poi的相关jar包,资源链接:http://download.csdn.net/detail/opening_world/9663247
具体过程就不多说了,直接上代码吧。
导出excel代码:
public void export2007(HttpServletResponse response, List<List<Object>> list,String filename,String[] title){
String[] header = title;
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(filename);
XSSFRow row = sheet.createRow((int) 0);
XSSFCellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 11);
font.setFontName("宋体");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFillForegroundColor(HSSFColor.GREY_80_PERCENT.index);
style.setFont(font);
XSSFCell cell = null;
for(int i=0;i<header.length;i++){
cell = row.createCell((short) i);
cell.setCellValue(header[i]);
cell.setCellStyle(style);
}
if(list == null){
return;
}
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
List<Object> clist = list.get(i);
for(int n=0;n<clist.size();n++) {
Object value = clist.get(n);
if(value instanceof Date){
row.createCell((short)n).setCellValue(fmt.format(value));
}else{
row.createCell((short)n).setCellValue(clist.get(n).toString());
}
}
}
try
{
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;filename=\"" + java.net.URLEncoder.encode(filename, "UTF-8") + ".xlsx" + "\" ");
wb.write(response.getOutputStream());
response.getOutputStream().close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
参数解释:
response : 响应对象,用于直接返回给浏览器。
list: 内容数据,遍历填充单元格。
filename: 文件名。
title: excel第一行的标题数组。
导出2003版excel文件与上面代码相似,只需使用另外一套类就行,感觉现在基本都是2007版了吧。
解析excel代码:
protected void readXls(InputStream is) throws IOException, InvalidFormatException {
Workbook hssfWorkbook = WorkbookFactory.create(is);
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
Row hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
/**已经直接取数据行,无需判定
if(hssfRow.getCell(0) == null ){
continue;
}else if(hssfRow.getCell(0).getCellType() == Cell.CELL_TYPE_STRING){
String value = hssfRow.getCell(0).getStringCellValue();
try{
Integer.parseInt(value);
}catch(Exception e){
continue;
}
}
*/
Map<String, Object> map = new HashMap<String, Object>();
map.put("jgId", getValue(hssfRow.getCell(1)));
map.put("name", getValue(hssfRow.getCell(3)));
map.put("cardType", getValue(hssfRow.getCell(4)).split("-")[0]);
map.put("cardNo", getValue(hssfRow.getCell(5)));
map.put("sex", getValue(hssfRow.getCell(6)).split("-")[0]);
map.put("birth", getValue(hssfRow.getCell(7)));
}
}
}
}
传入的参数是文件流InputStream,根据文件类型自动识别解析,因此.xls和.xlsx两种格式都能解析,示例代码是将解析的数据存入map,这方面可以根据具体业务进行修改。
操作其实很简单,有什么问题欢迎留言交流。
基于POI的Excel导入导出(JAVA实现)的更多相关文章
- SpringBoot集成文件 - 集成POI之Excel导入导出
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能.本文主要介绍通过Spr ...
- Java之POI的excel导入导出
一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...
- 如何自动化你的Excel导入导出(Java)?
GitHub | 中文 | English | 博客 为什么使用AutoExcel? Excel导入导出在软件开发中非常常见,只要你接触过开发,就一定会遇到.相信很多人会跟我一样选择用Apache P ...
- 【原创】POI操作Excel导入导出工具类ExcelUtil
关于本类线程安全性的解释: 多数工具方法不涉及共享变量问题,至于添加合并单元格方法addMergeArea,使用ThreadLocal变量存储合并数据,ThreadLocal内部借用Thread.Th ...
- Spring Boot学习笔记----POI(Excel导入导出)
业务:动态生成模板导出Excel,用户修改完再导入Excel. Spring boot + bootstrap + poi 1.添加Dependence <dependency> < ...
- .net core 基于NPOI 的excel导入导出类,支持自定义导出哪些字段,和判断导入是否有失败的记录
#region 从Excel导入 //用法 //var cellHeader = new Dictionary<string, string>(); //cellHeader.Add(&q ...
- 基于NPOI的Excel导入导出类库
概述 支持多sheet导入导出.导出字段过滤.特性配置导入验证,非空验证,唯一验证,错误标注等 用于基础配置和普通报表的导入导出,对于复杂需求,比如合并列,公式,导出图片等暂不支持 GitHub地址: ...
- POI实现Excel导入导出
我们知道要创建一张excel你得知道excel由什么组成,比如说sheet也就是一个工作表格,例如一行,一个单元格,单元格格式,单元格内容格式…这些都对应着poi里面的一个类. 一个excel表格: ...
- apache POI 操作excel<导入导出>
1.首先导入maven依赖 <!-- POI核心依赖 --> <dependency> <groupId>org.apache.poi</groupId> ...
随机推荐
- sort函数使用的基本知识
STL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:sort(begin,en ...
- APUE读书笔记:进程控制
重点函数:fork,exit,_exit 一.fork 函数原型: #include <unistd.> pid_t fork(void) 函数说明:fork函数将创建一个子进程,该函数调 ...
- Gym 100917C Constant Ratio 数论+暴力
题目: Description standard input/outputStatements Given an integer n, find out number of ways to repre ...
- static方法与非static方法是否可以互相调用
情况一.static方法调用非static方法 非静态方法只有实例对象才可调用,而静态方法随着类的加载而加载,类的加载在实例对象产生之前,所以静态方法不能调用非静态方法 情况二.非atic方法调用st ...
- sonar tomacat配置
最近在学习Sonar,配置了好几天,才搭建起来环境,为自己的学习能力感到汗颜,赶紧在此记录一下,所谓好记性不如烂笔头. 1.Sonar介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java ...
- HDU 4460 Friend Chains(map + spfa)
Friend Chains Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- java 文件字节输入流
Example10_4.java import java.io.*; public class Example10_4 { public static void main(String args[]) ...
- 转:selenium 并行启动多个浏览器
https://github.com/fool2fish/selenium-doc/blob/master/official-site/selenium-grid.md Selenium Grid 快 ...
- LINUX中磁盘挂载与卸除
一.挂载格式与参数说明: 要将文件系统挂载到我们的 Linux 系统上,就要使用 mount 这个命令啦! 不过,这个命令真的是博大精深-粉难啦!我们学简单一点啊- ^_^ [root@www ~]# ...
- Telepro工具注册码
Teleport Pro v1.54 注册码 Teleport Pro v1.54姓名(Name):3ddown.com序列号(Serial):161594593