您的位置:首页 > 编程语言 > Java开发

spring mvc项目中导出excel表格简单实现

2015-11-11 13:24 381 查看
查阅了一些资料,才整理出spring mvc 项目导出excel表格的实现,其实很是简单,小计一下,方便以后查阅,也希望帮助有需要的朋友。

1.导入所需要依赖(Jar包)。我使用的是maven,所以坐标如下:

    <!-- excel导出 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>


2.配置spring mvc 基本环境.(略)

3.编写方法,添加映射。具体实现如下:

@RequestMapping("exportException")
public void  exportExcel(ProblemPlace problemPlace,HttpServletRequest request,HttpServletResponse response){
    //设置响应类型
response.setContentType("application/vnd.ms-excel");
String codedFileName = null;
String fileName=null;
OutputStream fOut = null;
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
List<PPResult> list=new ArrayList<PPResult>();
if("nolink".equals(problemPlace.getExceptionType())){
fileName="警综关联异常清单"+sdf.format(d);
list = ppService.getNoLinkPointException(problemPlace);
}else{
fileName="地图撒点异常清单"+sdf.format(d);
list = ppService.getNoMapPointException(problemPlace);
}
// 进行转码,使其支持中文文件名

try {
codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
String[] headers={"派出所","场所名称","场所地址","场所类型"};
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet("异常清单");
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(50);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();

// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++)
{
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}

if(list.size()>0){
for (int i = 0; i < list.size(); i++) {
//新建一行
HSSFRow createRow = sheet.createRow(i+1);
HSSFCell cell0 = createRow.createCell(0);
HSSFRichTextString text = new HSSFRichTextString(list.get(i).getSspcs_mc());
cell0.setCellValue(text);

HSSFCell cell1 = createRow.createCell(1);
HSSFRichTextString text1 = new HSSFRichTextString(list.get(i).getCsmc());
cell1.setCellValue(text1);

HSSFCell cell2 = createRow.createCell(2);
HSSFRichTextString text2 = new HSSFRichTextString(list.get(i).getCsdz());
cell2.setCellValue(text2);

HSSFCell cell3 = createRow.createCell(3);
HSSFRichTextString text3 = new HSSFRichTextString(list.get(i).getXslx_mc());
cell3.setCellValue(text3);
}
}
try {
OutputStream out = response.getOutputStream();
workbook.write(out);
} catch (IOException e) {

e.printStackTrace();
}

}


当然,还有很多样式都没有实现,这里只是一个简单的导出功能,有需求的时候再进一步学习。

字体和样式设置:

HSSFFont f  = wb.createFont();
f.setFontHeightInPoints((short) 11);//字号
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗

HSSFCellStyle style = wb.createCellStyle();
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style.setRotation(short rotation);//单元格内容的旋转的角度

HSSFDataFormat df = wb.createDataFormat();
style1.setDataFormat(df.getFormat("0.00%"));//设置单元格数据格式
cell.setCellFormula(string);//给单元格设公式
style.setRotation(short rotation);//单元格内容的旋转的角度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: