import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.servlet.view.document.AbstractXlsView;
import cn.xaele.kq.psm.bean.KqWorkType;
import cn.xaele.utils.PoiUtils;
/**
* “工种” Excel文档输出控制类
*
* @author taotao
* @since 2016-10-04 15:50
* @version 1.0.0
*/
public class KqWorkType_ExcelView extends AbstractXlsView {
// 日志输出
private static final Logger log = LogManager.getLogger(KqWorkType_ExcelView.class);
// 测试标记
private boolean isTest = true;
// 设置输出列表题头内容
private String[] fieldName = { "序号", "工种名称", "关键工种", "备注" };
/**
* 输出所有 工种 信息 @param model @param workBook @param request @param
* response @throws
*/
public void buildExcelDocument(Map model, Workbook workBook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 接收传递过来的数据
@SuppressWarnings("unchecked")
List objList = (List) model.get("objList");
if (objList == null) {
objList = new ArrayList();
}
if (isTest) {
log.info("list.size()-------------->" + objList.size());
}
String saveFileName = "KqWorkTypeRpt";
int rowNum = 0;
int cellNum = 0;
try {
workBook = new HSSFWorkbook();
Sheet sheet = workBook.createSheet("Sheet1");
Row row = null;
// 获取样式
CellStyle headStyle = PoiUtils.getHeadStyle(workBook);
CellStyle tblTitleStyle = PoiUtils.getTblTitleCellStyle(workBook);
CellStyle cellStyle = PoiUtils.getTblCellStyle(workBook);
CellStyle cellIntStyle = PoiUtils.getTblCellStyle_INT(workBook);
// 设置默认行高
sheet.setDefaultRowHeightInPoints(20);
// 设置默认列宽
sheet.setDefaultColumnWidth(10);
// 设置特定单元格的宽度
// 设置“序号”的宽度
sheet.setColumnWidth(0, 4 * 256);
// sheet.setColumnWidth(1, 20 * 256);
// sheet.setColumnWidth(2, 30 * 256);
// 创建主标题
CellRangeAddress titleRange = new CellRangeAddress(0, 0, 0, fieldName.length - 1);
sheet.addMergedRegion(titleRange);
// 创建新行
row = sheet.createRow(rowNum);
// 设置行高
row.setHeightInPoints(24);
Cell cell = row.createCell(0);
cell.setCellValue("工种 列表");
cell.setCellStyle(headStyle);
// 创建副标题
// 创建列表题头信息
// 创建新行,遵循规则“看前不看后,后者自己补齐相关数据”
rowNum++;
row = sheet.createRow(rowNum);
// 设置行高
row.setHeightInPoints(24);
// 设置列表题头及样式
for (int cellitem = 0; cellitem < fieldName.length; cellitem++) {
cell = row.createCell(cellitem);
cell.setCellValue(fieldName[cellitem]);
cell.setCellStyle(tblTitleStyle);
}
// 创建列表信息
for (int item = 0; item < objList.size(); item++) {
KqWorkType bean = objList.get(item);
cellNum = 0;
// 创建新行
// 创建新行,遵循规则“看前不看后,后者自己补齐相关数据”
rowNum++;
row = sheet.createRow(rowNum);
// 设置行高
row.setHeightInPoints(24);
// 设置序号
cell = row.createCell(cellNum);
cell.setCellValue(item + 1);
cell.setCellStyle(cellIntStyle);
// 工种名称
// 创建新列,遵循规则“看前不看后,后者自己补齐相关数据”
cellNum++;
cell = row.createCell(cellNum);
cell.setCellValue(bean.getWorkTypeName());
cell.setCellStyle(cellStyle);
// 关键工种
// 创建新列,遵循规则“看前不看后,后者自己补齐相关数据”
cellNum++;
cell = row.createCell(cellNum);
cell.setCellValue(bean.getPivotalStr());
cell.setCellStyle(cellStyle);
// 备注
// 创建新列,遵循规则“看前不看后,后者自己补齐相关数据”
cellNum++;
cell = row.createCell(cellNum);
cell.setCellValue(bean.getMemo());
cell.setCellStyle(cellStyle);
}
// 设置标题:
// 将第一行作为标题,即每页都打印此行
CellRangeAddress repeatRange = new CellRangeAddress(0, 1, 0, fieldName.length - 1);
sheet.setRepeatingColumns(repeatRange);
// 页脚:
Footer footer = sheet.getFooter();
footer.setRight("第" + HSSFFooter.page() + "页 共" + HSSFFooter.numPages() + "页 ");
// 设置活动sheet为第一个
workBook.setActiveSheet(0);
// 输出到页面
// response.setContentType("text/html;charset=GBK");
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;filename=" + saveFileName + ".xls");
OutputStream out = response.getOutputStream();
workBook.write(out);
out.close();
} catch (IndexOutOfBoundsException ex) {
ex.printStackTrace();
}
}
}