Java excelエクスポートは2007以上のバージョンをサポートしています
4011 ワード
一.ワークシートXSSFWORkbook wb=new XSSFWORkbook()を作成する.
. sheet ( sheet , )
XSSFSheet sheet1 = createSheet(wb, " ");
XSSFSheet sheet2 = createSheet(wb, " ");
2.1 createSheet() : sheet ,
public XSSFSheet createSheet(XSSFWorkbook wb,String sheetName){
wb.createSheet(sheetName);
// sheet
sheet.setHorizontallyCenter(true);
// ( )
sheet.setFitToPage(false);
// excel, excel
sheet.setForceFormulaRecalculation(true);
//
XSSFPrintSetup ps = sheet.getPrintSetup();
ps.setLandscape(true); // true: ,false: , ,
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); // A4
return sheet; }
3.//
XSSFFont font = wb.cteateFont();
font.setFontName(" ");
//
font.setColor(IndexedColors.WHITE.getIndex());
// list
genExcel(wb,sheet,list); ( : , )
OutputStream os=null;
HttpServletResponse response=null;
try{
response=ServletActionContext.getResponse();
os=response.getOutputStream();
response.reset();
//
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String filename="java"+new date(new SimpleFormat("yyyy-MM-dd"))+"xlsx";
filename=URLEncoder.encode(filename,"utf-8");
response.setHeader("content-disposition","filename=" + fileName);
wb.write(os);
catch (Exception e) {
}finally{
try {
os.flush();
os.close();
} catch (Exception e2) {
}
}
}
public void genExcel(XSSFWorkbook workbook, XSSFSheet sheet,List list) {
XSSFCellStyle headStyle = haveHeadStyle(workbook); //
XSSFCellStyle contextStyle = haveNormalStyle(workbook);//
XSSFCellStyle footStyle = haveRemarkStyle(workbook);//
XSSFCellStyle pinkStyle = haveExceptionStyle(workbook);//
// new CellRangeAddress
CellRangeAddress cra = new CellRangeAddress(
list.size() + 1, //first row
list.size() + 2, //last row
0,
column_title.length-1
);
sheet.addMergedRegion(cra);
// RegionUtil
//
RegionUtil.setBorderBottom(6, cra, sheet, workbook);
//
RegionUtil.setBorderLeft(6, cra, sheet, workbook);
//
RegionUtil.setBorderTop(6, cra, sheet, workbook);
//
RegionUtil.setBorderRight(6, cra, sheet, workbook);
//
XSSFRow row = sheet.createRow(0);
XSSFCell cell=null;
for(int i=0;i