java 导出表格 poi的使用

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("excel表格");
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTitle = rowm.createCell(0);
HSSFFont font = workbook.createFont();

// 设置字体大小
font.setFontHeightInPoints((short) 12);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("宋体");
// 设置样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置低边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置低边框颜色
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置右边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置顶边框
style.setTopBorderColor(HSSFColor.BLACK.index);
// 设置顶边框颜色
style.setTopBorderColor(HSSFColor.BLACK.index);
// 在样式中应用设置的字体
style.setFont(font);
// 设置自动换行
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

/**
* 发送响应流方法
* @param response
* @param fileName
*/
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream");
//这后面可以设置导出Excel的名称,此例中名为student.xls
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
} catch (Exception ex) {
ex.printStackTrace();
}
}


//响应到客户端
try {
// response.setContentType("application/x-download");
// response.setHeader("content-type", "application/octet-stream");
// response.setContentType("application/octet-stream");
exp.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
   workbook.write(out);
  os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
设置单元个斜线
HSSFCellStyle style = exp.getStyle(workbook);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
sheet.setColumnWidth(0,22*256);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//前四个参数代表自动布满单元格,后四个参数代表第1列第3行
HSSFClientAnchor a = new HSSFClientAnchor(0, 0, 1023, 255, (short)0, 2, (short)0, 2);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
shape1.setLineStyle(HSSFSimpleShape.LINESTYLE_SOLID);
hssfCell.setCellValue(value);
sheet.createFreezePane(1,3,1,3);

猜你喜欢

转载自www.cnblogs.com/shihx/p/12109125.html