java导出excel
生活随笔
收集整理的這篇文章主要介紹了
java导出excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
請看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,//序號 0UploadConstants.PRO_EXPORT_TIT,//圖書名稱1UploadConstants.PRO_EXPORT_THDH,//是否中國標準書號2UploadConstants.PRO_EXPORT_THWLDH,//CIP分類號3UploadConstants.PRO_EXPORT_ISBN,//ISBN編碼4UploadConstants.PRO_EXPORT_THDZ,//新出或重印或租型5UploadConstants.PRO_EXPORT_ZS,//字數(千字)6UploadConstants.PRO_EXPORT_SFTS,//是否套書7UploadConstants.PRO_EXPORT_BCYSC,//本次印數(冊)8UploadConstants.PRO_EXPORT_MCDJ,//每冊、張定價(元)9UploadConstants.PRO_EXPORT_MCYZ,//每冊、張印張(印張)10UploadConstants.PRO_EXPORT_FZB,//分裝部(卷張冊)11UploadConstants.PRO_EXPORT_BCYST,//本次印數(套)12UploadConstants.PRO_EXPORT_MTDJ,//每套定價(元)13UploadConstants.PRO_EXPORT_MTYZ,//每套印張(印張)14UploadConstants.PRO_EXPORT_DJZJE,//定價總金額(元)15UploadConstants.PRO_EXPORT_CYGG,//常用規格16UploadConstants.PRO_EXPORT_ZDY,//自定義17UploadConstants.PRO_EXPORT_BCZYZ,//本次總印張(千印張)18UploadConstants.PRO_EXPORT_BOOK,//是否課本19UploadConstants.PRO_EXPORT_SFJF,//是否教輔讀物20UploadConstants.PRO_EXPORT_SFSE,//是否少兒讀物21UploadConstants.PRO_EXPORT_SFGJ,//是否古籍整理圖書22UploadConstants.PRO_EXPORT_TRAN,//是否翻譯圖書23UploadConstants.PRO_EXPORT_SIZE,//開本24UploadConstants.PRO_EXPORT_LANG,//文種(字)25UploadConstants.PRO_EXPORT_PACO,//頁碼26UploadConstants.PRO_EXPORT_FIA,//著(編、繪)譯者27UploadConstants.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) {/*******************************************************************************************///序號0sheet.addCell(new Label(0, line, Integer.toString(line)));//圖書名稱1if (product.getTitle() != null) {sheet.addCell(new Label(1, line, product.getTitle()));} else {sheet.addCell(new Label(1, line, ""));}//是否中國標準書號2sheet.addCell(new Label(2, line, ""));//CIP分類號3if (productPropList.get(0).getSubjectCode() != null) {sheet.addCell(new Label(3, line, productPropList.get(0).getSubjectCode()));} else {sheet.addCell(new Label(3, line, ""));}//ISBN編碼4if (product.getIsbn() != null) {sheet.addCell(new Label(4, line, product.getIsbn()));} else {sheet.addCell(new Label(4, line, ""));}//新出或重印或租型5if (productPropList.get(0).getBookRent() != null) {//2c9080904c53df44014c540fce60001dif(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//字數(千字)6if (productPropList.get(0).getPageNum() != null) {sheet.addCell(new Label(6, line, Integer.toString(productPropList.get(0).getPageNum())));} else {sheet.addCell(new Label(6, line, ""));}//套書7if (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, ""));}//本次印數(冊)8sheet.addCell(new Label(8, line, ""));//每冊、張定價(元)9if (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, "每冊、張定價(元)")); //每冊、張印張(印張)10sheet.addCell(new Label(10, line, ""));//分裝部(卷張冊)11sheet.addCell(new Label(11, line, ""));//本次印數(套)12sheet.addCell(new Label(12, line, ""));//每套定價(元)13sheet.addCell(new Label(13, line, ""));//每套印張(印張)14sheet.addCell(new Label(14, line, ""));//定價總金額(元)15if (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 pageSpecif (productPropList.get(0).getPageSpec() != null) {sheet.addCell(new Label(16, line, productPropList.get(0).getPageSpec()));} else {sheet.addCell(new Label(16, line, ""));}//自定義17sheet.addCell(new Label(17, line, ""));//本次總印張(千印張)18if (product.getTotalNum() != null) {sheet.addCell(new Label(18, line, Integer.toString(product.getTotalNum())));} else {sheet.addCell(new Label(18, line, ""));}//是否課本19if (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, ""));}//是否教輔讀物20sheet.addCell(new Label(20, line, ""));//是否少兒讀物21if (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, ""));}//是否古籍整理圖書22sheet.addCell(new Label(22, line, ""));//是否翻譯圖書23if (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, ""));}//開本24if (product.getSize() != null) {sheet.addCell(new Label(24, line, product.getSize()));} else {sheet.addCell(new Label(24, line, ""));}//文種(字)25if (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, ""));}//頁碼26if (productPropList.get(0).getPageCount() != null) {sheet.addCell(new Label(26, line, Integer.toString(productPropList.get(0).getPageCount())));} else {sheet.addCell(new Label(26, line, ""));}//著(編、繪)譯者27if (product.getFirstAuthor() != null) {sheet.addCell(new Label(27, line, product.getFirstAuthor()));} else {sheet.addCell(new Label(27, line, ""));}//備注28if ( productPropList.get(0).getRemark()!= null) {sheet.addCell(new Label(28, line, productPropList.get(0).getRemark()));} else {sheet.addCell(new Label(28, line, ""));}line++;}else{/*******************************************************************************************///序號0sheet.addCell(new Label(0, line, Integer.toString(line)));//圖書名稱1if (product.getTitle() != null) {sheet.addCell(new Label(1, line, product.getTitle()));} else {sheet.addCell(new Label(1, line, ""));}//是否中國標準書號2sheet.addCell(new Label(2, line, ""));//CIP分類號3sheet.addCell(new Label(3, line, ""));//ISBN編碼4if (product.getIsbn() != null) {sheet.addCell(new Label(4, line, product.getIsbn()));} else {sheet.addCell(new Label(4, line, ""));}//新出或重印或租型5sheet.addCell(new Label(5, line, ""));//字數(千字)6sheet.addCell(new Label(6, line, ""));//套書7sheet.addCell(new Label(7, line, ""));//本次印數(冊)8sheet.addCell(new Label(8, line, ""));//每冊、張定價(元)9if (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, "每冊、張定價(元)")); //每冊、張印張(印張)10sheet.addCell(new Label(10, line, ""));//分裝部(卷張冊)11sheet.addCell(new Label(11, line, ""));//本次印數(套)12sheet.addCell(new Label(12, line, ""));//每套定價(元)13sheet.addCell(new Label(13, line, ""));//每套印張(印張)14sheet.addCell(new Label(14, line, ""));//定價總金額(元)15if (product.getPrice() != null) {sheet.addCell(new Label(15, line, String.valueOf(product.getPrice())));} else {sheet.addCell(new Label(15, line, ""));}//常用規格16 pageSpecsheet.addCell(new Label(16, line, ""));//自定義17sheet.addCell(new Label(17, line, ""));//本次總印張(千印張)18if (product.getTotalNum() != null) {sheet.addCell(new Label(18, line, Integer.toString(product.getTotalNum())));} else {sheet.addCell(new Label(18, line, ""));}//是否課本19sheet.addCell(new Label(19, line, ""));//是否教輔讀物20sheet.addCell(new Label(20, line, ""));//是否少兒讀物21sheet.addCell(new Label(21, line, ""));//是否古籍整理圖書22sheet.addCell(new Label(22, line, ""));//是否翻譯圖書23sheet.addCell(new Label(23, line, ""));//開本24if (product.getSize() != null) {sheet.addCell(new Label(24, line, product.getSize()));} else {sheet.addCell(new Label(24, line, ""));}//文種(字)25sheet.addCell(new Label(25, line, ""));//頁碼26sheet.addCell(new Label(26, line, ""));//著(編、繪)譯者27if (product.getFirstAuthor() != null) {sheet.addCell(new Label(27, line, product.getFirstAuthor()));} else {sheet.addCell(new Label(27, line, ""));}//備注28sheet.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);}先貼代碼,過幾天再寫代碼注釋,講解,啥啥的。
轉載于:https://my.oschina.net/wswhr/blog/709836
總結
以上是生活随笔為你收集整理的java导出excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【自然语言处理】gensim的word2
- 下一篇: Qt支持的平台有那些?