一些乱七八糟记不住的属性。。
XSSFWorkbook类是xlsx,HSSFWorkbook是xls
//创建XSSFWorkbook(HSSFWorkbook)
XSSFWorkbook wb = new XSSFWorkbook();
// 打印格式
XSSFPrintSetup ps = sheet.getPrintSetup();ps.setLandscape(true); //true横向false纵向
ps.setPaperSize(XSSFPrintSetup.A4_PAPERSIZE);//A4纸
// 上下左右边距
sheet.setMargin(XSSFSheet.TopMargin, 0.64);
sheet.setMargin(XSSFSheet.BottomMargin, 0.64);
sheet.setMargin(XSSFSheet.LeftMargin, 0.24);
sheet.setMargin(XSSFSheet.RightMargin, 0.24);
// 水平垂直居中
sheet.setHorizontallyCenter(true); //水平
// sheet.setVerticallyCenter(true); //垂直
//合并单元格
CellRangeAddress rangoin = new CellRangeAddress(2,2,8,9); //起止行终止行起止列终止列
sheet.addMergedRegion(rangoin); //加入指定sheet
//单元格宽度
sheet.setColumnWidth(0, 1250);
//单元格垂直水平居中
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中格式
//字体
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11); // 字号
style.setFont(font1);
//行高
row.setHeightInPoints(30);
// 自动换行
style2.setWrapText(true);
cell.setCellStyle(style2);
//创建行
row = sheet.createRow((int) 0); //从0开始为第一行
//单元格设置文本
cell = row.createCell((short) 1); //从0开始
cell.setCellValue("文本");
cell.setCellStyle(style2);
//导出内容
for (int i = 2; i <list.size()+2;i++){
row = sheet.createRow((int) i+1);
row.setHeightInPoints(30);
vo = (PeiShouJiaoKuanZongHui) list.get(y);
cell = row.createCell((short) 0);
cell.setCellValue(y);
cell.setCellStyle(style3);
}
//导出
try
{
String fileName = "huiGouExportExcels"+new SimpleDateFormat("yyyyMMddhhmmss").format(new Date(System.currentTimeMillis()))+".xlsx"; //是HSSFWorkbook改xls
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
response.setCharacterEncoding("UTF-8");
OutputStream os = response.getOutputStream();
wb.write(os);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
//代更