导出excel

public ActionForward exportList(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) {
  try {
  super.list(mapping, form, request, response);

  HSSFWorkbook wb =null;
  wb = bo.generateExcel1();
  String filename ="列表";
  byte[] tempByte = filename.getBytes("GBK");
  String tempStr = new String(tempByte, "ISO8859_1");
  // excel 文件的 MIME 类型
  response.setContentType("application/msexcel");
  // attachment -因为不希望在浏览器中直接打开它,
  // 可以通过设置default file name来确定保存文当时的建议名称。
  response.setHeader("Content-disposition", "attachment; filename="
  + tempStr + ".xls");
  javax.servlet.ServletOutputStream os = response.getOutputStream();
  wb.write(os);
  os.flush();
  os.close();
  // 返回到列表页面
  return null;
  } catch (Exception e) {
  e.printStackTrace();
  generalError(request, e);
  return mapping.findForward(FAIL);
  }
  }

******************************************************************************************************

public HSSFWorkbook generateExcel1() throws Exception{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");

// 数据格样式
HSSFCellStyle dataCellStyle = createDataCellStyle(wb);
// 小标题样式
HSSFCellStyle cellStyle = createCellStyle(wb);
sheet.setDefaultRowHeight((short) 400);
sheet.setColumnWidth((short) 1, (short) 15000);
int rows = 0;

HSSFRow row1 = sheet.createRow((short) (rows++));
row1.setHeight((short) 500);
createCell(row1, (short) 0, cellStyle, "");
createCell(row1, (short) 1, cellStyle, "");
createCell(row1, (short) 2, cellStyle, "");
createCell(row1, (short) 3, cellStyle, "");
createCell(row1, (short) 4, cellStyle, "");


for(int i=0;i<100;i++){
HSSFRow row2 = sheet.createRow((short) (rows++));
createCell(row2, (short) 0, dataCellStyle,"");
createCell(row2, (short) 1, dataCellStyle, ""));
createCell(row2, (short) 2, dataCellStyle, "");
createCell(row2, (short) 3, dataCellStyle,"");
createCell(row2, (short) 4, dataCellStyle, "");

}

return wb;
}



/********************************** 导出excel **********************************************/

private HSSFCellStyle createDataCellStyle(HSSFWorkbook wb) {
HSSFCellStyle dataCellStyle = wb.createCellStyle();
dataCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
return dataCellStyle;
}

**********************************
private HSSFCellStyle createTitleCellStyle(HSSFWorkbook wb) {
// 大标题样式
HSSFCellStyle titleCellStyle = wb.createCellStyle();
// 水平居中
titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 字体
HSSFFont font = wb.createFont();
// 设置字体的大小
font.setFontHeightInPoints((short) 18);
// 设置字体为粗体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 然后将字体关联到样式
titleCellStyle.setFont(font);
return titleCellStyle;
}
**********************************
private HSSFCellStyle createCellStyle(HSSFWorkbook wb) {
// 小标题样式
HSSFCellStyle cellStyle = wb.createCellStyle();
// 水平居中
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 字体
HSSFFont littleFont = wb.createFont();
// 设置字体为粗体
littleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 然后将字体关联到样式
cellStyle.setFont(littleFont);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
return cellStyle;
}


-------------设置颜色
String str = "#000000";
//处理把它转换成十六进制并放入一个数
int[] color=new int[3];
color[0] = Integer.parseInt(str.substring(1, 3), 16);
color[1] = Integer.parseInt(str.substring(3, 5), 16);
color[2] = Integer.parseInt(str.substring(5, 7), 16);
// 自定义颜色
HSSFPalette palette = wb.getCustomPalette();
palette.setColorAtIndex(HSSFColor.BLACK.index, (byte) color[0],
(byte) color[1], (byte) color[2]);
// 将自定义的颜色引入进来
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
dataCellStyle.setFont(font);

你可能感兴趣的:(导出Excel)