poi技术导出海量数据到excel
2013-03-16 22:49
239 查看
http://poi.apache.org/download.html这是包的下载网站
---------------------------------------------------------------------------------------------------------
//导出数据到EXCEL表格
public static void ExportLgExcel(HttpServletRequest request, HttpServletResponse resp) throws Exception {
//读取指定目录下面的excel导出模板
String excelDir = request.getServletContext().getRealPath("/upload/ExcelStencil");
Workbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xlsx")));
} catch (Exception e) {
wb = new HSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xls")));
}
// 创建字体,设置其为红色、粗体:
Font font = wb.createFont();
font.setColor(Font.COLOR_RED);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
//创建格式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
CellStyle cellStyleBorder = wb.createCellStyle();
cellStyleBorder.setBorderBottom(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderLeft(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderRight(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderTop(CellStyle.BORDER_THIN);
//这个是从数据库里面查询出来的数据,存放到列表中
//Class log:数据库查询出数据的一个对象
List<Class log> result = new ArrayList<Class log>();
//获得第一个工作区
Sheet sheet = wb.getSheetAt(0);
if (!CommUtils.isNull(result)) {
//循环遍历查询出来的结果对象,写入到excel表格里面去
Integer listSize = result.size();
for (int i = 0; i < listSize; i++) {
Class log = result.get(i);
//因为第一行存放的是列标题,所以从第二行开始写入
//从第二行开始写入
Row row = sheet.createRow(i + 1);
//往表格里面填充数据
Integer cellLeng = log.getClass().getDeclaredFields().length;
for (int cellIndex = 0; cellIndex < cellLeng; cellIndex++) {
//设置excel表格的样式
Cell cell = row.createCell(cellIndex);
if (cellIndex < cellLeng - 1) {
cell.setCellStyle(cellStyleBorder);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
//将第一个数据保存到第一个单元格,依次类推
if (cellIndex == 0) {
String one = log.getXXXXX();
cell.setCellValue(one);
} else if (cellIndex == 1) {
String two = log.getXXXX();
cell.setCellValue(two);
} else if (cellIndex == 2) {
Integer three = log.getXXXXX();
cell.setCellValue(three.toString());
} else if (cellIndex > 2) {
break;
}
}
}
}
//生成导出文件日期
Calendar c = Calendar.getInstance();
String time = c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DATE) + "_" + c.get(Calendar.HOUR) + "_" + c.get(Calendar.MINUTE) + "_" + c.get(Calendar.SECOND);
//生成文件名(以下涉及到文件的输入和输出流)
File excelFile = new File("XXXXX_" + time + ".xls");
FileOutputStream fos = new FileOutputStream(excelFile);
wb.write(fos);
if (!CommUtils.isNull(fos)) {
fos.close();
}
// 读到流中
InputStream inStream = new FileInputStream(excelFile);
// 设置输出的格式
resp.reset();
resp.setContentType("application/x-download; charset=utf-8");
resp.addHeader("Content-Disposition", "attachment; filename=\"" + excelFile + "\"");
// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
try {
OutputStream out = resp.getOutputStream();
while (inStream.read(b) > 0) {
out.write(b);
}
inStream.close();
out.close();
excelFile.delete();
} catch (IOException ex) {
}
}
---------------------------------------------------------------------------------------------------------
//导出数据到EXCEL表格
public static void ExportLgExcel(HttpServletRequest request, HttpServletResponse resp) throws Exception {
//读取指定目录下面的excel导出模板
String excelDir = request.getServletContext().getRealPath("/upload/ExcelStencil");
Workbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xlsx")));
} catch (Exception e) {
wb = new HSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xls")));
}
// 创建字体,设置其为红色、粗体:
Font font = wb.createFont();
font.setColor(Font.COLOR_RED);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
//创建格式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
CellStyle cellStyleBorder = wb.createCellStyle();
cellStyleBorder.setBorderBottom(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderLeft(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderRight(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderTop(CellStyle.BORDER_THIN);
//这个是从数据库里面查询出来的数据,存放到列表中
//Class log:数据库查询出数据的一个对象
List<Class log> result = new ArrayList<Class log>();
//获得第一个工作区
Sheet sheet = wb.getSheetAt(0);
if (!CommUtils.isNull(result)) {
//循环遍历查询出来的结果对象,写入到excel表格里面去
Integer listSize = result.size();
for (int i = 0; i < listSize; i++) {
Class log = result.get(i);
//因为第一行存放的是列标题,所以从第二行开始写入
//从第二行开始写入
Row row = sheet.createRow(i + 1);
//往表格里面填充数据
Integer cellLeng = log.getClass().getDeclaredFields().length;
for (int cellIndex = 0; cellIndex < cellLeng; cellIndex++) {
//设置excel表格的样式
Cell cell = row.createCell(cellIndex);
if (cellIndex < cellLeng - 1) {
cell.setCellStyle(cellStyleBorder);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
//将第一个数据保存到第一个单元格,依次类推
if (cellIndex == 0) {
String one = log.getXXXXX();
cell.setCellValue(one);
} else if (cellIndex == 1) {
String two = log.getXXXX();
cell.setCellValue(two);
} else if (cellIndex == 2) {
Integer three = log.getXXXXX();
cell.setCellValue(three.toString());
} else if (cellIndex > 2) {
break;
}
}
}
}
//生成导出文件日期
Calendar c = Calendar.getInstance();
String time = c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DATE) + "_" + c.get(Calendar.HOUR) + "_" + c.get(Calendar.MINUTE) + "_" + c.get(Calendar.SECOND);
//生成文件名(以下涉及到文件的输入和输出流)
File excelFile = new File("XXXXX_" + time + ".xls");
FileOutputStream fos = new FileOutputStream(excelFile);
wb.write(fos);
if (!CommUtils.isNull(fos)) {
fos.close();
}
// 读到流中
InputStream inStream = new FileInputStream(excelFile);
// 设置输出的格式
resp.reset();
resp.setContentType("application/x-download; charset=utf-8");
resp.addHeader("Content-Disposition", "attachment; filename=\"" + excelFile + "\"");
// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
try {
OutputStream out = resp.getOutputStream();
while (inStream.read(b) > 0) {
out.write(b);
}
inStream.close();
out.close();
excelFile.delete();
} catch (IOException ex) {
}
}
相关文章推荐
- java利用poi技术解析、导出excel
- Spring MVC环境下用poi技术实现Excel的导入导出
- SSM框架使用POI技术导出Excel表
- Java中用poi技术将报表导出成excel表
- JAVA实现数据库数据导入/导出到Excel(POI技术)
- JavaPoi技术二-------导出数据到Excel以及从excel中读取数据
- 【poi】解决java导出excel 海量数据内存溢出问题
- SSM框架使用POI技术导出Excel表
- 项目所用技术回顾之excel导出(基于poi)
- Spring MVC环境下用poi技术实现Excel的导入导出
- Java通过POI技术操作Excel(3)----数据导出
- java通过POI技术操作Excel(1)----模板导出
- JAVAweb开发技术-------(七)POI导入导出excel技术
- jcob操作Excel文档,并且调用打印机打印(Excel导出是用POI)
- java poi 导入导出Excel xsl xslx
- JAVA导入导出EXCEL(POI)
- POI导出EXCEL文档、水平垂直居中、表头添加颜色
- JAVA使用POI根据模版导出EXCEL
- POI导出excel的整个过程
- POI导出Excel(设置字体,字体大小,行高,边框),让Excel更优美。(裴东辉)