做竞价网站用什么系统好,wordpress主题和备份下载,石河子建设网站,还有做网站的必要吗之前在项目中会用到在java在后台把数据填入Word文档的模板来提供前台下载#xff0c;为了自己能随时查看当时的实现方案及方便他人学习我写了这篇博客#xff0c;访问量已经是我写的博客里第一了。于是乎我在学会用Java在后台利用Apache poi 生成excel文档提供前台下载之后就…之前在项目中会用到在java在后台把数据填入Word文档的模板来提供前台下载为了自己能随时查看当时的实现方案及方便他人学习我写了这篇博客访问量已经是我写的博客里第一了。于是乎我在学会用Java在后台利用Apache poi 生成excel文档提供前台下载之后就想着来写一篇姊妹篇啦。在生成Excel文档的时候我采用了和生成Word时的不同方法Apache poi。它是用Java编写的免费开源的跨平台的 Java API提供API给Java程式对Microsoft Office格式档案读和写的功能。想要实现这个功能就按照下面的步骤来做吧为了方便起见我直接拿项目中遇到的实例来举例说明是的我在写这篇博客的时候同时也在完成手上的项目。step1:创建xls格式的模板表头含有我的甲方信息就打码了可以看到我搞了一个空的模板文件现在有很多东西需要在后台填入step2:前台触发事件搞一个按钮用户点击的时候用javascript的window.location.href将页面重定向到你处理下载的URL去比方说这是我项目的前台看到那个表面质量按钮吗来看一下当它被点击的时候调用的函数function exportBatch() {//get请求可以传递参数比方说我这里就传了一堆卷号我只生成传过去的这堆卷号的检验记录//参数rollNumbers的细节就不展示了业务相关window.location.href ../ir/exportSurface?rollNumberList rollNumbers;}有朋友可能想用什么Ajax来发送请求我反正是没搞出来挺麻烦的网上找的相关解决方案也都比较蛋疼因此不传什么复杂的敏感的参数就这么写就可以。step3:后台处理首先你当然要把Apache poi那一套东西引入你的项目啦我的项目是Maven项目添加依赖很容易org.apache.poipoi3.14然后为了方便导出Excel在项目中建了一个ExcelUtils工具类后面给出源码这么一来导出Excel会变得更简单。ExcelUtils里面rXogrNBYP除了一些既定的方法外还有就是你具体怎么去操作模板的方法了。当然你用的少的话可以不用我这工具类而是在你需要的时候import相关的类然后在你处理的时候就把操作模板的逻辑写进去也可以。但我这个项目很多次用到导出Excel所以抽象出一个工具类是很有必要的符合设计模式。我的项目是基于SpringMVC的来看看我后台接收到请求以后做了些什么吧Controller:/**** 批量导出表面质量检验记录** return* throws Exception*/RequestMapping(value exportSurface, method RequestMethod.GET)ResponseBodypublic void exportSurface(HttpServletRequest request,HttpServletResponse response) throws Exception {//参数获取及处理业务相关不展示//把要填写的数据放在一个map里Map map new HashMap();map.put(sequence, 0001);//mock编号map.put(date, DateUtils.toDateStr(new Date(), DateUtils.DEFAULT_DATE_PATTERN_CHINES最后调用ExcelUtils里的相关导出方法这个方法是自定义的它定义的是怎样去操作模板自定义的方法:public static void exportInspectionRecordSurface(HttpServletRequest request, HttpServletResponse response, Map map) throws IOException {//模板的路径这个在自己的项目中很容易弄错相对位置一定要写对啊String prXogrNBYPsth request.getRealPath(/) INSPECTIONRECORD_SURFACE_TEMPLET_PATH;Workbook webBook readExcel(psth);createCellStyle(webBook);Sheet sheet webBook.getSheetAt(0);//开始操作模板找到某行某列(某个cell)需要注意的是这里有个坑行和列的计数都是从0开始的//一次数据插入的位置不对别灰心多试几次就好啦你要是能看懂我下面的代码数据插在了什么位置你就明白了int rows 1;Row row sheet.getRow(rows);row.createCell(1).setCellValue((String) map.get(sequence));row.createCell(3).setCellValue((String) map.get(date));row.createCell(9).setCellValue((String) map.get(chetaihao));rows 2;row sheet.getRow(rows);row.createCell(1).setCellValue((String) map.get(productName));row.createCell(3).setCellValue((String) map.get(specification));row.createCell(9).setCellValue((String) map.get(memo));//检验记录的插入业务相关不展示其实就是for循环在合适的行合适的列插入一个个对象的属性即可你这么聪明没问题的writeExcel(response, webBook, 表面质量检验记录);}ExcelUtils://这里得有你自己的package名import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.net.URLEncoder;import java.util.ArrayList;import java.util.List;import java.util.Map;/*** Created by bwju on 2016/12/06.*/public class ExcelUtils {private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH /asserts/templete/InspectionRecordSurface.xls;private static HSSFCellStyle cellstyle null;public static void exportInspectionRecordSurface(HttpServletRequest request, HttpServletResponse response, Map map) throws IOException {//实现上文里有改个函数名写你的操作模板函数吧}private static Workbook readExcel(String filePath) {InputStream in null;Workbook work null;try {in new FileInputStream(filePath);work new HSSFWorkbook(in);} catch (FileNotFoundException e) {System.out.println(文件路径错误);e.printStackTrace();} catch (IOException e) {System.out.println(文件输入流错误);e.printStackTrace();}return work;}rXogrNBYPprivate static void writeExcel(HttpServletResponse response, Workbook work, String fileName) throws IOException {OutputStream out null;try {out response.getOutputStream();response.setContentType(application/ms-excel;charsetUTF-8);response.setHeader(Content-Disposition, attachment;filename.concat(String.valueOf(URLEncoder.encode(fileName .xls, UTF-8))));work.write(out);} catch (IOException e) {System.out.println(输出流错误);e.printStackTrace();} finally {out.close();}}private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, String value) {cell.setCellStyle(style);cell.setCellValue(value);return cell;}private static Cell setCellStyleWithValue(Cell cell, String value) {cell.setCellStyle(cellstyle);cell.setCellValue(value);return cell;}private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, RichTextString value) {cell.setCellStyle(style);cell.setCellValue(value);return cell;}private static Cell setCellStyleWithValue(Cell cell, int value) {cell.setCellStyle(cellstyle);cell.setCellValue(value);return cell;}private static Cell setCellStyleWithValue(Cell cell, double value) {cell.setCellStyle(cellstyle);cell.setCellValue(value);return cell;}private static HSSFCellStyle createCellStyle(Workbook wb) {cellstyle (HSSFCellStyle) wb.createCellStyle();cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENwww.cppcns.comTER);cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);return cellstyle;}}step4:启动项目然后测试一下看完美的导出了。。。有图为证嗯嗯文章写到这里就结束啦Apache poi还提供了很多API在本例中为得到展示比如能够指定样式等等。以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持我们。本文标题: java后台利用Apache poi 生成excel文档提供前台下载示例本文地址: http://www.cppcns.com/ruanjian/java/190786.html