报表数据导出到excel,用jxl工具
- //导出到excel
- private boolean exportExcelOfJxl(List list, List<String> listHeader, String reportName) {
- WritableWorkbook wwb = null;
- boolean flag = true;
- Date now = new Date();
- SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
- try {
- //用Workbook类的工厂方法创建工作薄(Workbook)对象
- wwb = Workbook.createWorkbook(new File("d:/" + reportName + f.format(now) + ".xls"));
- } catch (IOException e) {
- e.printStackTrace();
- return flag = false;
- }
- if (wwb != null) {
- //创建一个可写入的工作表
- //Workbook的createSheet方法两个参数,1名称,2位置
- WritableSheet ws = wwb.createSheet(reportName, 0);
- //下面开始添加单元格
- //导出excel
- for (int n = 0; n < listHeader.size(); n++) {
- Label labelC = new Label(n, 0, listHeader.get(n));
- try {
- ws.addCell(labelC);
- } catch (RowsExceededException e) {
- e.printStackTrace();
- return flag = false;
- } catch (WriteException e) {
- e.printStackTrace();
- return flag = false;
- }
- }
- for (int i = 1; i < list.size(); i++) {
- for (int j = 0; j < listHeader.size(); j++) {
- //这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
- Object[] objs = (Object[]) list.get(i);
- Label labelC;
- if (objs[j] == null) {
- labelC = new Label(j, i, "");
- } else {
- labelC = new Label(j, i, objs[j] + "");
- }
- try {
- //将生成的单元格添加到工作表中
- ws.addCell(labelC);
- } catch (RowsExceededException e) {
- e.printStackTrace();
- return flag = false;
- } catch (WriteException e) {
- e.printStackTrace();
- return flag = false;
- }
- }
- }
- try {
- //从内存中写入文件中
- wwb.write();
- wwb.close();
- return flag;
- } catch (IOException e) {
- e.printStackTrace();
- return flag = false;
- } catch (WriteException e) {
- e.printStackTrace();
- return flag = false;
- }
- }
- return flag;
- }
报表数据导出到excel,用POI工具
- private boolean exportExcelOfPOI(List list, List<String> listHeader, String reportName) {
- boolean flag = true;
- Date now = new Date();
- SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
- // 创建新的Excel 工作簿
- HSSFWorkbook workbook = new HSSFWorkbook();
- //建一工作表
- HSSFSheet sheet = workbook.createSheet();
- workbook.setSheetName(0,reportName,HSSFWorkbook.ENCODING_UTF_16 );
- // HSSFSheet sheet = workbook.createSheet("报表");
- //1、创建字体,设置其为红色、粗体:
- HSSFFont font = workbook.createFont();
- font.setColor(HSSFFont.COLOR_RED);
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- //2、创建格式
- HSSFCellStyle cellStyle= workbook.createCellStyle();
- cellStyle.setFont(font);
- // 生成excel表头
- for (int n = 0; n < listHeader.size(); n++) {
- HSSFRow row = sheet.createRow((short) 0);
- HSSFCell cell = row.createCell((short) n);
- // 定义单元格为字符集
- cell.setEncoding(HSSFCell.ENCODING_UTF_16);
- // 定义单元格为字符串类型
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- //应用样式
- cell.setCellStyle(cellStyle);
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- // 单元格内容
- cell.setCellValue(listHeader.get(n));
- }
- for (int i = 1; i < list.size(); i++) {
- for (int j = 0; j < listHeader.size() - 1; j++) {
- Object[] objs = (Object[]) list.get(i);
- //再第二行开始
- HSSFRow row = sheet.createRow((short) i);
- //再第0列开始
- HSSFCell cell = row.createCell((short) j);
- // 定义单元格为字符集
- cell.setEncoding(HSSFCell.ENCODING_UTF_16);
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- // 单元格内容
- if (objs[j] == null) {
- cell.setCellValue("");
- } else {
- cell.setCellValue(objs[j] + "");
- }
- }
- }
- try {
- // 新建一输出文件流
- FileOutputStream fOut = new FileOutputStream("d:/" + reportName + f.format(now) + ".xls");
- // 把相应的Excel 工作簿存盘
- workbook.write(fOut);
- fOut.flush();
- // 操作结束,关闭文件
- fOut.close();
- return flag;
- } catch (Exception e) {
- return flag = false;
- }
- }