- 2023-03-19 11:23:26
- 9623 热度
- 0 评论
下面有源码,下载就可以用,如果因为包的问题,就拷贝我的包就行了
首先是模板工具类的写法:
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; /** * 导出Excel的工具类,只需给相应参数即可 */ public class ExcelUtil { /** * 导出Excel的方法 * @param request Web应用中的request对象 * @param excelContent 数据体集合,集合内放置String数组 * @param columnNames 数据列的头,使用数组 * @param titleName 要导出的文件名称 * @param fOut 导出流,Web中使用response得到 * @throws IOException 可能会爆出的异常 */ @SuppressWarnings("deprecation") public static void create(HttpServletRequest request, List<?> excelContent, String[] columnNames, String titleName, OutputStream fOut) throws IOException { HSSFWorkbook wb = new HSSFWorkbook();// 建立新HSSFWorkbook对象 HSSFSheet sheet = wb.createSheet("sheet1");// 建立新的sheet对象 // ================合并标题列========================= 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.setFillPattern((short)300); 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编码解决中文高位字节截断 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);// 设置cell编码解决中文高位字节截断 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())); // filename是工作薄的存放位置 wb.write(fOut); fOut.flush(); fOut.close(); } }
再者是调用的Servlet,只是为了演示,具体使用看自己的了:
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; } }
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- ORA(15)
- JPA(15)
- Apache(15)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- Data(12)
- JSON(12)
- OAuth2(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- mail(7)
- IntelliJ(7)
- windows(7)
- too(7)
- HTML(7)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- nginx(6)
- Server(6)
- ueditor(6)
- jar(6)
- ehcache(6)
- UDP(6)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- Syntaxhighlighter(5)
- script(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)