poi导出获取不到第二个工作表,但是poi模板导出里有四个工作表怎么办?

Access denied | www.supmen.com used Cloudflare to restrict access
Please enable cookies.
What happened?
The owner of this website (www.supmen.com) has banned your access based on your browser's signature (3fc51b2f46fe516a-ua98).如何用Java实现在一个工作薄中创建多个工作表_百度知道
如何用Java实现在一个工作薄中创建多个工作表
我有更好的答案
workbook.createSheet(工作簿名称);create几次就有几个workbook是org.apache.poi.ss.usermodel.Workbook对象具体API请google POI 参考官方文档
采纳率:17%
楼主可以去看下apache的一个开源插件----poi
为您推荐:
其他类似问题
您可能关注的内容
工作表的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。使用Apache POI导出Excel小结--导出XLS格式文档 - 简书
使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结
关于使用Apache POI导出Excel我大概会分三篇文章去写
导出XLS格式文档
做企业应用项目难免会有数据导出到Excel的需求,最近在使用其,并对导出Excel封装成工具类开放出来供大家参考。关于Apache POI Excel基本的概念与操作我在这里就不啰嗦了。
请大家参考如下:
在使用Apache POI导出Excel有以下限制:
Excel &=2003 数据限制,行(65536)*列(256)
Excel =2007 数据限制,行(1048576)*列(16384)
POI结构说明
包名称 说明
HSSF提供读写Microsoft Excel XLS格式档案的功能。
XSSF提供读写Microsoft Excel OOXML XLSX格式档案的功能。
SXSSF提供基于流式读写Microsoft Excel OOXML XLSX格式档案的功能(适用于大数据量)。
POI常用类说明
以HSSF为例其他的只是前缀命名变动
如HSSF--&HSSFWorkbook、XSSF--&XSSFWorkbook、SXSSF--&SXSSFWorkbook
HSSFWorkbook
Excel的文档对象
Excel的表单
Excel的格子单元
HSSFDataFormat
格子单元的日期格式
HSSFHeader
Excel文档Sheet的页眉
HSSFFooter
Excel文档Sheet的页脚
HSSFCellStyle
格子单元样式
HSSFDateUtil
HSSFPrintSetup
HSSFErrorConstants
错误信息表
下来给大家依次展现代码:
Maven依赖配置
使用的Apache POI版本
&poi.version&3.9&/poi.version&
&dependency&
&groupId&org.apache.poi&/groupId&
&artifactId&poi&/artifactId&
&version&${poi.version}&/version&
&/dependency&
&dependency&
&groupId&org.apache.poi&/groupId&
&artifactId&poi-ooxml&/artifactId&
&version&${poi.version}&/version&
&/dependency&
&dependency&
&groupId&org.apache.poi&/groupId&
&artifactId&poi-ooxml-schemas&/artifactId&
&version&${poi.version}&/version&
&/dependency&
生成XLS格式Excel文档
使用于小数据量生成
受Excel XLS格式文档本身限制(XLS格式文档大约处理最大数据行[65536条])
基于Excel模板写入数据会引发内存溢出
以下代码少ReportInternalException大家可以忽略(我们封装的一个异常类)
导出的Excel同时考虑到数据的本身类型,如整数、小数、日期等
第一种写入数据方式[writeExcel]方法为直接写入数据
第二种写入数据方式需依次调用方法[writeExcelTitle、writeExcelData],先完成写入Excel标题与列名,再完成数据写入(或者说基于模板方式写入数据)
第二种方式有内存溢出的可能性
我们使用[styleMap]方法避免重复创建Excel单元格样式(否则受Excel创建样式数量限制)
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFC
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeA
import javax.imageio.ImageIO;
import java.io.*;
import java.text.SimpleDateF
import java.util.D
import java.util.LinkedHashM
import java.util.L
import java.util.M
* Excel 相关操作类(小数据量写入&=65536)
public class Excel2003Utils {
private static final int DEFAULT_COLUMN_SIZE = 30;
* 断言Excel文件写入之前的条件
* @param directory 目录
* @param fileName
* @return file
* @throws IOException
private static File assertFile(String directory, String fileName) throws IOException {
File tmpFile = new File(directory + File.separator + fileName + ".xls");
if (tmpFile.exists()) {
if (tmpFile.isDirectory()) {
throw new IOException("File '" + tmpFile + "' exists but is a directory");
if (!tmpFile.canWrite()) {
throw new IOException("File '" + tmpFile + "' cannot be written to");
File parent = tmpFile.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
return tmpF
* 日期转化为字符串,格式为yyyy-MM-dd HH:mm:ss
private static String getCnDate(Date date) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
* Excel 导出,POI实现
* @param fileName
* @param sheetName
sheet页名称
* @param columnNames 表头列表名
* @param sheetTitle
sheet页Title
* @param objects
目标数据集
public static File writeExcel(String directory, String fileName, String sheetName, List&String& columnNames,
String sheetTitle, List&List&Object&& objects, boolean append) throws ReportInternalException, IOException {
File tmpFile = assertFile(directory, fileName);
return exportExcel(tmpFile, sheetName, columnNames, sheetTitle, objects, append);
* Excel 导出,POI实现,先写入Excel标题,与writeExcelData配合使用
* 先使用writeExcelTitle再使用writeExcelData
* @param directory
* @param fileName
* @param sheetName
* @param columnNames 列名集合
* @param sheetTitle
* @param append
是否在现有的文件追加
* @return file
* @throws ReportInternalException
* @throws IOException
public static File writeExcelTitle(String directory, String fileName, String sheetName, List&String& columnNames,
String sheetTitle, boolean append) throws ReportInternalException, IOException {
File tmpFile = assertFile(directory, fileName);
return exportExcelTitle(tmpFile, sheetName, columnNames, sheetTitle, append);
* Excel 导出,POI实现,写入Excel数据行列,与writeExcelTitle配合使用
* 先使用writeExcelTitle再使用writeExcelData
* @param directory 目录
* @param fileName
* @param sheetName sheetName
* @param objects
* @return file
* @throws ReportInternalException
* @throws IOException
public static File writeExcelData(String directory, String fileName, String sheetName, List&List&Object&& objects)
throws ReportInternalException, IOException {
File tmpFile = assertFile(directory, fileName);
return exportExcelData(tmpFile, sheetName, objects);
* 导出字符串数据
* @param file
* @param columnNames 表头
* @param sheetTitle
sheet页Title
* @param append
是否追加写文件
* @return file
* @throws ReportInternalException
private static File exportExcelTitle(File file, String sheetName, List&String& columnNames,
String sheetTitle, boolean append) throws ReportInternalException, IOException {
// 声明一个工作薄
Workbook workB
if (file.exists() && append) {
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
workBook = new HSSFWorkbook();
Map&String, CellStyle& cellStyleMap = styleMap(workBook);
// 表头样式
CellStyle headStyle = cellStyleMap.get("head");
// 生成一个表格
Sheet sheet = workBook.getSheet(sheetName);
if (sheet == null) {
sheet = workBook.createSheet(sheetName);
//最新Excel列索引,从0开始
int lastRowIndex = sheet.getLastRowNum();
if (lastRowIndex & 0) {
lastRowIndex++;
// 设置表格默认列宽度
sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(lastRowIndex, lastRowIndex, 0, columnNames.size() - 1));
// 产生表格标题行
Row rowMerged = sheet.createRow(lastRowIndex);
lastRowIndex++;
Cell mergedCell = rowMerged.createCell(0);
mergedCell.setCellStyle(headStyle);
mergedCell.setCellValue(new HSSFRichTextString(sheetTitle));
// 产生表格表头列标题行
Row row = sheet.createRow(lastRowIndex);
for (int i = 0; i & columnNames.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(headStyle);
RichTextString text = new HSSFRichTextString(columnNames.get(i));
cell.setCellValue(text);
OutputStream ops = new FileOutputStream(file);
workBook.write(ops);
ops.flush();
ops.close();
} catch (IOException e) {
throw new ReportInternalException(e);
* 导出字符串数据
* @param file
* @param objects 目标数据
* @throws ReportInternalException
private static File exportExcelData(File file, String sheetName, List&List&Object&& objects) throws ReportInternalException, IOException {
// 声明一个工作薄
Workbook workB
if (file.exists()) {
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
workBook = new HSSFWorkbook();
Map&String, CellStyle& cellStyleMap = styleMap(workBook);
// 正文样式
CellStyle contentStyle = cellStyleMap.get("content");
//正文整数样式
CellStyle contentIntegerStyle = cellStyleMap.get("integer");
//正文带小数整数样式
CellStyle contentDoubleStyle = cellStyleMap.get("double");
// 生成一个表格
Sheet sheet = workBook.getSheet(sheetName);
if (sheet == null) {
sheet = workBook.createSheet(sheetName);
//最新Excel列索引,从0开始
int lastRowIndex = sheet.getLastRowNum();
if (lastRowIndex & 0) {
lastRowIndex++;
// 设置表格默认列宽度
sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);
// 遍历集合数据,产生数据行,前两行为标题行与表头行
for (List&Object& dataRow : objects) {
Row row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int j = 0; j & dataRow.size(); j++) {
Cell contentCell = row.createCell(j);
Object dataObject = dataRow.get(j);
if (dataObject != null) {
if (dataObject instanceof Integer) {
contentCell.setCellStyle(contentIntegerStyle);
contentCell.setCellValue(Integer.parseInt(dataObject.toString()));
} else if (dataObject instanceof Double) {
contentCell.setCellStyle(contentDoubleStyle);
contentCell.setCellValue(Double.parseDouble(dataObject.toString()));
} else if (dataObject instanceof Long && dataObject.toString().length() == 13) {
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate(new Date(Long.parseLong(dataObject.toString()))));
} else if (dataObject instanceof Date) {
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate((Date) dataObject));
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(dataObject.toString());
contentCell.setCellStyle(contentStyle);
// 设置单元格内容为字符型
contentCell.setCellValue("");
OutputStream ops = new FileOutputStream(file);
workBook.write(ops);
ops.flush();
ops.close();
} catch (IOException e) {
throw new ReportInternalException(e);
* 导出字符串数据
* @param file
* @param columnNames 表头
* @param sheetTitle
sheet页Title
* @param objects
* @param append
是否追加写文件
* @throws ReportInternalException
private static File exportExcel(File file, String sheetName, List&String& columnNames,
String sheetTitle, List&List&Object&& objects, boolean append) throws ReportInternalException, IOException {
// 声明一个工作薄
Workbook workB
if (file.exists() && append) {
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
workBook = new HSSFWorkbook();
Map&String, CellStyle& cellStyleMap = styleMap(workBook);
// 表头样式
CellStyle headStyle = cellStyleMap.get("head");
// 正文样式
CellStyle contentStyle = cellStyleMap.get("content");
//正文整数样式
CellStyle contentIntegerStyle = cellStyleMap.get("integer");
//正文带小数整数样式
CellStyle contentDoubleStyle = cellStyleMap.get("double");
// 生成一个表格
Sheet sheet = workBook.getSheet(sheetName);
if (sheet == null) {
sheet = workBook.createSheet(sheetName);
//最新Excel列索引,从0开始
int lastRowIndex = sheet.getLastRowNum();
if (lastRowIndex & 0) {
lastRowIndex++;
// 设置表格默认列宽度
sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(lastRowIndex, lastRowIndex, 0, columnNames.size() - 1));
// 产生表格标题行
Row rowMerged = sheet.createRow(lastRowIndex);
lastRowIndex++;
Cell mergedCell = rowMerged.createCell(0);
mergedCell.setCellStyle(headStyle);
mergedCell.setCellValue(new HSSFRichTextString(sheetTitle));
// 产生表格表头列标题行
Row row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int i = 0; i & columnNames.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(headStyle);
RichTextString text = new HSSFRichTextString(columnNames.get(i));
cell.setCellValue(text);
// 遍历集合数据,产生数据行,前两行为标题行与表头行
for (List&Object& dataRow : objects) {
row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int j = 0; j & dataRow.size(); j++) {
Cell contentCell = row.createCell(j);
Object dataObject = dataRow.get(j);
if (dataObject != null) {
if (dataObject instanceof Integer) {
contentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
contentCell.setCellStyle(contentIntegerStyle);
contentCell.setCellValue(Integer.parseInt(dataObject.toString()));
} else if (dataObject instanceof Double) {
contentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
contentCell.setCellStyle(contentDoubleStyle);
contentCell.setCellValue(Double.parseDouble(dataObject.toString()));
} else if (dataObject instanceof Long && dataObject.toString().length() == 13) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate(new Date(Long.parseLong(dataObject.toString()))));
} else if (dataObject instanceof Date) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate((Date) dataObject));
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(dataObject.toString());
contentCell.setCellStyle(contentStyle);
// 设置单元格内容为字符型
contentCell.setCellValue("");
OutputStream ops = new FileOutputStream(file);
workBook.write(ops);
ops.flush();
ops.close();
} catch (IOException e) {
throw new ReportInternalException(e);
* 创建单元格表头样式
* @param workbook 工作薄
private static CellStyle createCellHeadStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 表头样式
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
* 创建单元格正文样式
* @param workbook 工作薄
private static CellStyle createCellContentStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 正文样式
style.setFillPattern(HSSFCellStyle.NO_FILL);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
* 单元格样式(Integer)列表
private static CellStyle createCellContent4IntegerStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 正文样式
style.setFillPattern(HSSFCellStyle.NO_FILL);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));//数据格式只显示整数
* 单元格样式(Double)列表
private static CellStyle createCellContent4DoubleStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 正文样式
style.setFillPattern(HSSFCellStyle.NO_FILL);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));//保留两位小数点
* 单元格样式列表
private static Map&String, CellStyle& styleMap(Workbook workbook) {
Map&String, CellStyle& styleMap = new LinkedHashMap&&();
styleMap.put("head", createCellHeadStyle(workbook));
styleMap.put("content", createCellContentStyle(workbook));
styleMap.put("integer", createCellContent4IntegerStyle(workbook));
styleMap.put("double", createCellContent4DoubleStyle(workbook));
return styleM
import java.io.IOE
import java.sql.D
import java.util.LinkedL
import java.util.L
* Excel2003Test
* Created by jianwei.zhou on .
public class Excel2003Test {
public static void main(String[] args) throws IOException {
String sheetName = "测试Excel格式";
String sheetTitle = "测试Excel格式";
List&String& columnNames = new LinkedList&&();
columnNames.add("日期-String");
columnNames.add("日期-Date");
columnNames.add("时间戳-Long");
columnNames.add("客户编码");
columnNames.add("整数");
columnNames.add("带小数的正数");
//写入标题--第二种方式
Excel2003Utils.writeExcelTitle("E:\\temp", "a", sheetName, columnNames, sheetTitle, false);
List&List&Object&& objects = new LinkedList&&();
for (int i = 0; i & 1000; i++) {
List&Object& dataA = new LinkedList&&();
dataA.add(" 17:27:25");
dataA.add(new Date(2L));
dataA.add(2L);
dataA.add("000628");
dataA.add(i);
dataA.add(1.323 + i);
objects.add(dataA);
//写入数据--第二种方式
Excel2003Utils.writeExcelData("E:\\temp", "a", sheetName, objects);
//直接写入数据--第一种方式
Excel2003Utils.writeExcel("E:\\temp", "a", sheetName, columnNames, sheetTitle, objects, false);
} catch (Exception e) {
e.printStackTrace();
生成XLSX格式Excel文档 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI导出Excel小结--导出XLSX格式文档 使用Apache POI导出Excel--大数量导出 上一篇我们已经给出不同版本Excel文档处理的数据信息...
生成XLSX格式Excel文档大数据量导出 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI导出Excel小结--导出XLSX格式文档 使用Apache POI导出Excel--大数量导出 现在我们看看如何基于XLSX格式Excel...
POI操作Excel Excel简介一个excel文件就是一个工作簿workbook,一个工作簿中可以创建多张工作表sheet,而一个工作表中包含多个单元格Cell,这些单元格都是由列(Column)行(Row)组成,列用大写英文字母表示,从A开始到Z共26列,然后再从AA...
实习第二周
No.2 项目功能里要求能够将展示的报表导出excel,因为报表的数据都是动态从list传进来的,所以使用了POI技术来动态构建excel文件。百科里说POI是介个样子的“ApachePOI是Apache软件基金会的开放源码函式库,POI提供API给Java程...
使用首先需要了解他的工作原理 1.POI结构与常用类 (1)创建Workbook和Sheet (2)创建单元格 (1)POI介绍 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。 ...
其实,我真的是一个很普通的女人,从来没有特别好的事降临我头上,也没很坏的事。每天就糊涂的过着随遇而安的生活。
但是,我有信念,从不相信所谓的迷信,认为那是很封建的,会给我带来好运气或坏运气。
比如,有一早...
抬头看满天星却挂念 某一道彩虹 生命太匆匆 她两手空空 但心事太沉重 今天讲的她,对我很重要,一直陪伴在我身边,管着我吃喝拉撒睡的她。 离你不远 曾经无数次埋怨过你,不肯放我离家千里远的地方念书,总是无数次担心我在外受欺负没人帮着安慰和报仇。 一次和你通电话你说你生病了,我...
小子 别跟我乱扯犊子 这几条街区都是我在乞讨 你个兔崽子 穿几件破衣 画几道黑漆 一瘸一拐就敢威风凛凛 抢了我的路牙子 小子 收起你的眼珠子 一副死乞白赖捧着小铁碗 挨揍的样子 抢我的生意 说我的台词 瘦骨嶙峋就敢混要饭吃 打了你的狗腿子 小子 赶紧玩儿蛋去 谁不知道你有多...
他是月亮陛下, 有一头微黄略带颓废的短发, 知道别人都拿自己当笑话, 可他却孜孜不倦,乐此不疲, 我曾问过他, 他说。月亮呐,那是他的信仰啊。 就像对所有人说的那样, 月亮的光芒总被人淡忘, 可却不羡慕太阳的长相, 会说的情话只有晚安,略带彷徨, 愿对所有说过晚安的人, 他...
高中数学老师和高二学生体验读经 国庆期间,二嫂康妈从湖北十堰到上海来,是高中数学老师。侄子康康从北京到上海来,在北京是中央民族学院附属中学高二学生。康妈和康康是过来看望爷爷奶奶,和其它亲人的,她们早就知道我家里一直坚持全家读经典,非常认可,在康爸的支持下,终于可以亲自体验“...JAVA 无需JXL和POI用PageOffice自动生成Excel表格
<span type="1" blog_id="1243568" userid='
分享到朋友圈
好的文章,和好友一起分享

我要回帖

更多关于 poi读取模板导出excel 的文章

 

随机推荐