关于JAVA 用POI 导出Excel

研究好几天终于把导出EXCEL 报表研究透彻了。
以下是我整理的一些资料。
框架:spring mvc 
公共类导出EXCEL:(这个很重要,拿过来不用太多的修改,每个方法的属性都有注解 关于JAVA <wbr>用POI <wbr>导出Excel
package com.cmcc.pas.common;

import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExportUtil
{
private XSSFWorkbook wb = null;

private XSSFSheet sheet = null;

public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet)
{
this.wb = wb;
this.sheet = sheet;
}

public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs)
{

int toprowNum = region.getFirstRow();
for (int i = toprowNum; i <= region.getLastRow(); i++)
{
XSSFRow row = sheet.getRow(i);
for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++)
{
XSSFCell cell = row.getCell(j);// XSSFCellUtil.getCell(row,
// (short) j);
cell.setCellStyle(cs);
}
}
}

public XSSFCellStyle getHeadStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格的背景颜色为淡蓝色
cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
// 设置单元格居中对齐
// 设置单元格内容水平对其方式   
// XSSFCellStyle.ALIGN_CENTER       居中对齐   
// XSSFCellStyle.ALIGN_LEFT         左对齐   
// XSSFCellStyle.ALIGN_RIGHT        右对齐   
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
// 设置单元格内容垂直对其方式   
// XSSFCellStyle.VERTICAL_TOP       上对齐   
// XSSFCellStyle.VERTICAL_CENTER    中对齐   
// XSSFCellStyle.VERTICAL_BOTTOM    下对齐  
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 创建单元格内容显示不下时自动换行
cellStyle.setWrapText(true);
// 设置单元格字体样式
XSSFFont font = wb.createFont();
// 设置字体加粗
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
// 设置单元格边框为细线条
// 设置单元格边框样式   
// CellStyle.BORDER_DOUBLE      双边线   
// CellStyle.BORDER_THIN        细边线   
// CellStyle.BORDER_MEDIUM      中等边线   
// CellStyle.BORDER_DASHED      虚线边线   
// CellStyle.BORDER_HAIR        小圆点虚线边线   
// CellStyle.BORDER_THICK       粗边线   
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}

public XSSFCellStyle getBodyStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格居中对齐
// 设置单元格内容水平对其方式   
// XSSFCellStyle.ALIGN_CENTER       居中对齐   
// XSSFCellStyle.ALIGN_LEFT         左对齐   
// XSSFCellStyle.ALIGN_RIGHT        右对齐   
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
// 设置单元格内容垂直对其方式   
// XSSFCellStyle.VERTICAL_TOP       上对齐   
// XSSFCellStyle.VERTICAL_CENTER    中对齐   
// XSSFCellStyle.VERTICAL_BOTTOM    下对齐  
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 创建单元格内容显示不下时自动换行
cellStyle.setWrapText(true);
// 设置单元格字体样式
XSSFFont font = wb.createFont();
// 设置字体加粗
font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
// 设置单元格边框为细线条
// 设置单元格边框样式   
// CellStyle.BORDER_DOUBLE      双边线   
// CellStyle.BORDER_THIN        细边线   
// CellStyle.BORDER_MEDIUM      中等边线   
// CellStyle.BORDER_DASHED      虚线边线   
// CellStyle.BORDER_HAIR        小圆点虚线边线   
// CellStyle.BORDER_THICK       粗边线   
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}

public XSSFCellStyle getBigHeadStyle()
{
// 创建单元格样式
XSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格的背景颜色为淡蓝色
cellStyle.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
// 设置单元格居中对齐
// 设置单元格内容水平对其方式   
// XSSFCellStyle.ALIGN_CENTER       居中对齐   
// XSSFCellStyle.ALIGN_LEFT         左对齐   
// XSSFCellStyle.ALIGN_RIGHT        右对齐   
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 设置单元格垂直居中对齐
// 设置单元格内容垂直对其方式   
// XSSFCellStyle.VERTICAL_TOP       上对齐   
// XSSFCellStyle.VERTICAL_CENTER    中对齐   
// XSSFCellStyle.VERTICAL_BOTTOM    下对齐  
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 创建单元格内容显示不下时自动换行
cellStyle.setWrapText(true);
// 设置单元格字体样式
XSSFFont font = wb.createFont();
// 设置字体加粗
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 400);
cellStyle.setFont(font);
// 设置单元格边框为细线条
// 设置单元格边框样式   
// CellStyle.BORDER_DOUBLE      双边线   
// CellStyle.BORDER_THIN        细边线   
// CellStyle.BORDER_MEDIUM      中等边线   
// CellStyle.BORDER_DASHED      虚线边线   
// CellStyle.BORDER_HAIR        小圆点虚线边线   
// CellStyle.BORDER_THICK       粗边线   
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
return cellStyle;
}
}

