java批量导入数据到excel
生活随笔
收集整理的這篇文章主要介紹了
java批量导入数据到excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.查詢并返回結果list集合
private List<TbTaxOrder> tbTaxOrderList = null; @RestController @RequestMapping("/core/order") public class TbTaxOrderController {public static final Logger LOGGER = Logger.getLogger("TbTaxOrderController");@Autowiredprivate ITbTaxOrderService tbTaxOrderService;@Autowiredprivate ITbTaxOilStationService stationService;private List<TbTaxOrder> tbTaxOrderList = null;/*** 根據取票方式和取票狀態查詢* @param params* @return*/@RequestMapping(value = "/getByMethodAndStatus", method = RequestMethod.POST)public ResponseVO getByReceiveMethodAndOrderStatus(@RequestBody Map<String, Object> params) {try{String receiveMethod = (String) params.get("receiveMethod");String orderStatus = (String) params.get("orderStatus");String stationStaffId = (String) params.get("stationStaffId");if(StringUtil.isNotEmpty(orderStatus)){String[] list = orderStatus.split(",");params.put("list",list);}if (StringUtil.isEmpty(receiveMethod) || StringUtil.isEmpty(stationStaffId)) {return ResultUtil.error("receiveMethod || stationStaffId 為空");}tbTaxOrderList = tbTaxOrderService.getByReceiveMethodAndOrderStatus(params);return ResultUtil.success(tbTaxOrderList);}catch (Exception e){e.printStackTrace();if(e instanceof TaxException){return ResultUtil.error(e.getMessage());}return ResultUtil.error();}}/*** 導出開票記錄到excel* @return*/@RequestMapping(value = "/DownLoadExcel", method = RequestMethod.POST)public ResponseVO DownLoadExcel() {try{//格式化時間SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//1、創建工作簿Workbook wb = new XSSFWorkbook();//1.1、設置表格的格式----居中CellStyle cs = wb.createCellStyle();// cs.setAlignment(HorizontalAlignment.CENTER);//2.1、創建工作表Sheet sheet = wb.createSheet("Excel download");//2.2、合并單元格 // sheet.addMergedRegion(new CellRangeAddress(4, 8, 5, 9));//3.1、創建行----表頭行Row row = sheet.createRow(0);//4、創建格Cell cell = row.createCell(0);cell.setCellValue("油卡號");cell.setCellStyle(cs);cell = row.createCell(1);cell.setCellValue("油卡手機號");cell.setCellStyle(cs);cell = row.createCell(2);cell.setCellValue("開票金額");cell.setCellStyle(cs);cell = row.createCell(3);cell.setCellValue("截止時間");cell.setCellStyle(cs);cell = row.createCell(4);cell.setCellValue("公司名稱");cell.setCellStyle(cs);cell = row.createCell(5);cell.setCellValue("開票申請時間");cell.setCellStyle(cs);cell = row.createCell(6);cell.setCellValue("取票狀態");cell.setCellStyle(cs);cell = row.createCell(7);cell.setCellValue("取票方式");cell.setCellStyle(cs);//5、寫入實體數據if(null != tbTaxOrderList && tbTaxOrderList.size() > 0){for (int i = 0; i < tbTaxOrderList.size(); i++) {//3.2、創建行----內容行row = sheet.createRow(i+1);TbTaxOrder order = (TbTaxOrder)tbTaxOrderList.get(i);//第幾行第幾格 第一行第一格為“code”row.createCell(0).setCellValue(order.getCardCode());row.createCell(1).setCellValue(order.getBindPhone());if(null != order.getBillAmount()){row.createCell(2).setCellValue(order.getBillAmount().toString());}else{row.createCell(2).setCellValue("");}if(null != order.getEndDate()){String endDate = formatter.format(order.getEndDate());row.createCell(3).setCellValue(endDate);}else{row.createCell(3).setCellValue(order.getEndDate());}row.createCell(4).setCellValue(order.getCompany());String insertDate = formatter.format(order.getInsertDate());row.createCell(5).setCellValue(insertDate);if("1".toString().equals(order.getReceiveMethod())){row.createCell(6).setCellValue("未辦理");}else if("2".toString().equals(order.getReceiveMethod())){row.createCell(6).setCellValue("開票中");}else if("3".toString().equals(order.getReceiveMethod())){row.createCell(6).setCellValue("開票完成");}else if("4".toString().equals(order.getReceiveMethod())){row.createCell(6).setCellValue("開票請求駁回");}else {row.createCell(6).setCellValue("已郵寄 或 已自提");}if("1".toString().equals(order.getReceiveMethod())){row.createCell(7).setCellValue("默認不選擇");}else if("12".toString().equals(order.getReceiveMethod())){row.createCell(7).setCellValue("場站自取");}else{row.createCell(7).setCellValue("郵寄");}}//6、將文件儲存到指定位置try {FileOutputStream fout = new FileOutputStream("D:\\" + new Date().getTime() + ".xlsx");wb.write(fout);fout.close();return ResultUtil.success("導出完成");} catch (IOException e) {e.printStackTrace();return ResultUtil.error("導出失敗");}}else{return ResultUtil.error("tbTaxOrderList 不能為空");}}catch (Exception e){e.printStackTrace();if(e instanceof TaxException){return ResultUtil.error(e.getMessage());}return ResultUtil.error();}}}?
?
總結
以上是生活随笔為你收集整理的java批量导入数据到excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis中的查询
- 下一篇: 编程相关的学习网站