龙空技术网

java导出图片到Excel,并通过浏览器下载Excel

Java老油条 111

前言:

如今你们对“java下载网络图片”都比较关心,看官们都需要分析一些“java下载网络图片”的相关内容。那么小编在网上汇集了一些有关“java下载网络图片””的相关资讯,希望大家能喜欢,我们一起来了解一下吧!

创建 Excel 表格

public void outExcel(Integer fileId, HttpServletResponse response){        //根据文件id查询档案对象        XzFile file = xzFileMapper.getFileById(fileId);        if (file==null || file.equals("")){            return;        }        //创建工作簿对象        HSSFWorkbook wb = new HSSFWorkbook();        //建立新的sheet对象(excel的表单)        HSSFSheet sheet = wb.createSheet(file.getName());        //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个        HSSFRow row = sheet.createRow(0);        //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个        HSSFCell cell = row.createCell(0);        //设置单元格的值        cell.setCellValue("登记卡");        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));        //多值判断        CellIsWhatUtil cellIsWhatUtil = new CellIsWhatUtil();        //在sheet中创建第二行        HSSFRow row2 = sheet.createRow(1);        //添加单元格中的内容        row2.createCell(0).setCellValue("姓名");        row2.createCell(1).setCellValue(file.getName());        row2.createCell(2).setCellValue("性别");        row2.createCell(3).setCellValue(cellIsWhatUtil.isSex(file.getSex()));        row2.createCell(4).setCellValue("联系方式");        row2.createCell(5).setCellValue(file.getMobile());        HSSFCell cell6 = row2.createCell(6);        if (file.getIdPhoto() == null || file.getIdPhoto().equals("")) {              cell6.setCellValue("贴相片处\n" + "(2寸白底)");        } else {            // 利用HSSFPatriarch将图片写入EXCEL            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//拿到图片的二进制数据   cellIsWhatUtil:图片转为二进制工具类            byte[] imgData = cellIsWhatUtil.getImgData(file.getIdPhoto());            //anchor主要用于设置图片的属性            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 7, 1, (short) 6, 4);            //Sets the anchor type (图片在单元格的位置)            //0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.            anchor.setAnchorType(3);            patriarch.createPicture(anchor, wb.addPicture(imgData, HSSFWorkbook.PICTURE_TYPE_JPEG));        }        sheet.addMergedRegion(new CellRangeAddress(1, 4, 6, 6));        //在sheet中创建第3行        HSSFRow row3 = sheet.createRow(2);        //添加单元格中的内容        row3.createCell(0).setCellValue("身份证号");        row3.createCell(1).setCellValue(file.getPerId());        row3.createCell(2).setCellValue("文化程度");        row3.createCell(3).setCellValue(cellIsWhatUtil.isEdu(file.getEduDegree()));        row3.createCell(4).setCellValue("民族");        row3.createCell(5).setCellValue(file.getNation());        //在sheet中创建第4行        HSSFRow row4 = sheet.createRow(3);        //添加单元格中的内容        row4.createCell(0).setCellValue("入伍时间");        row4.createCell(1).setCellValue(cellIsWhatUtil.timeToString(file.getEnlistmentTime()));        row4.createCell(2).setCellValue("退役时间");        row4.createCell(3).setCellValue(cellIsWhatUtil.timeToString(file.getRetirementTime()));        row4.createCell(4).setCellValue("待遇批准时间");        row4.createCell(5).setCellValue(cellIsWhatUtil.timeToString(file.getTreatmentTime()));        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列        sheet.addMergedRegion(new CellRangeAddress(4, 4, 3, 5));        //在sheet中创建第5行        HSSFRow row5 = sheet.createRow(4);        //添加单元格中的内容        row5.createCell(0).setCellValue("政治面貌");        row5.createCell(1).setCellValue(cellIsWhatUtil.isGood(file.getPolOutlook()));        row5.createCell(2).setCellValue("户籍所在地");        row5.createCell(3).setCellValue(file.getHometown());        //在sheet中创建第6行        HSSFRow row6 = sheet.createRow(5);        //添加单元格中的内容        row6.createCell(0).setCellValue("人员类别");        row6.createCell(1).setCellValue(cellIsWhatUtil.isPer(file.getPerCategory(), file));        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列        sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 6));        //在sheet中创建第7行        HSSFRow row7 = sheet.createRow(6);        //添加单元格中的内容        row7.createCell(0).setCellValue("变更情况");        row7.createCell(1).setCellValue(file.getChanges());        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列        sheet.addMergedRegion(new CellRangeAddress(6, 6, 1, 6));        //在sheet中创建第8行        HSSFRow row8 = sheet.createRow(7);        //添加单元格中的内容        row8.createCell(0).setCellValue("备注:");        row8.createCell(1).setCellValue(file.getRemark());        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列        sheet.addMergedRegion(new CellRangeAddress(7, 7, 1, 6));        //设置每行的高度        row.setHeightInPoints(30);        row2.setHeightInPoints(20);        row3.setHeightInPoints(20);        row4.setHeightInPoints(20);        row5.setHeightInPoints(30);        row6.setHeightInPoints(30);        row7.setHeightInPoints(30);        row8.setHeightInPoints(30);        //设置每列的宽度        int[] width = {3000, 3000, 3000, 3000, 3000, 3000, 3000};        for (int i = 0; i < width.length; i++) {            sheet.setColumnWidth(i, width[i]);        }        // row1        HSSFCellStyle cellStyle = wb.createCellStyle();        HSSFFont fontStyle = wb.createFont();        /*//设置上下左右四个边框宽度        cellStyle.setBorderTop(HSSFBorderFormatting.BORDER_THIN);        cellStyle.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);        cellStyle.setBorderLeft(HSSFBorderFormatting.BORDER_THIN);        cellStyle.setBorderRight(HSSFBorderFormatting.BORDER_THIN);        //设置上下左右四个边框颜色        cellStyle.setTopBorderColor(HSSFColor.RED.index);        cellStyle.setBottomBorderColor(HSSFColor.RED.index);        cellStyle.setLeftBorderColor(HSSFColor.RED.index);        cellStyle.setRightBorderColor(HSSFColor.RED.index);*/        //水平居中        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);        //垂直居中        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);        //设置自动换行        cellStyle.setWrapText(true);        //设置字体样式        fontStyle.setFontName("宋体");        //设置字体高度        fontStyle.setFontHeightInPoints((short) 15);        //设置字体颜色        fontStyle.setColor(HSSFColor.BLACK.index);        //设置粗体        fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);        //设置斜体//        fontStyle.setItalic(true);        //设置下划线//        fontStyle.setUnderline(HSSFFont.U_SINGLE);        //字体也是单元格格式的一部分,所以从属于HSSFCellStyle        // 将字体对象赋值给单元格样式对象        cellStyle.setFont(fontStyle);        // 将单元格样式应用于单元格        cell.setCellStyle(cellStyle);        // row2,3,4        HSSFCellStyle cellStyle2 = wb.createCellStyle();        HSSFFont fontStyle2 = wb.createFont();        //水平居中        cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);        //垂直居中        cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);        //设置自动换行        cellStyle2.setWrapText(true);        //设置字体样式        fontStyle2.setFontName("宋体");        //设置字体高度        fontStyle2.setFontHeightInPoints((short) 7);        //设置字体颜色        fontStyle2.setColor(HSSFColor.BLACK.index);        //设置粗体        fontStyle2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);        // 将字体对象赋值给单元格样式对象        cellStyle2.setFont(fontStyle2);        //第二行        for (int i = 0; i < 7; i++) {            row2.getCell(i).setCellStyle(cellStyle2);        }        //第三四行        for (int i = 0; i < 6; i++) {            row3.getCell(i).setCellStyle(cellStyle2);            row4.getCell(i).setCellStyle(cellStyle2);        }        //第五行        for (int i = 0; i < 4; i++) {            row5.getCell(i).setCellStyle(cellStyle2);        }        //第6,7,8行        for (int i = 0; i < 2; i++) {            row6.getCell(i).setCellStyle(cellStyle2);            row7.getCell(i).setCellStyle(cellStyle2);            row8.getCell(i).setCellStyle(cellStyle2);        }        //输出Excel文件//        FileOutputStream fileOutputStream = null;        OutputStream output;        try {            output = response.getOutputStream();            //清空缓存            response.reset();            //定义浏览器响应表头,顺带定义下载名,比如students(中文名需要转义)            String s = file.getName() + file.getBirthday();            response.setHeader("Content-disposition", "attachment;filename=" + new String(s.getBytes(), "iso-8859-1") + ".xls");            //定义下载的类型,标明是excel文件            response.setContentType("application/vnd.ms-excel");            //这时候把创建好的excel写入到输出流            wb.write(output);            //养成好习惯,出门记得随手关门            output.close();        } catch (IOException e) {            e.printStackTrace();        }    }复制代码
cellIsWhatUtil工具类:拿到图片的二进制数据复制代码
    public byte[] getImgData(String pictureUrl){        if(StringUtils.isNotBlank(pictureUrl)) {            if (pictureUrl==null || pictureUrl.equals("")){                return null;            }      //图片地址前缀            String str = "http://*****/temp/";            //路径有汉字需要转码    String url = “****/test?condition=”+URLEncoder.encode("工厂","utf-8");            int i = pictureUrl.lastIndexOf("/");            String substring = pictureUrl.substring(i+1);            URL url = null;            try {                url = new URL(str+ URLEncoder.encode(substring,"utf-8"));                //打开链接                HttpURLConnection conn = (HttpURLConnection)url.openConnection();                //设置请求方式为"GET"                conn.setRequestMethod("GET");                //超时响应时间为5秒                conn.setConnectTimeout(5 * 1000);                //通过输入流获取图片数据                InputStream inStream = conn.getInputStream();                //得到图片的二进制数据,以二进制封装得到数据,具有通用性                byte[] data = readInputStream(inStream);// 得到图片的二进制数据                return data;            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (ProtocolException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } catch (Exception e) {                e.printStackTrace();            }        }        return null;    }

作者:zagwk

链接:

来源:稀土掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签: #java下载网络图片 #从java导出excel #java按模板导出excel #java使用模板导出excel #java如何将图片导入数据库