Export Table to use java poi

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

// set the font size
font.setFontHeightInPoints ((Short) 12);
// font bold
font.setBoldweight (HSSFFont.BOLDWEIGHT_BOLD);
// set the font name
font.setFontName ( "Arial" );
// set the style
HSSFCellStyle style = workbook.createCellStyle ();
// set the low border
style.setBorderBottom (HSSFCellStyle.BORDER_THIN);
// set the low border color
style.setBottomBorderColor (HSSFColor.BLACK.index);
// set the right border
style.setBorderRight (HSSFCellStyle.BORDER_THIN);
// set the top border
style.setTopBorderColor (HSSFColor.BLACK.index);
// set the top border color
style.setTopBorderColor (HSSFColor.BLACK.index);
// apply styles provided font
style.setFont (font);
// set word wrap
style.setWrapText (to false);
// Sets the horizontal alignment of the pattern is centered;
style.setAlignment (HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);

/ ** 
* The method of transmitting the response stream
* @param Response
* @param fileName
* /
public void setResponseHeader (the HttpServletResponse Response, String fileName) {
the try {
the try {
fileName = new new String (fileName.getBytes (), "ISO8859-1") ;
} the catch (UnsupportedEncodingException E) {
// the TODO Auto-Generated Block the catch
e.printStackTrace ();
}
the response.setContentType ( "file application / OCTET-Stream");
// name may be provided behind this export Excel, and in this case named student.xls
response.setHeader ( "the Content-Disposition", "Attachment; filename =" + fileName);
response.flushBuffer ();
} the 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();
}
Setting unit slash
Style = exp.getStyle HSSFCellStyle (Workbook); 
style.setAlignment (HSSFCellStyle.ALIGN_LEFT);
style.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);
sheet.setColumnWidth (0,22 * 256);
HSSFPatriarch Patriarch sheet.createDrawingPatriarch = ();
// the first four parameters represent the full automatic cell, four parameters represent the column 3, line 1
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);

Guess you like

Origin www.cnblogs.com/shihx/p/12109125.html