java导出excel

来源:互联网 发布:软件导刊教育技术 编辑:程序博客网 时间:2024/06/10 01:17

请看excel分类,其他的博客,下面的博客写的比较早,没啥太大利用价值,纯粹是复制代码



springMVC,

但是,原理应该是一样的,直接贴代码。

因为最近要写一个excel上传数据的,所以,先把下载excel的代码贴上来。

@SuppressWarnings({ "unchecked", "rawtypes" })@RequestMapping(value = "/form/downs/downloadExcel")    public ResponseEntity<byte[]> downloadExcelCopy(PProductForm form) throws Exception {    byte[] data = null;        HttpHeaders header = new HttpHeaders();        try {        Map<String, Object> condition = new HashMap<>();        Map<String, Object> accountInfo = (HashMap) this.request.getSession().getAttribute("pt_accountInfo");        String employeeId = (String) accountInfo.get("employeeId");        if (Strings.isNullOrEmpty(employeeId)) {        } else {        List<String> proids = new ArrayList<>();                for (String taskId : form.getSubIds().split(",")) {                proids.add(taskId);                }                System.out.println("*************************IDS是:"+form.getSubIds());                WritableWorkbook workbook;            String absolutePath = Param.getParam("ebmep.root.path").get("dir").replace("-", ":");            String folder = Param.getParam("download.directory.config").get("product");            Properties props = new Properties();            InputStream in = this.getClass().getResourceAsStream("/product_custom_code.properties");            props.load(in);            String fileName = props.getProperty("excel_tsjbdc_name");            String path = folder + File.separator + fileName;            FileUtil.newFolder(absolutePath + folder);            File file = new File(absolutePath + path);            workbook = jxl.Workbook.createWorkbook(file);            WritableSheet sheet = workbook.createSheet(fileName, 0);            String[] title = {            UploadConstants.PRO_EXPORT_ORDER,//序号 0            UploadConstants.PRO_EXPORT_TIT,//图书名称1            UploadConstants.PRO_EXPORT_THDH,//是否中国标准书号2            UploadConstants.PRO_EXPORT_THWLDH,//CIP分类号3            UploadConstants.PRO_EXPORT_ISBN,//ISBN编码4            UploadConstants.PRO_EXPORT_THDZ,//新出或重印或租型5            UploadConstants.PRO_EXPORT_ZS,//字数(千字)6            UploadConstants.PRO_EXPORT_SFTS,//是否套书7            UploadConstants.PRO_EXPORT_BCYSC,//本次印数(册)8            UploadConstants.PRO_EXPORT_MCDJ,//每册、张定价(元)9            UploadConstants.PRO_EXPORT_MCYZ,//每册、张印张(印张)10            UploadConstants.PRO_EXPORT_FZB,//分装部(卷张册)11            UploadConstants.PRO_EXPORT_BCYST,//本次印数(套)12            UploadConstants.PRO_EXPORT_MTDJ,//每套定价(元)13            UploadConstants.PRO_EXPORT_MTYZ,//每套印张(印张)14            UploadConstants.PRO_EXPORT_DJZJE,//定价总金额(元)15            UploadConstants.PRO_EXPORT_CYGG,//常用规格16            UploadConstants.PRO_EXPORT_ZDY,//自定义17            UploadConstants.PRO_EXPORT_BCZYZ,//本次总印张(千印张)18            UploadConstants.PRO_EXPORT_BOOK,//是否课本19            UploadConstants.PRO_EXPORT_SFJF,//是否教辅读物20            UploadConstants.PRO_EXPORT_SFSE,//是否少儿读物21            UploadConstants.PRO_EXPORT_SFGJ,//是否古籍整理图书22            UploadConstants.PRO_EXPORT_TRAN,//是否翻译图书23            UploadConstants.PRO_EXPORT_SIZE,//开本24            UploadConstants.PRO_EXPORT_LANG,//文种(字)25            UploadConstants.PRO_EXPORT_PACO,//页码26            UploadConstants.PRO_EXPORT_FIA,//著(编、绘)译者27            UploadConstants.PRO_EXPORT_REM,//备注28            };            for (int i = 0; i < title.length; i++) {            sheet.addCell(new Label(i, 0, title[i]));            }            int line = 1;            for (String proId : proids) {            PProduct product = this.productService.getProduct(proId);            condition.clear();            condition.put("productId", product.getId());            List<PProductStylebook> productPropList = this.productStylebookService.getProductPropList(condition, null);            Map<String, Object> productStylebookMap = new HashMap<>();            for (PProductStylebook pProductStylebook : productPropList) {            productStylebookMap.put(pProductStylebook.getSubjectCode(), pProductStylebook.getBookPage());            }            if (product != null && productPropList.size() > 0) {            /*******************************************************************************************/            //序号0            sheet.addCell(new Label(0, line, Integer.toString(line)));                        //图书名称1            if (product.getTitle() != null) {            sheet.addCell(new Label(1, line, product.getTitle()));            } else {            sheet.addCell(new Label(1, line, ""));            }                        //是否中国标准书号2            sheet.addCell(new Label(2, line, ""));                        //CIP分类号3            if (productPropList.get(0).getSubjectCode() != null) {            sheet.addCell(new Label(3, line, productPropList.get(0).getSubjectCode()));            } else {            sheet.addCell(new Label(3, line, ""));            }            //ISBN编码4            if (product.getIsbn() != null) {            sheet.addCell(new Label(4, line, product.getIsbn()));            } else {            sheet.addCell(new Label(4, line, ""));            }            //新出或重印或租型5            if (productPropList.get(0).getBookRent() != null) {//2c9080904c53df44014c540fce60001d            if(this.dicClassService.getDic(productPropList.get(0).getBookRent()) != null){            sheet.addCell(new Label(5, line, this.dicClassService.getDic(productPropList.get(0).getBookRent()).getName()));            }else{            sheet.addCell(new Label(5, line, productPropList.get(0).getBookRent()));            }                        } else {            sheet.addCell(new Label(5, line, ""));            }            //sheet.addCell(new Label(, line, "新出或重印或租型"));   bookRent                        //字数(千字)6            if (productPropList.get(0).getPageNum() != null) {            sheet.addCell(new Label(6, line, Integer.toString(productPropList.get(0).getPageNum())));            } else {            sheet.addCell(new Label(6, line, ""));            }                        //套书7            if (productPropList.get(0).getBooksFlg() != null) {            if(this.dicClassService.getDic(productPropList.get(0).getBooksFlg())!=null ){            sheet.addCell(new Label(7, line, this.dicClassService.getDic(productPropList.get(0).getBooksFlg()).getName()));            }else{            sheet.addCell(new Label(7, line, productPropList.get(0).getBooksFlg()));            }                        } else {            sheet.addCell(new Label(7, line, ""));            }                        //本次印数(册)8            sheet.addCell(new Label(8, line, ""));            //每册、张定价(元)9            if (product.getPrice() != null) {            sheet.addCell(new Label(9, line, String.valueOf(product.getPrice())));            } else {            sheet.addCell(new Label(9, line, ""));            }            //sheet.addCell(new Label(, line, "每册、张定价(元)"));              //每册、张印张(印张)10            sheet.addCell(new Label(10, line, ""));            //分装部(卷张册)11            sheet.addCell(new Label(11, line, ""));            //本次印数(套)12            sheet.addCell(new Label(12, line, ""));            //每套定价(元)13            sheet.addCell(new Label(13, line, ""));            //每套印张(印张)14            sheet.addCell(new Label(14, line, ""));            //定价总金额(元)15            if (product.getPrice() != null) {            sheet.addCell(new Label(15, line, String.valueOf(product.getPrice())));            } else {            sheet.addCell(new Label(15, line, ""));            }            //sheet.addCell(new Label(15, line, "定价总金额(元)")); getPrice            //常用规格16   pageSpec            if (productPropList.get(0).getPageSpec() != null) {            sheet.addCell(new Label(16, line, productPropList.get(0).getPageSpec()));            } else {            sheet.addCell(new Label(16, line, ""));            }                        //自定义17            sheet.addCell(new Label(17, line, ""));                        //本次总印张(千印张)18            if (product.getTotalNum() != null) {            sheet.addCell(new Label(18, line, Integer.toString(product.getTotalNum())));            } else {            sheet.addCell(new Label(18, line, ""));            }                        //是否课本19            if (productPropList.get(0).getBook() != null) {            if(this.dicClassService.getDic(productPropList.get(0).getBook())!=null){            sheet.addCell(new Label(19, line,this.dicClassService.getDic(productPropList.get(0).getBook()).getName() ));            }else{            sheet.addCell(new Label(19, line,productPropList.get(0).getBook()));            }                        } else {            sheet.addCell(new Label(19, line, ""));            }                        //是否教辅读物20            sheet.addCell(new Label(20, line, ""));                        //是否少儿读物21            if (productPropList.get(0).getChildBook() != null) {            if(this.dicClassService.getDic(productPropList.get(0).getChildBook())!=null){            sheet.addCell(new Label(21, line, this.dicClassService.getDic(productPropList.get(0).getChildBook()).getName()));            }else{            sheet.addCell(new Label(21, line, productPropList.get(0).getChildBook()));            }                        } else {            sheet.addCell(new Label(21, line, ""));            }                        //是否古籍整理图书22            sheet.addCell(new Label(22, line, ""));                        //是否翻译图书23            if (productPropList.get(0).getTranslateBook() != null) {            if(this.dicClassService.getDic(productPropList.get(0).getTranslateBook())!=null){            sheet.addCell(new Label(23, line,this.dicClassService.getDic(productPropList.get(0).getTranslateBook()).getName() ));            }else{            sheet.addCell(new Label(23, line,productPropList.get(0).getTranslateBook()  ));            }                        } else {            sheet.addCell(new Label(23, line, ""));            }                        //开本24            if (product.getSize() != null) {            sheet.addCell(new Label(24, line, product.getSize()));            } else {            sheet.addCell(new Label(24, line, ""));            }                        //文种(字)25                        if (productPropList.get(0).getLanguage() != null) {            if(this.dicClassService.getDic(productPropList.get(0).getLanguage())!=null){            sheet.addCell(new Label(25, line, this.dicClassService.getDic(productPropList.get(0).getLanguage()).getName()));            }else{            sheet.addCell(new Label(25, line, productPropList.get(0).getLanguage()));            }                        } else {            sheet.addCell(new Label(25, line, ""));            }                        //页码26            if (productPropList.get(0).getPageCount() != null) {            sheet.addCell(new Label(26, line, Integer.toString(productPropList.get(0).getPageCount())));            } else {            sheet.addCell(new Label(26, line, ""));            }                        //著(编、绘)译者27            if (product.getFirstAuthor() != null) {            sheet.addCell(new Label(27, line, product.getFirstAuthor()));            } else {            sheet.addCell(new Label(27, line, ""));            }                        //备注28            if ( productPropList.get(0).getRemark()!= null) {            sheet.addCell(new Label(28, line, productPropList.get(0).getRemark()));            } else {            sheet.addCell(new Label(28, line, ""));            }                        line++;            }else{            /*******************************************************************************************/            //序号0            sheet.addCell(new Label(0, line, Integer.toString(line)));                        //图书名称1            if (product.getTitle() != null) {            sheet.addCell(new Label(1, line, product.getTitle()));            } else {            sheet.addCell(new Label(1, line, ""));            }                        //是否中国标准书号2            sheet.addCell(new Label(2, line, ""));                        //CIP分类号3            sheet.addCell(new Label(3, line, ""));            //ISBN编码4            if (product.getIsbn() != null) {            sheet.addCell(new Label(4, line, product.getIsbn()));            } else {            sheet.addCell(new Label(4, line, ""));            }            //新出或重印或租型5            sheet.addCell(new Label(5, line, ""));                        //字数(千字)6            sheet.addCell(new Label(6, line, ""));                        //套书7            sheet.addCell(new Label(7, line, ""));                        //本次印数(册)8            sheet.addCell(new Label(8, line, ""));            //每册、张定价(元)9            if (product.getPrice() != null) {            sheet.addCell(new Label(9, line, String.valueOf(product.getPrice())));            } else {            sheet.addCell(new Label(9, line, ""));            }            //sheet.addCell(new Label(, line, "每册、张定价(元)"));              //每册、张印张(印张)10            sheet.addCell(new Label(10, line, ""));            //分装部(卷张册)11            sheet.addCell(new Label(11, line, ""));            //本次印数(套)12            sheet.addCell(new Label(12, line, ""));            //每套定价(元)13            sheet.addCell(new Label(13, line, ""));            //每套印张(印张)14            sheet.addCell(new Label(14, line, ""));            //定价总金额(元)15            if (product.getPrice() != null) {            sheet.addCell(new Label(15, line, String.valueOf(product.getPrice())));            } else {            sheet.addCell(new Label(15, line, ""));            }                        //常用规格16   pageSpec            sheet.addCell(new Label(16, line, ""));                        //自定义17            sheet.addCell(new Label(17, line, ""));                        //本次总印张(千印张)18            if (product.getTotalNum() != null) {            sheet.addCell(new Label(18, line, Integer.toString(product.getTotalNum())));            } else {            sheet.addCell(new Label(18, line, ""));            }                        //是否课本19            sheet.addCell(new Label(19, line, ""));                        //是否教辅读物20            sheet.addCell(new Label(20, line, ""));                        //是否少儿读物21            sheet.addCell(new Label(21, line, ""));                        //是否古籍整理图书22            sheet.addCell(new Label(22, line, ""));                        //是否翻译图书23            sheet.addCell(new Label(23, line, ""));                        //开本24            if (product.getSize() != null) {            sheet.addCell(new Label(24, line, product.getSize()));            } else {            sheet.addCell(new Label(24, line, ""));            }                        //文种(字)25            sheet.addCell(new Label(25, line, ""));                        //页码26            sheet.addCell(new Label(26, line, ""));                        //著(编、绘)译者27            if (product.getFirstAuthor() != null) {            sheet.addCell(new Label(27, line, product.getFirstAuthor()));            } else {            sheet.addCell(new Label(27, line, ""));            }                        //备注28            sheet.addCell(new Label(28, line, ""));                        line++;                        }            }                        workbook.write();            workbook.close();            //下载            header.setContentType(MediaType.parseMediaType("application/x-msdownload"));            header.set("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));            data = FileUtils.readFileToByteArray(new File(absolutePath + path));        }} catch (Exception e) {e.printStackTrace();}        return new ResponseEntity<byte[]>(data, header, HttpStatus.OK);    }

先贴代码,过几天再写代码注释,讲解,啥啥的。


0 0
原创粉丝点击