poi 导入导出excel
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*; import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Excel工具类
* 创建Sheet, 填充数据
* created : 2016/3/18.
*/
public class ExcelBuilder { /**
* 创建只包含标题列的sheet表
*
* @param wb
* @param sheetName
* @param titleNames
* @return
*/
public static HSSFSheet createExcelSheetWithTitle(HSSFWorkbook wb, String sheetName, List<String> titleNames) {
if (BlankUtil.isNotEmpty(titleNames)) {
return createExcelSheetWithTitle(wb, sheetName, titleNames.toArray(new String[titleNames.size()]));
}
return createExcelSheetWithTitle(wb, sheetName, new String[]{});
} /**
* 创建只包含标题列的sheet表
*
* @param wb
* @param sheetName
* @param titleNames
* @return
*/
public static HSSFSheet createExcelSheetWithTitle(HSSFWorkbook wb, String sheetName, String[] titleNames) {
if (BlankUtil.isEmpty(sheetName)) {//如果没传sheetName,给定默认值
sheetName = "Sheet";
}
HSSFSheet sheet = wb.createSheet(sheetName);
sheet.setDefaultColumnWidth(20);//设置默认列宽
sheet.setDefaultRowHeight((short) 300);//设置默认行高
if (BlankUtil.isNotEmpty(titleNames)) {//设置Sheet首行列标题
Row row = sheet.createRow(0);
Cell cell = null;
for (int i = 0; i < titleNames.length; i++) {
cell = row.createCell(i);
cell.setCellValue(titleNames[i]);
}
}
return sheet;
} /**
* 创建包含标题列的sheet, 并填充数据
*
* @param wb
* @param sheetName
* @param titleNames
* @param datas
* @return
*/
public static HSSFSheet createExcelSheetWithTitleAndData(
HSSFWorkbook wb, String sheetName, List<String> titleNames, List<Map<String, Object>> datas) {
if (BlankUtil.isNotEmpty(titleNames) && BlankUtil.isNotEmpty(datas)) {
return createExcelSheetWithTitleAndData(wb, sheetName, titleNames.toArray(new String[titleNames.size()]), datas);
}
return createExcelSheetWithTitle(wb, sheetName, new String[]{});
} /**
* 创建包含标题列的sheet, 并填充数据
*
* @param wb
* @param sheetName
* @param titleNames
* @param datas
* @return
*/
public static HSSFSheet createExcelSheetWithTitleAndData(
HSSFWorkbook wb, String sheetName, String[] titleNames, List<Map<String, Object>> datas) {
HSSFSheet sheet = createExcelSheetWithTitle(wb, sheetName, titleNames);//创建包含标题列的空sheet
fillDataToSheet(sheet, datas);//给sheet填充数据
return sheet;
} /**
* 向sheet中填充数据行
*
* @param sheet
* @param datas
*/
public static HSSFSheet fillDataToSheet(HSSFSheet sheet, List<Map<String, Object>> datas) {
if (sheet != null && BlankUtil.isNotEmpty(datas)) {
Row row = null;
Cell cell = null;
Map<String, Object> rowDataMap = null;
Object columnValue = null;
//第一行是标题行, 所以从第二行开始插入数据
for (int rowIndex = 0; rowIndex < datas.size(); rowIndex++) {
row = sheet.createRow(rowIndex + 1);//从第二行开始
rowDataMap = datas.get(rowIndex);
int columnIndex = 0;
for (String key : rowDataMap.keySet()) {
cell = row.createCell(columnIndex);
columnValue = rowDataMap.get(key);
columnIndex++;
if (columnValue == null) {
continue;
}
if (columnValue instanceof Integer) {
cell.setCellValue((Integer) columnValue);
} else if (columnValue instanceof Long) {
cell.setCellValue((Long) columnValue);
} else {
cell.setCellValue(String.valueOf(columnValue));
}
}
}
}
return sheet;
} /***
* 解析excel并封装成list
*
* @param inputStream
* @return
*/
public static List<Map<String, String>> readExcelandToList(InputStream inputStream) {
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
BufferedInputStream bis = null;
// 获得一个特定的工作薄
try {
bis = new BufferedInputStream(inputStream);
Workbook workbook = WorkbookFactory.create(bis);
// 循环获得每一个sheet(只处理第一个sheet)
for (int numSheets = 0; workbook != null
&& numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (numSheets != 0) {
break;
}
// 获得一个sheet
Sheet sheet = workbook.getSheetAt(numSheets);
if (null == sheet) {
continue;
}
// 循环每一行
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row aRow = sheet.getRow(rowNum);
if (null == aRow) {
continue;
}
Map<String, String> datum = new HashMap<String, String>();
// Map<String, String> datum =new LinkedHashMap<String, String>(); // 循环特定行的每一列
for (short cellNum = 0; cellNum < aRow.getLastCellNum(); cellNum++) {
// 得到特定单元格
Cell aCell = aRow.getCell(cellNum);
if (aCell == null) {
continue;
}
String cellValue = "";
if(aCell != null) {
if(aCell.getCellTypeEnum() == CellType.NUMERIC) {
if(DateUtil.isCellDateFormatted(aCell)) {
cellValue = new DataFormatter().formatCellValue(aCell);
} else {
cellValue = String.valueOf(aCell.getNumericCellValue());
}
} else {
cellValue = aCell.getRichStringCellValue().toString();
}
}
datum.put("cell" + cellNum, cellValue.trim());
}
data.add(datum);
}
} bis.close();
if (inputStream != null) {
inputStream.close();
}
return data;
} catch (Exception e) {
e.printStackTrace();
if (bis != null) {
try {
bis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
throw e;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
return null;
}
}
} /***
* 解析excel并封装成list,Map中包含列名
*
* @param inputStream
* @return
*/
public static List<Map<String, String>> readExcelToList(InputStream inputStream) {
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
BufferedInputStream bis = null;
// 获得一个特定的工作薄
try {
bis = new BufferedInputStream(inputStream);
Workbook workbook = WorkbookFactory.create(bis);
// 循环获得每一个sheet(只处理第一个sheet)
for (int numSheets = 0; workbook != null
&& numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (numSheets != 0) {
break;
}
// 获得一个sheet
Sheet sheet = workbook.getSheetAt(numSheets);
if (null == sheet) {
continue;
}
//得到表头
Row tRow = sheet.getRow(0);
List<String> cellName = new ArrayList<>();
for (short cellNum = 0; cellNum < tRow.getLastCellNum(); cellNum++) {
Cell aCell = tRow.getCell(cellNum);
if (aCell == null) {
continue;
}
aCell.setCellType(Cell.CELL_TYPE_STRING);
String cellValue = (aCell == null ? null : aCell.getRichStringCellValue().toString());
cellName.add(cellValue.trim());
}
// 循环每一行
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row aRow = sheet.getRow(rowNum);
if (null == aRow) {
continue;
}
Map<String, String> datum = new HashMap<String, String>();
// 循环特定行的每一列
for (short cellNum = 0; cellNum < aRow.getLastCellNum(); cellNum++) {
// 得到特定单元格
Cell aCell = aRow.getCell(cellNum);
if (aCell == null) {
continue;
}
aCell.setCellType(Cell.CELL_TYPE_STRING);
String cellValue = (aCell == null ? null : aCell.getRichStringCellValue().toString());
datum.put(cellName.get(cellNum), cellValue.trim());
}
data.add(datum);
}
}
bis.close();
if (inputStream != null) {
inputStream.close();
}
return data;
} catch (Exception e) {
e.printStackTrace();
if (bis != null) {
try {
bis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
throw e;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
return null;
}
}
}
}
poi 导入导出excel的更多相关文章
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- Java POI导入导出Excel
1.异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 解决方法: 使用的poi的相关jar ...
- java中使用poi导入导出excel文件_并自定义日期格式
Apache POI项目的使命是创造和保持java API操纵各种文件格式基于Office Open XML标准(OOXML)和微软的OLE复合文档格式(OLE2)2.总之,你可以读写Excel文件使 ...
- POI导入导出excel(附工具类)
关于POI导出excel的功能我在前面的文章已经写过了,POI导出excel的三种方式 , 导出表格数据到excel并下载(HSSFWorkbook版) ,本篇文章主要是将导入导出功能进一步地封装,在 ...
- POI导入导出Excel(HSSF格式,User Model方式)
1.POI说明 Apache POI是Apache软件基金会的开源代码库, POI提供对Microsoft Office格式档案读和写的功能. POI支持的格式: HSSF - 提供读写Microso ...
- SpringMvc 使用poi导入导出Excel
Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...
- java poi 导入导出Excel xsl xslx
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- 导入导出Excel工具类ExcelUtil
前言 前段时间做的分布式集成平台项目中,许多模块都用到了导入导出Excel的功能,于是决定封装一个ExcelUtil类,专门用来处理Excel的导入和导出 本项目的持久化层用的是JPA(底层用hibe ...
- 导入导出Excel的Java工具类ExcelUtil
在编写ExcelUtil之前,在网上查了一些资料.java中用来处理Excel的第三方开源项目主要就是POI和JXL.poi功能强大,但是比较耗资源,对于大数据量的导入导出性能不是太好:jxl功能简单 ...
随机推荐
- 理解linux 块, i节点
https://blog.csdn.net/zdf19/article/details/54424880 https://www.cnblogs.com/hnrainll/archive/2012/0 ...
- Go语言图形界面开发:Go版GTK
https://www.cnblogs.com/tennysonsky/p/8433888.html package main import ( "os" "github ...
- feign的callback设定后,项目启动错误
错误如下: Error starting ApplicationContext. To display the auto-configuration report re-run your applic ...
- Connect to a ROS Network---2
原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ 一.Introduction ROS网络由单个ROS主机和多个ROS节点组成. ROS ...
- Java从控制台接受输入字符
创建一个类,在该类的主方法中创建Scanner扫描起来封装System类的in输入流,然后提示用户输入身份证号码,并输入身份证号码的位数. 代码如下: import java.util.Scanner ...
- POJ 3273 Monthly Expense(二分搜索)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- CentOS 6.3下搭建Web服务器
准备前的工作: 1.修改selinux配置文件(/etc/sysconfig/selinux) 关闭防火墙 (1)把SELINUX=enforcing注释掉 (2)并添加SELINUX=disable ...
- 免费SVN、Git项目托管主机推荐
Unfuddle 200MB的免费空间,界面友好,特性丰富,支持Git,但只能一个账户一个用户并且只允许一个项目,付费服务相对来说价格偏高 CodeSpaces 500MB,一个账户两个免费用户,付费 ...
- java 递归
package com.j1.soa.resource.cms.service.oracle; import com.j1.base.dto.ServiceMessage; import com.j1 ...
- Linux怎样创建FTP服务器--修改用户默认目录
在创建FTP服务器之有先命令: ps -ef |grep vsftpd 查一下系统有没有安装vsftpd这个服务器,如果出现如下图所示的界面说明没有安装. 然后再执行:yum install ...