1、poi-3.16.jar
2、poi-ooxml-3.16.jar
3、poi-ooxml-schemas-3.16.jar
4、commons-collections4-4.1.jar
5、xmlbeans-2.6.9.jar
注意版本号要统一否则会产生classNotFound的异常
二、编写一个poi打印的简单入门实例:
import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * 简单的poi报表打印类 * @author Administrator * */ public class PoiDemo { public static void main(String[] args) { //创建工作薄 Workbook wb = new HSSFWorkbook(); //用工作薄对象创建单元格格式对象 CellStyle cellStyle = wb.createCellStyle(); //用工作薄对象设置字体 Font font = wb.createFont(); //设置单元格格式 cellStyle.setFont(font); //创建工作表 Sheet sheet = wb.createSheet(); //创建行对象 Row row = sheet.createRow(0); //创建单元格对象 Cell cell = row.createCell(0); //设置单元格内容 cell.setCellValue("第一个行 第一个单元格的值"); //设置第二个单元格的值 Cell cell2 = row.createCell(1); cell2.setCellValue("第一行 第二个单元格"); //设置文件输出流 try { FileOutputStream out = new FileOutputStream("e:\\a.xls"); //将报表输出到指定的路径下 wb.write(out); //关流 out.close(); System.out.println("报表打印结束"); } catch (Exception e) { e.printStackTrace(); } } }关于设置单元格的样式,后续会进行补充。
三、介绍通过在线下载的方式实现表格的打印
实现方式是
1、点击按钮在后台触发事件。
2、在事件里面通过向response流里面进行写数据实现数据的导出。
3、注意不能发送ajax请求,通过location.href=url的方式进行发送请求。
创建流的代码如下:
OutputStream os = response.getOutputStream();// 取得输出流 response.reset();// 清空输出流 String filename = "XXX.xlsx"; response.setContentType("application/msexcel"); response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); 在创建了workbook之后将数据workbook.write(os); public void exportUserCount(String startTime, String endTime, String interval, HttpServletResponse response) { try { //创建工作薄对象 Workbook workbook = new SXSSFWorkbook(); //创建response输出流 OutputStream os = response.getOutputStream(); response.reset(); String filename = "工号统计报表"+startTime+"_"+endTime+".xlsx"; response.setContentType("application/msexcel"); response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); //创建工作表 Sheet sheet = workbook.createSheet(); //获取结果集 JSONObject result = loadUserId(startTime,endTime,interval); JSONArray lines = result.getJSONObject("data").getJSONArray("rows"); //创建表头 Row row = sheet.createRow(0); //设置每一列的宽度 sheet.setColumnWidth(0,25*256); sheet.setColumnWidth(1,25*256); sheet.setColumnWidth(2,25*256); sheet.setColumnWidth(3,25*256); sheet.setColumnWidth(4,25*256); //创建单元格的格式 字体加粗 CellStyle cellStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setBold(true); cellStyle.setFont(font); //设置标题行的内容 Cell cell = row.createCell(0); cell.setCellValue("时间段"); cell.setCellStyle(cellStyle); cell = row.createCell(1); cell.setCellValue("APhotline"); cell.setCellStyle(cellStyle); cell = row.createCell(2); cell.setCellValue("SSE"); cell.setCellStyle(cellStyle); cell = row.createCell(3); cell.setCellValue("PC(实时识别)"); cell.setCellStyle(cellStyle); cell = row.createCell(4); cell.setCellValue("PC(离线转写)"); cell.setCellStyle(cellStyle); //遍历结果集 for(int i = 0; i < lines.size(); i++) { JSONObject line = lines.getJSONObject(i); row = sheet.createRow(i+1); cell = row.createCell(0); cell.setCellValue(line.getString("time")); cell = row.createCell(1); cell.setCellValue(line.getString("APhotline")); cell = row.createCell(2); cell.setCellValue(line.getString("SSE")); cell = row.createCell(3); cell.setCellValue(line.getString("PCOnline")); cell = row.createCell(4); cell.setCellValue(line.getString("PCOffline")); } workbook.write(os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } }
