@Override
public InputStream exportTrialAccount(Map<String, Object> params) {
List<Object> list = this.searchTrialAccountList(params);
try {
if (list != null && list.size() > 0) {
XSSFWorkbook xssf = new XSSFWorkbook();
Workbook wb = new SXSSFWorkbook(xssf);
Sheet sheet = wb.createSheet("导出");
sheet.setAutoFilter(CellRangeAddress.valueOf("B1:C1"));
sheet.setColumnWidth(1,256*8*2);
sheet.setColumnWidth(2,256*8*2);
sheet.setColumnWidth(3,256*8*2);
sheet.setColumnWidth(4,256*8*2);
sheet.setColumnWidth(5,256*8*1);
sheet.setColumnWidth(6,256*6*2);
sheet.setColumnWidth(7,256*6*2);
sheet.setColumnWidth(8,256*8*1);
Row row1 = sheet.createRow(0);
// excel 第一行为列名
String[] cols = new String[]{"序号","开号人员", "分配员工", "产品名称", "软件帐号", "软件密码", "开始时间", "结束时间", "帐号状态", "分配状态"};
String[] columnNames = new String[]{"排名","staffName", "assignName", "name", "account", "password", "start_time", "end_time", "status", "empAssignStatus"};
CellStyle style = setRowStyle(wb);
for (int i = 0; i < cols.length; i++) {
Cell colCell = row1.createCell(i);
colCell.setCellValue(cols[i]);
colCell.setCellStyle(style);
}
int rowNum = 1;// 数据从第二行开始显示
int colNum = 0;// 每一行的cell数
for (int k = 0; k < list.size(); k++) {
Row dataRow = sheet.createRow(rowNum);
Map entity = (Map) list.get(k);
for (colNum = 0; colNum < columnNames.length; colNum++){
String key = columnNames[colNum];
Cell cell = null;
if (colNum == 0) {
cell = dataRow.createCell(colNum);
cell.setCellValue(k+1);
} else {
cell = dataRow.createCell(colNum);
cell.setCellValue(entity.get(key) != null ? entity.get(key).toString() : "");
}
}
rowNum++;
colNum = 0;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
baos.flush();
baos.close();
return swapStream;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private CellStyle setRowStyle(Workbook wb) {
CellStyle style = wb.createCellStyle();
//边框填充
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中对齐
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
Font font = wb.createFont();
font.setFontName("黑体");
style.setFont(font);
// style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setWrapText(true);
return style;
}