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功能简单 ...
随机推荐
- jQuery时间格式转换
http://www.cnblogs.com/ShaYeBlog/p/4129301.html
- linux 按照端口一句命令杀死进程,按照进程名称一句命令杀死进程
例如杀死nginx 按照程序名称杀死进程 例如杀死nginx的进程 ps -aux|grep nginx|grep -v grep|cut -c 9-15|xargs kill -9 或者 ps -a ...
- 禁止requests请求https的提示InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more
提示这个 InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from ...
- 【linux】 linux 禁止ping
linux 禁止 ping 一.修改内核参数 1.临时允许PING操作的命令为:echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all 2.永久允许PIN ...
- Eclipse------导入项目后出现Java compiler level does not match the version of the installed Java project facet
报错信息:Java compiler level does not match the version of the installed Java project facet 解决方法: 1.点击工具 ...
- Oracle的动态SQL
例1:传递表名,和Where条件删除数据 CREATE OR REPLACE PROCEDURE raise_emp_salary (column_value NUMBER, emp_column V ...
- uploadify在火狐下上传不了的解决方式,java版(Spring+SpringMVC+MyBatis)具体解决方式
因为技术选型的原因,在一个产品中.我选择了uploadify,选择它的原因是它有完好的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是 ...
- JSP面试知识
JSP方面 1. JSP四种范围是什么?区别是什么? Page:指单单一页jsp page的范围: Request:的范围只在一jsp页发出请求到另一页之间,随后这个属性失效: Session:范围是 ...
- UDP通信-UdpClient
static void Main(string[] args) { Console.WriteLine("发送端"); byte[] buffer = System.Text.En ...
- Python生成器笔记
Python中三大器有迭代器,生成器,装饰器,本文主要讲述生成器.主要从生成器的概念,本质,以及yield关键字的使用执行过程. 本质:生成器是一类特殊的迭代器,使用了yield关键字的函数不再是函数 ...