在Java Web中,将内容导出到Excel文档的方式有很多,本文主要介绍以POI的方式来进行导出。
情景:浏览器页面上显示有“本地下载”的超链接,点击超链接——后台查询数据库,将数据库的内容保存到Excel中——浏览器弹出下载框,开启下载流程。
POI需要的JAR包
需要两个JAR包:poi-3.9.jar、poi-ooxml-3.9.jar。
附上两个JAR包的下载链接:poi-3.9.jar、poi-ooxml-3.9.jar。
这里也推荐一个JAR包的下载地址:Maven Repository,在里面可以下载到基本全部的JAR包。
前端JSP页面设置
应为本文主要讲的是POI方式导出到Excel,所以就只采用最简单的Servlet的方式,不用Spring这些框架了。
<body> This is my JSP page. <br/> <a href="<c:url value="/DownLoadServlet" />">本地下载</a> </body>
点击超链接之后,就会跳转到DownLoadServlet的goGet(...)方法上。
通用实体类
本来导出Excel是没有实体类的,但为了能够使程序更通用化(就像是发送邮件的Mail一样),本文也提供一个实体类来进行导出Excel的操作。
具体实体类CommonExcel的定义如下:
import javax.servlet.http.HttpServletResponse; 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.HSSFRichTextString; 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.ss.util.CellRangeAddress; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; public class CommonExcel { // 显示的导出Excel表的标题(通常第一行) private String title; // 导出Excel表的列名(通常第二行) private String[] rowName; // 导出Excel表的文件名 private String fileName; // 导出Excel表的文件主题内容(通常从第三行往下的内容) // 需要注意的是:主要内容的格式为第一列为数字(序号),从第二列开始都是字符串 // 每一行为Object[]类型,很多行用List包装起来 private List<Object[]> dataList = new ArrayList<Object[]>(); // 用于下载的response private HttpServletResponse response; // 构造方法,传入要导出的数据 public CommonExcel(String title, String[] rowName, List<Object[]> dataList, HttpServletResponse response, String fileName) { this.dataList = dataList; this.rowName = rowName; this.title = title; this.response = response; this.fileName = fileName; } // 下载Excel的方法 public void downloadExcel() throws Exception { try { HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 创建工作表 // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); // sheet样式定义(两个函数均为自定义,可以自行修改) HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for (int n = 0; n < columnNum; n++) { HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } // 将查询出的数据设置到sheet对应的单元格中 for (int i = 0; i < dataList.size(); i++) { Object[] obj = dataList.get(i); //遍历每个对象 HSSFRow row = sheet.createRow(i + 3); //创建所需的行数 for (int j = 0; j < obj.length; j++) { HSSFCell cell = null; //设置单元格的数据类型 if (j == 0) { cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC); //第一列为数字 cell.setCellValue(i + 1); } else { cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING); //第二列起为字符串 if (!"".equals(obj[j]) && obj[j] != null) { cell.setCellValue(obj[j].toString()); //设置单元格的值 } } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if (colNum == 0) { sheet.setColumnWidth(colNum, (columnWidth - 2) * 256); } else { sheet.setColumnWidth(colNum, (columnWidth + 4) * 256); } } if (workbook != null) { try { if (response != null) { response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=\""+new String(fileName.getBytes("gb2312"),"ISO8859-1")); OutputStream out = response.getOutputStream(); workbook.write(out); out.close(); } else { FileOutputStream outputStream = new FileOutputStream("C:/"+fileName); workbook.write(outputStream); outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } // 列头单元格样式 public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 font.setFontHeightInPoints((short) 12); //字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("微软雅黑"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } // 列数据信息单元格样式 public HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体名字 font.setFontName("微软雅黑"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }
上面这段通用实体类CommonExcel可以原封不动地直接照抄,其中有几个部分需要注明一下:
- getColumnTopStyle()、getStyle()两个函数是对列头单元格(title和rowName)、列数据单元格(dataList)进行定义格式,可以自行修改;
- 默认规定列数据单元格,第一列为数字列,从第二列开始为字符串列。
具体每个成员在最终Excel中的位置,见下图:
后台Servlet层的逻辑代码
关于数据库的读取就不展开写了,就直接调用Service层的代码了。
主要的工作就是将CommonExcel中5个参数的主要四个参数(title、rowName、dataList、fileName)初始化并赋值,最后创建CommonExcel对象,调用downloadExcel()方法就行了。
public class DownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Service层获取数据库的数据 List<User> userList=userService.findAllUser(); // 定义标题(第一行) String title = "用户报表"; // 定义列名(第二行) String[] rowsName = new String[]{"序号","用户名","密码","邮箱","手机号","地址"}; // 定义主题内容(第三行起) List<Object[]> dataList = new ArrayList<Object[]>(); // 定义每一行的临时变量,并放入数据 Object[] objs = null; for (int i = 0; i < userList.size(); i++) { objs = new Object[rowsName.length]; objs[0] = i; objs[1] = userList.get(i).getUsername(); objs[2] = userList.get(i).getPassword(); objs[3] = userList.get(i).getEmail(); objs[4] = userList.get(i).getPhone(); objs[5] = userList.get(i).getAddress(); dataList.add(objs); } // 定义Excel文件名 String fileName="用户报表"+String.valueOf(System.currentTimeMillis()).substring(4,13)+".xls"; // 创建CommonExcel对象,调用downloadExcel()对象导出Excel CommonExcel ex = new CommonExcel(title, rowsName, dataList,response,fileName); try { ex.downloadExcel(); } catch (Exception e) { e.printStackTrace(); } } }
最终的点击页面超链接,弹出下载框:
直接点击下载,就可以下载Excel文件了,文件打开内容就是前文的Excel图片了。