JSP前台页面:
由于ajax 不能回显出,excel下载保存打开窗口。所以必须要用form 表单的submit。
代码如下:
//导出
function onExport(){
 
var form1 = window.document.getElementByIdx_x("form");//获取form1对象
form1.action="${pageContext.request.contextPath}/deposit_DepositStatisticsController/export.json";
form1.submit(); 
}
Controller层:
@LogRecord
@RequestMapping(value = "/export")
@AccessAuthority(access = AccessType.AUTH, name = "备付金管理-导出EXCEL", id = "6203")
public Map export(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception
{
response.setContentType("application/binary;charset=ISO8859_1");
try
{
ServletOutputStream outputStream = response.getOutputStream();
String fileName = new String(("导出excel例子").getBytes(), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx"); 
String[] titles = { "ID", "客户编号", "客户名称", "金额" };
List > listData = new ArrayList >();
listData = excelService.queryExcelDate();
excelService.exportExcel(titles, listData, outputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
return model;
}
service层:

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmcc.pas.common.ExportUtil;
import com.cmcc.pas.repository.deposit.ExcelDAO;

@Service
public class ExcelService
{

public void exportExcel(String[] titles, List > listData, ServletOutputStream outputStream)
{


XSSFWorkbook workBook = new XSSFWorkbook();


XSSFSheet sheet = workBook.createSheet("内存excel名称");
ExportUtil exportUtil = new ExportUtil(workBook, sheet);


XSSFCellStyle bigHeadStyle = exportUtil.getBigHeadStyle();

XSSFCellStyle headStyle = exportUtil.getHeadStyle();

XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();


XSSFCell cell = null;


sheet.addMergedRegion(new CellRangeAddress(0, (short) 1, 0, (short) 3));
XSSFRow regRow = sheet.createRow(0);
cell = regRow.createCell(0);
cell.setCellStyle(bigHeadStyle);
cell.setCellValue("报表");

XSSFRow headRow = sheet.createRow(2);
for (int i = 0; i < titles.length; i++)
{
cell = headRow.createCell(i);
cell.setCellStyle(headStyle);
cell.setCellValue(titles[i]);
}

if (listData != null && listData.size() > 0)
{
for (int j = 0; j < listData.size(); j++)
{

XSSFRow bodyRow = sheet.createRow(j + 3);
Map excelmodel = listData.get(j);

cell = bodyRow.createCell(0);
cell.setCellStyle(bodyStyle);
cell.setCellValue(excelmodel.get("ID").toString());
//设置表格长度
sheet.setColumnWidth(0, (excelmodel.get("ID").toString().getBytes().length) * 256);

cell = bodyRow.createCell(1);
cell.setCellStyle(bodyStyle);
cell.setCellValue(excelmodel.get("CUS_NO").toString());
//设置表格长度
sheet.setColumnWidth(1, (excelmodel.get("CUS_NO").toString().getBytes().length) * 256);

cell = bodyRow.createCell(2);
cell.setCellStyle(bodyStyle);
cell.setCellValue(excelmodel.get("CUS_NAME").toString());
//设置表格长度
sheet.setColumnWidth(2, (excelmodel.get("CUS_NAME").toString().getBytes().length) * 256);

cell = bodyRow.createCell(3);
cell.setCellStyle(bodyStyle);
cell.setCellValue(excelmodel.get("MONEY").toString());
//设置表格长度
sheet.setColumnWidth(3, (excelmodel.get("MONEY").toString().getBytes().length) * 256);
}
}
try
{
workBook.write(outputStream);
outputStream.flush();
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}
}


JAR包:(当然还有非常重要的JAR包)
这里面我就把需要的JAR包截图了,实在是找不到如何上传压缩文件。
关于JAVA <wbr>用POI <wbr>导出Excel
基本上写的非常详细吧。对于开发人员来说,这些东西应该很快都能看懂。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值