System.out.println("下载EXCEL");
//设置类型
response.setContentType("application/octet-stream");
//设置文件的返回类型
response.setHeader("Content-disposition", "attachment;filename=ItemManage.xls");//默认Excel名称
response.flushBuffer();
OutputStream outputStream = response.getOutputStream();
manageHostService.getExcel(outputStream);
public void getExcel(OutputStream outputStream){
//获取到list里的一个ManageHost的字段个数是
Map<String,String> hashmap = new HashMap<String,String>();
hashmap.put("id", "主机ID");
hashmap.put("customerCode", "公司名称");
hashmap.put("hostName", "主机名称");
hashmap.put("manageHostState", "主机状态");
hashmap.put("deviceType", "传输协议");
hashmap.put("port", "主机类型");
String sheetName="管理主机信息";
try {
//导出excel
downlondExcel(hashmap,outputStream,sheetName, (ArrayList<ManageHost>) list);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @auther lh
* @param mp 标题
* @param outputStream 输出流
* @param name excel的sheet名字
* @param list 数据源
*/
public void downlondExcel(Map<String,String> mp, OutputStream outputStream, String name, ArrayList<ManageHost> list) throws IOException {
//创建工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//创建一个工作表
HSSFSheet sheet = hssfWorkbook.createSheet(name);
int size = mp.size();
//合并单元格 标题第一行
CellRangeAddress cra = new CellRangeAddress(0, 0, 0, (size-1));
sheet.addMergedRegion(cra);
HSSFRow row = sheet.createRow(0);
HSSFCell cell3 = row.createCell(0);
cell3.setCellValue("科技发展有限公司");
//创建一个单元格样式
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);//靠上
//创建字体
HSSFFont font = hssfWorkbook.createFont();
font.setFontHeightInPoints((short) 50); //设置字体大小
font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗
//把字体应用到样式上
style.setFont(font);
//把样式应用到单元格上
cell3.setCellStyle(style);
//设置行高
row.setHeightInPoints(20); //第一行
//存放标题
String[] names = new String[mp.size()];
int counts=0;
Set<Map.Entry<String, String>> entries = mp.entrySet();
for (Map.Entry<String, String> ent:entries){
names[counts] =ent.getValue();
counts++;
}
//第一行单元格添加内容 (标题)
HSSFRow row1 = sheet.createRow(1);
for (int j = 0; j <names.length ; j++) {
row1.createCell(j).setCellValue(names[j]);
}
int rowNum=2; //存放第几行
if (list.size()>0 && list != null){
//遍历list数据源
for (ManageHost Items:list) {
//创建第rowNum行
HSSFRow row2 = sheet.createRow(rowNum);
//往此行里添加内容
row2.createCell(0).setCellValue(Items.getId());
row2.createCell(1).setCellValue(Items.getCustomerCode());
row2.createCell(2).setCellValue(Items.getHostName());
row2.createCell(3).setCellValue(Items.getManageHostState());
row2.createCell(4).setCellValue(Items.getDeviceType());
row2.createCell(5).setCellValue(Items.getPort());
//下一行
rowNum++;
}
}
//写出
hssfWorkbook.write(outputStream);
}