/** * 导出 excel * */ public void exportExcel(HttpServletResponse response, String title, List<Kqstatistic> lists){
File file = new File("exportexcel.xls"); try {
// 声明工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); //获取头部标题 String[] headers = {
"姓名","部门","应出勤","实际出勤"}; //获取显示字段 String[] infields = {
"memberName","departName","yingchuqin","wcl"}; JSONArray jsonArray = new JSONArray(); for (Kqstatistic entity: lists) {
JSONObject jsonObj = new JSONObject(); jsonObj.put("memberName", entity.getMemberName()); jsonObj.put("departName", entity.getDepartame());
jsonObj.put("yingchuqin", entity.getYingchuqin());
jsonObj.put("shichuqin", entity.getShichuqin());
jsonArray.add(jsonObj);
}
//调用util的方法,写入excel
ExcelUtil.createExcelSheet(title, headers, jsonArray, infields, workbook);
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(title+".xls", "utf-8"));
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
package com.hzstoa.zjswb.assessment.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import dm.jdbc.util.StringUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** * * @Description: 根据接口 生成 相应的 excel * @author cheng.hao * @param title excelsheet名称 * @param headers thead * @param jarray json 返回的jarray * @param infields 传入的 要显示的字段 */
@SuppressWarnings("deprecation")
public static HSSFSheet createExcelSheet(String title, String[] headers, JSONArray jarray, String[] infields, HSSFWorkbook workbook) throws Exception {
// 生成一个sheet 并设置名称
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 30);
//sheet.setDefaultRowHeightInPoints((float)1.4) ;
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(IndexedColors.BLACK.getIndex());
//设置底边框;
style.setBorderBottom(BorderStyle.THIN);
//设置左边框;
style.setBorderLeft(BorderStyle.THIN);
//设置右边框;
style.setBorderRight(BorderStyle.THIN);
//设置顶边框;
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short) 15);
font.setColor(IndexedColors.BLACK.getIndex());
//字体加粗
font.setBold(true);
// 把字体应用到当前的样式
style.setFont(font);
style.setWrapText(true);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(IndexedColors.BLACK.getIndex());
style2.setAlignment(HorizontalAlignment.LEFT);
style2.setVerticalAlignment(VerticalAlignment.CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
// 把字体应用到当前的样式
font2.setFontHeightInPoints((short) 12);
font2.setColor(IndexedColors.BLACK.getIndex());
style2.setFont(font2);
style2.setWrapText(true);
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
int sames = 0 ;
JSONArray j_array_combile = new JSONArray() ;
for(int i = 0 ;i<jarray.size();i++){
j_array_combile.add(jarray.getJSONObject(i)) ;
// 是否是最后一条记录的开关
boolean last = (i == jarray.size() - 1);
// 取出相邻的两条记录进行比较
JSONObject csl1 = null;
JSONObject csl2 = null;
if (!last) {
csl1 = jarray.getJSONObject(i);
csl2 = jarray.getJSONObject(i + 1);
} else {
// 防止最后一条记录无法加入集合
csl1 = jarray.getJSONObject(i);
if (jarray.size() != 1)
csl2 = jarray.getJSONObject(i - 1);
else
csl2 = jarray.getJSONObject(i);
}
// 默认 是 表头的 第一个字段 进行合并
if(!csl1.get(infields[0]).toString().equals(csl2.get(infields[0]).toString())){
JSONObject t_jobj = new JSONObject() ;
t_jobj.put("sames", sames) ;
j_array_combile.add(t_jobj) ;
sames = 0 ;
}else{
sames++ ; //
}
}
int index = 0;
for (int i = 0; i < j_array_combile.size(); i++) {
JSONObject jobj = j_array_combile.getJSONObject(i);
if(jobj.get("sames")==null){
index++;
row = sheet.createRow(index);
// 根据传入的 字段数组 取值
int cellIndex = 0 ;
for (String field : infields) {
HSSFCell cell = row.createCell(cellIndex);
cell.setCellStyle(style2);
Object value = jobj.get(field);
if(value==null){
value="";
}
// 判断值的类型后进行强制类型转换
String textValue = "";
if(!StringUtil.isEmpty(value.toString())){
textValue = value.toString();
}
if (textValue != null) {
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
HSSFRichTextString richString = new HSSFRichTextString(textValue);
richString.applyFont(font2);
cell.setCellValue(richString);
}
}
cellIndex++ ;
}
}else{
int same = jobj.getIntValue("sames") ;
// 进行合并
if(same!=0)
sheet.addMergedRegion(new CellRangeAddress((index-same), index, 0, 0));// 横向 合并 第几行 到第几行 第几列 到第几列
}
}
return sheet;
}