zoukankan      html  css  js  c++  java
  • Excel表格

    1.表格下载工具类的封装

    public class ExcelUtils {
    
        public static Workbook createWorkBook(List<Map<String, Object>> list,String []keys,String columnNames[],Double total) {
            // 创建excel工作簿
            Workbook wb = new HSSFWorkbook();
            // 创建第一个sheet(页),并命名
            Sheet sheet = wb.createSheet(list.get(0).get("sheetName").toString());
            // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。
            for(int i=0;i<keys.length;i++){
                sheet.setColumnWidth((short) i, (short) (50 * 180));
            }
    
            // 创建第一行
            Row row = sheet.createRow((short) 0);
    
            // 创建两种单元格格式
            CellStyle cs = wb.createCellStyle();
            CellStyle cs2 = wb.createCellStyle();
    
            // 创建两种字体
            Font f = wb.createFont();
            Font f2 = wb.createFont();
    
            // 创建第一种字体样式(用于列名)
            f.setFontHeightInPoints((short) 15);
            f.setColor(IndexedColors.BLACK.getIndex());
            f.setBoldweight(Font.BOLDWEIGHT_BOLD);
    
            // 创建第二种字体样式(用于值)
            f2.setFontHeightInPoints((short) 11);
            f2.setColor(IndexedColors.BLACK.getIndex());
    
    //        Font f3=wb.createFont();
    //        f3.setFontHeightInPoints((short) 10);
    //        f3.setColor(IndexedColors.RED.getIndex());
    
            // 设置第一种单元格的样式(用于列名)
            cs.setFont(f);
            cs.setBorderLeft(CellStyle.BORDER_THIN);
            cs.setBorderRight(CellStyle.BORDER_THIN);
            cs.setBorderTop(CellStyle.BORDER_THIN);
            cs.setBorderBottom(CellStyle.BORDER_THIN);
            cs.setAlignment(CellStyle.ALIGN_CENTER);
    
            // 设置第二种单元格的样式(用于值)
            cs2.setFont(f2);
            cs2.setBorderLeft(CellStyle.BORDER_THIN);
            cs2.setBorderRight(CellStyle.BORDER_THIN);
            cs2.setBorderTop(CellStyle.BORDER_THIN);
            cs2.setBorderBottom(CellStyle.BORDER_THIN);
            cs2.setAlignment(CellStyle.ALIGN_CENTER);
            //设置列名
            for(int i=0;i<columnNames.length;i++){
                Cell cell = row.createCell(i);
                cell.setCellValue(columnNames[i]);
                cell.setCellStyle(cs);
            }
            //设置每行每列的值
            for (short i = 1; i <= list.size(); i++) {
                // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的
                // 创建一行,在页sheet上
                Row row1 = sheet.createRow((short) i);
                /*if(i==list.size()){
                    Cell cell = row1.createCell(5);
                    cell.setCellStyle(cs2);
                    cell.setCellValue("合计投资金额"+total);
                    break;
                }*/
                // 在row行上创建一个方格
                for(short j=0;j<keys.length;j++){
                    Cell cell = row1.createCell(j);
                    cell.setCellValue(list.get(i).get(keys[j]) == null?" ": list.get(i).get(keys[j]).toString());
                    cell.setCellStyle(cs2);
                }
            }
            return wb;
        }
        public static Workbook createWorkBook2(List<Map<String, Object>> list,String []keys,String columnNames[]) {
            // 创建excel工作簿
            Workbook wb = new HSSFWorkbook();
            // 创建第一个sheet(页),并命名
            Sheet sheet = wb.createSheet(list.get(0).get("sheetName").toString());
            // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。
            for(int i=0;i<keys.length;i++){
                sheet.setColumnWidth((short) i, (short) (38 * 190));
            }
    
            // 创建第一行
            Row row = sheet.createRow((short) 0);
    
            // 创建两种单元格格式
            CellStyle cs = wb.createCellStyle();
            CellStyle cs2 = wb.createCellStyle();
    
            // 创建两种字体
            Font f = wb.createFont();
            Font f2 = wb.createFont();
    
            // 创建第一种字体样式(用于列名)
            f.setFontHeightInPoints((short) 10);
            f.setColor(IndexedColors.BLACK.getIndex());
            f.setBoldweight(Font.BOLDWEIGHT_BOLD);
    
            // 创建第二种字体样式(用于值)
            f2.setFontHeightInPoints((short) 10);
            f2.setColor(IndexedColors.BLACK.getIndex());
    
    //        Font f3=wb.createFont();
    //        f3.setFontHeightInPoints((short) 10);
    //        f3.setColor(IndexedColors.RED.getIndex());
    
            // 设置第一种单元格的样式(用于列名)
            cs.setFont(f);
            cs.setBorderLeft(CellStyle.BORDER_THIN);
            cs.setBorderRight(CellStyle.BORDER_THIN);
            cs.setBorderTop(CellStyle.BORDER_THIN);
            cs.setBorderBottom(CellStyle.BORDER_THIN);
            cs.setAlignment(CellStyle.ALIGN_CENTER);
    
            // 设置第二种单元格的样式(用于值)
            cs2.setFont(f2);
            cs2.setBorderLeft(CellStyle.BORDER_THIN);
            cs2.setBorderRight(CellStyle.BORDER_THIN);
            cs2.setBorderTop(CellStyle.BORDER_THIN);
            cs2.setBorderBottom(CellStyle.BORDER_THIN);
            cs2.setAlignment(CellStyle.ALIGN_CENTER);
            //设置列名
            for(int i=0;i<columnNames.length;i++){
                Cell cell = row.createCell(i);
                cell.setCellValue(columnNames[i]);
                cell.setCellStyle(cs);
            }
            //设置每行每列的值
            for (short i = 1; i < list.size(); i++) {
                // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的
                // 创建一行,在页sheet上
                Row row1 = sheet.createRow((short) i);
                // 在row行上创建一个方格
                for(short j=0;j<keys.length;j++){
                    Cell cell = row1.createCell(j);
                    cell.setCellValue(list.get(i).get(keys[j]) == null?" ": list.get(i).get(keys[j]).toString());
                    cell.setCellStyle(cs2);
                }
            }
            return wb;
        }
    }

    2.service层的代码

    @Override
    public void entrustExcel(HttpServletRequest request, HttpServletResponse response, HQueryReqParams params) throws Exception { // TODO Auto-generated method stub String fileName = "excel文件"; List<Entrust> entrusts = tradeMapper.entrustListExcel(params); List<Map<String, Object>> list = createExcelRecord(entrusts); String columnNames[] = { "券商账号", "账号", "操盘手", "委托单号", "委托日期", "证券名称", "证券代码", "买卖方向", "状态", "委托价格", "委托数量", "成交价格", "成交数量", "成交金额", "券商委托编号"};// 列名 String keys[] = { "qnumber", "vaccountId", "userName", "entrustNo", "createTime", "sharesName", "sharesCode", "type", "entrustState", "entrustPrice", "entrustCount", "dealSingleMoney", "dealNum", "dealMoney", "entrustNoData" };// map中的key ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ExcelUtils.createWorkBook2(list, keys, columnNames).write(os); } catch (IOException e) { e.printStackTrace(); } byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); // 设置response参数,可以打开下载页面 response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (final IOException e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } } private List<Map<String, Object>> createExcelRecord(List<Entrust> entrusts) { /* * SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss"); * for(User list : user){ String date = * sdf.format(list.getCreatetime()); list.setDate(date); } */ List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("sheetName", "sheet1"); listmap.add(map); Entrust entrust = null; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss"); for (int j = 0; j < entrusts.size(); j++) { entrust = entrusts.get(j); Integer entrustState = entrust.getEntrustState(); String entrustStateStr = null; if (entrustState==1) { entrustStateStr="委托中"; }else if (entrustState==2) { entrustStateStr="全部成交"; }else if (entrustState==3) { entrustStateStr="部分成交"; }else if (entrustState==4) { entrustStateStr="全部撤单"; }else { entrustStateStr="部分撤单"; } Map<String, Object> mapValue = new HashMap<String, Object>(); mapValue.put("qnumber", entrust.getQnumber()); mapValue.put("vaccountId", entrust.getVaccountId()); mapValue.put("userName", entrust.getUserName()); // mapValue.put("telphone", entrust.getTelphone()); mapValue.put("entrustNo", entrust.getEntrustNo()); mapValue.put("createTime", sdf1.format(entrust.getCreateTime())); // mapValue.put("createTimeForHMS", sdf2.format(entrust.getCreateTimeForHMS())); mapValue.put("sharesName", entrust.getSharesName()); mapValue.put("sharesCode", entrust.getSharesCode()); mapValue.put("type", entrust.getType()==1?"买入":"卖出"); mapValue.put("entrustState", entrustStateStr); mapValue.put("entrustPrice", entrust.getEntrustPrice()); mapValue.put("entrustCount", entrust.getEntrustCount()); mapValue.put("dealSingleMoney", entrust.getDealSingleMoney()); mapValue.put("dealNum", entrust.getDealNum()); mapValue.put("dealMoney", entrust.getDealMoney()); mapValue.put("entrustNoData", entrust.getEntrustNoData()); listmap.add(mapValue); } return listmap; }

  • 相关阅读:
    笔记1
    笔记2
    笔记3
    两个多线程的交替打印
    三个多线程的交替打印
    内部类
    基本反射了解
    键盘监听事件KeyListener
    焦点监听事件FocusListener
    文本框JTextField,密码框JPasswordField
  • 原文地址:https://www.cnblogs.com/appc/p/8796986.html
Copyright © 2011-2022 走看看