CXF框架利用poi导出excel

在此记下此笔记,以便自己以后忘了重新学习使用,也希望能帮到大家,本项目中我用到的是webservice+spring+hibernate+jersey,怎么配置使用的我就不解释了,在此仅向大家演示如何导出在此框架下如何导出excel,导出excel是大家常用的功能,希望能帮到大家。

前言:如果没有poi的jar包,请先下载,或者进入我给的这个连接下载:

http://download.csdn.net/download/m0_37679452/10196769

废话不多说,直接上代码:

----------------------------------------------------------------------------------------------------

页面:

<div>类型:<select name="ratingType" id="export_ratingType" class="xuanxiangkuang" >
            <option value="年中">年中</option>
<option value="年度">年度</option>
<option value="其它">其它</option>
            </select>
      </div>
       <div>状态:<select name="state" id="export_state" class="xuanxiangkuang" >
            <option value="科室通过">科室通过</option>
            <option value="已提交">已提交</option>
            <option value="科室意见">科室意见</option>
            </select>
      </div>
        <button class="exportdata-button">导出</button>

-------------------------------------------------------------------------------------------------------

<!--js部分  我使用了jquery-->

<script>
$(function(){
$(".exportdata-button").click(function(){  
var ratingType=$("#export_ratingType").val();
var state=$("#export_state").val();

//通过get方式请求到自己所写的路径(在控制器中指定了get方式来请求,所以只能用get方式)
window.location.href=自己项目的url前缀+'exportExcel?ratingType='+ratingType+'&state='+state;
})
</script>

-------------------------------------------------------------------------------------------------------

/**路径:exportExcel

* param:ratingType,state
* @return Response
*/
@GET
@Path("/exportExcel")
@Produces("text/plain")
public synchronized Response exportExcel(@QueryParam("ratingType") String ratingType, @QueryParam("state") String state){
// 导出当月汇总表
SimpleDateFormat sdf = new SimpleDateFormat("MM");
String date = DateUtil.getTimestamp();
String month = sdf.format(new Date());
String path = request.getSession().getServletContext().getRealPath("/") + "upload\\";
// 删除临时文件夹下的所有文件
delAllFile(path);
FileOutputStream fos = null;
String filename = month + "能级管理汇总表" + date + ".xls";
try {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("sheet1");

// 设置第一列的宽
sheet.setColumnWidth(0, 20 * 256);
// 设置第2列的宽
sheet.setColumnWidth(1, 20 * 256);
// 设置第5列的宽
sheet.setColumnWidth(4, 20 * 256);
// 冻结前三行
sheet.createFreezePane(0, 3);

// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 生成一个字体
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);// HSSFColor.VIOLET.index
// //字体颜色
font.setFontHeightInPoints((short) 14);

HSSFCell cell = row.createCell(0);
cell.setCellValue(month + "能级管理汇总表");
style.setFont(font);
cell.setCellStyle(style);
// 重新创建 格式 还原默认
style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

row = sheet.createRow(1);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell = row.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(style);

cell = row.createCell(1);
cell.setCellValue("科室");
cell.setCellStyle(style);
// 需合并
cell = row.createCell(2);
cell.setCellValue("姓名");
cell.setCellStyle(style);

cell = row.createCell(3);
cell.setCellValue("工号");
cell.setCellStyle(style);

cell = row.createCell(4);
cell.setCellValue("届数");
cell.setCellStyle(style);


cell = row.createCell(5);
cell.setCellValue("能级");
cell.setCellStyle(style);

cell = row.createCell(6);
cell.setCellValue("系数");
cell.setCellStyle(style);

// 表头合并格式
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 11));

List<Levels>  levels = levelFlowDao.findByStateAndType(state, ratingType);
for (int i = 0; i < levels.size(); i++) {
Levels level = levels.get(i);
// 统计数据
row = sheet.createRow(i + 2);// 从第三行开始创建
cell = row.createCell(0);
cell.setCellValue(i+1);
cell.setCellStyle(style);

cell = row.createCell(1);
cell.setCellValue(level.getDeptname());
cell.setCellStyle(style);

cell = row.createCell(2);
cell.setCellValue(level.getName());
cell.setCellStyle(style);

cell = row.createCell(3);
cell.setCellValue(level.getPersonid());
cell.setCellStyle(style);

cell = row.createCell(4);
cell.setCellValue(level.getWorkstarttime());
cell.setCellStyle(style);

cell = row.createCell(5);
cell.setCellValue(level.getLevel());
cell.setCellStyle(style);

cell = row.createCell(6);
cell.setCellValue(level.getQuotiety());
cell.setCellStyle(style);
}

fos = new FileOutputStream(path + filename);
wb.write(fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// 导出
File file = new File(path + filename);
ResponseBuilder response = Response.ok(file);
try {
response.header("Content-Disposition", "attachment;filename='" + URLEncoder.encode(file.getName(), "utf-8") + "'"); // 存储文件名称
} catch (Exception e) {
e.printStackTrace();
}
return response.build();

}

// 删除指定文件夹下所有文件
// param path 文件夹完整绝对路径
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);// 再删除空文件夹
flag = true;
}
}
return flag;
}

-----------------------------------------------------------------------------------------------------------------

好了,到此就结束了,希望能帮到各位同学,如果有疑问请评论留言。

猜你喜欢

转载自blog.csdn.net/m0_37679452/article/details/79013440