下面有源码,下载就可以用,如果因为包的问题,就拷贝我的包就行了
首先是模板工具类的写法:
Java代码??
- package?com.util;??
- ??
- import?java.io.IOException;??
- import?java.io.OutputStream;??
- import?java.math.BigDecimal;??
- import?java.text.MessageFormat;??
- import?java.util.List;??
- import?javax.servlet.http.HttpServletRequest;??
- import?org.apache.poi.hssf.usermodel.HSSFCell;??
- import?org.apache.poi.hssf.usermodel.HSSFCellStyle;??
- import?org.apache.poi.hssf.usermodel.HSSFFont;??
- import?org.apache.poi.hssf.usermodel.HSSFPrintSetup;??
- import?org.apache.poi.hssf.usermodel.HSSFRow;??
- import?org.apache.poi.hssf.usermodel.HSSFSheet;??
- import?org.apache.poi.hssf.usermodel.HSSFWorkbook;??
- import?org.apache.poi.hssf.util.HSSFColor;??
- import?org.apache.poi.hssf.util.Region;??
- ??
- ?
- ?
- ?
- ?
- ?
- ??
- public?class?ExcelUtil?{??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????@SuppressWarnings("deprecation")??
- ????public?static?void?create(HttpServletRequest?request,?List<?>?excelContent,??
- ????????????String[]?columnNames,?String?titleName,?OutputStream?fOut)??
- ????????????throws?IOException?{??
- ????????HSSFWorkbook?wb?=?new?HSSFWorkbook();??
- ????????HSSFSheet?sheet?=?wb.createSheet("sheet1");??
- ??????????
- ????????Region?region?=?new?Region();??
- ????????region.setColumnFrom((short)?0);??
- ????????region.setColumnTo((short)?(columnNames.length?-?1));??
- ????????region.setRowFrom((short)?0);??
- ????????region.setRowTo((short)?0);??
- ????????sheet.addMergedRegion(region);??
- ??????????
- ??????????
- ????????HSSFFont?titleFont?=?wb.createFont();??
- ????????titleFont.setColor(HSSFFont.COLOR_RED);??
- ????????titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);??
- ????????titleFont.setFontHeight((short)?300);??
- ??
- ??????????
- ????????HSSFFont?headerFont?=?wb.createFont();??
- ????????headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);??
- ????????headerFont.setColor(HSSFFont.BOLDWEIGHT_NORMAL);??
- ????????HSSFPrintSetup?printSetup?=?sheet.getPrintSetup();??
- ????????printSetup.setLandscape(true);??
- ????????sheet.setFitToPage(true);??
- ????????sheet.setHorizontallyCenter(true);??
- ????????sheet.createFreezePane(0,?1);??
- ????????sheet.setAutobreaks(true);??
- ????????sheet.setDefaultColumnWidth((short)?13.5);??
- ????????printSetup.setFitHeight((short)?100);??
- ????????printSetup.setFitWidth((short)?180);??
- ??????????
- ????????HSSFCellStyle?titlestyle?=?wb.createCellStyle();??
- ????????titlestyle.setFont(titleFont);??
- ????????titlestyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);??
- ????????titlestyle.setFillForegroundColor(HSSFColor.WHITE.index);??
- ????????titlestyle.setFillPattern(HSSFCellStyle.SQUARES);??
- ????????titlestyle.setLeftBorderColor(HSSFColor.BLACK.index);??
- ????????titlestyle.setRightBorderColor(HSSFColor.BLACK.index);??
- ????????titlestyle.setTopBorderColor(HSSFColor.BLACK.index);??
- ????????titlestyle.setBottomBorderColor(HSSFColor.BLACK.index);??
- ??????????
- ??
- ????????titlestyle.setWrapText(true);??
- ??????????
- ????????HSSFCellStyle?style?=?wb.createCellStyle();??
- ????????style.setFont(headerFont);??
- ????????style.setBorderBottom(HSSFCellStyle.BORDER_THIN);??
- ????????style.setAlignment(HSSFCellStyle.ALIGN_CENTER);??
- ????????style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);??
- ????????style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);??
- ????????style.setHidden(true);??
- ??????????
- ????????HSSFCellStyle?style2?=?wb.createCellStyle();??
- ????????style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);??
- ????????style2.setDataFormat(wb.createDataFormat().getFormat("0.00"));??
- ????????style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);??
- ??????????
- ????????HSSFRow?titleRow?=?null;??
- ??
- ????????if?(!titleName.equals(""))?{??
- ????????????titleRow?=?sheet.createRow((short)?0);??
- ????????????titleRow.setHeightInPoints(30.120f);??
- ????????????HSSFCell?titlecell?=?titleRow.createCell((short)?0);??
- ????????????titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);??
- ????????????titlecell.setCellStyle(titlestyle);??
- ????????????titlecell.setCellValue(titleName);??
- ????????}??
- ??
- ??????????
- ????????HSSFRow?headRow?=?sheet.createRow((short)?1);??
- ????????headRow.setHeightInPoints(20.120f);??
- ??
- ????????for?(int?i?=?0;?i?<?columnNames.length;?i++)?{??
- ????????????HSSFCell?cell?=?headRow.createCell((short)?i);??
- ????????????cell.setCellStyle(style);??
- ????????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);??
- ????????????cell.setCellValue(columnNames[i]);??
- ????????}??
- ??????????
- ????????for?(int?i?=?0;?i?<?excelContent.size();?i++)?{??
- ??????????????
- ????????????HSSFRow?row?=?sheet.createRow((short)?i?+?2);??
- ????????????for?(int?j?=?0;?j?<?((Object[])?excelContent.get(i)).length;?j++)?{??
- ??????????????????
- ????????????????HSSFCell?cell?=?row.createCell((short)?j);??
- ????????????????cell.setCellStyle(style2);??
- ????????????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);??
- ????????????????if?(j?==?0)?{??
- ????????????????????cell.setCellValue((i?+?1)?+?"");??
- ????????????????????continue;??
- ????????????????}??
- ????????????????Object?t?=?((Object[])?excelContent.get(i))[j];??
- ????????????????if?(t?instanceof?BigDecimal)?{??
- ????????????????????cell.setCellValue(((BigDecimal)?((Object[])?excelContent??
- ????????????????????????????.get(i))[j]).toString());??
- ????????????????????continue;??
- ????????????????}??
- ????????????????if?(t?instanceof?java.sql.Date)?{??
- ????????????????????cell.setCellValue(((java.sql.Date)?((Object[])?excelContent??
- ????????????????????????????.get(i))[j]).toString());??
- ????????????????????continue;??
- ????????????????}??
- ????????????????if?(t?instanceof?java.util.Date)?{??
- ????????????????????cell??
- ????????????????????????????.setCellValue(((java.util.Date)?((Object[])?excelContent??
- ????????????????????????????????????.get(i))[j]).toString());??
- ????????????????????continue;??
- ????????????????}??
- ????????????????cell.setCellValue((String)?((Object[])?excelContent.get(i))[j]);??
- ????????????}??
- ????????}??
- ??
- ????????request.setCharacterEncoding("gbk");??
- ????????MessageFormat.format("{0}/{1}.xls",?request.getRealPath("expExcel"),??
- ????????????????Long.toString(System.currentTimeMillis()));???
- ????????wb.write(fOut);??
- ????????fOut.flush();??
- ????????fOut.close();??
- ????}??
- ??
- }??
?
再者是调用的Servlet,只是为了演示,具体使用看自己的了:
Java代码??
- package?com.servlet;??
- ??
- import?java.io.IOException;??
- import?java.io.OutputStream;??
- import?java.util.ArrayList;??
- import?java.util.List;??
- ??
- import?javax.servlet.ServletException;??
- import?javax.servlet.http.HttpServlet;??
- import?javax.servlet.http.HttpServletRequest;??
- import?javax.servlet.http.HttpServletResponse;??
- ??
- import?com.util.ExcelUtil;??
- ??
- public?class?ExcelServlet?extends?HttpServlet?{??
- ??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ??
- ????@SuppressWarnings("unchecked")??
- ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)??
- ????????????throws?ServletException,?IOException?{??
- ??
- ????????List<?>?excelContent?=?getPersonList();??
- ????????String[]?columnNames?=?new?String[]?{?"编号",?"名称",?"住址",?"介绍",?"出生日期",??
- ????????????????"月薪"?};??
- ????????String?titleName?=?"测试Excel工具类导出.xls";??
- ??
- ????????OutputStream?fOut?=?response.getOutputStream();??
- ????????response.setHeader("Content-Disposition",?"attachment;?filename="??
- ????????????????+?new?String(titleName.getBytes("GB2312"),?"ISO8859-1"));??
- ??
- ????????ExcelUtil.create(request,?excelContent,?columnNames,?titleName,?fOut);??
- ????}??
- ??
- ????@Override??
- ????protected?void?doPost(HttpServletRequest?request,??
- ????????????HttpServletResponse?response)?throws?ServletException,?IOException?{??
- ????????this.doGet(request,?response);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ??
- ????@SuppressWarnings("unchecked")??
- ????public?static?List?getPersonList()?{??
- ????????List?list?=?new?ArrayList();??
- ????????String[]?p1?=?new?String[]?{?"00001",?"科研室主任",?"海淀区",?"北京大学研究生毕业",??
- ????????????????"1988-9-6",?"4500.99"?};??
- ????????String[]?p2?=?new?String[]?{?"00002",?"营销部经理",?"朝阳区",?"北京国际商务学院本科生",??
- ????????????????"1995-6-8",?"6000.25"?};??
- ????????String[]?p3?=?new?String[]?{?"00003",?"策划部专员",?"石景山区",?"清华大学应届毕业生",??
- ????????????????"1889-9-5",?"10000"?};??
- ????????list.add(p1);??
- ????????list.add(p2);??
- ????????list.add(p3);??
- ????????return?list;??
- ????}??
- ??
- }??
?
如果有问题,欢迎大家提出意见和建议,我们一同进步
-
excelPOI.rar?(4.8 MB)
- 下载次数: 75