Java POI的导出

废话不多说 很简单 读取模板的导出 总体思路就是 先读取模板 然后将数据导入到模板中 最后导出

@RequestMapping(value = "/exportInfo", method = RequestMethod.GET)
	public void exportInfo(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("text/html");
		boolean flag = false;
		String bdid = "2008";
//		String path = request.getParameter("path");
		String fileName = "11";
		String filePath = request.getSession().getServletContext().getRealPath("/uploads/excle/models/aa.xlsx");  
		//设置响应头  用户点击后形成一个下载窗口,让用户保存
		response.addHeader("Content-Disposition", "attachment;filename="  
                + new String(fileName.getBytes("gb2312"),"ISO8859-1")+".xls");  
        OutputStream toClient = new BufferedOutputStream(  
                response.getOutputStream());  
        response.setContentType("application/vnd.ms-excel;charset=gb2312");  
		OutputStream out = response.getOutputStream();
		PrintWriter print = null;
		ExportExcel xportExcel = new ExportExcel();
			//弹出对话框
//			String adr = exportExcel.selectSavePath();
			ImportParam persons = new ImportParam();
			String bdids[] = bdid.split(",");
			List<Import> list = iequipment.getAll(bdids);//获取详细设备信息
			ExportExcel<Import> ex = new ExportExcel<Import>();
			String examname = list.get(0).getBdid();
			ex.ExportInfo(filePath,list,out);
			try {
				out.close();
				toClient.flush();
				toClient.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
	}
  public void ExportInfo(String filePath,List<Import> list, OutputStream out){
    	XSSFWorkbook workbook = null;
		try {
			workbook = new XSSFWorkbook(new FileInputStream(filePath));//读取excel模板  
			 XSSFSheet sheet = workbook.getSheetAt(0);   //读取第一个工作簿  
			  XSSFRow row;  
			  XSSFCell cell = null;  
			  int rownum = 3; //添加的起始行  
			  XSSFCellStyle style = this.getStyle(workbook);  
			  Iterator<Import> it = list.iterator();  
			  Import order = null;
			   while(it.hasNext()) {  
                   order = new Import();  
                   order = it.next();  
                   row = sheet.createRow(rownum);  
                   myCreateCell(0, String.valueOf(rownum-2), row, cell, style);    //列1  
                   myCreateCell(1, order.getTheater(), row, cell, style);    //列1  
                   myCreateCell(2, order.getSubplot(),row, cell, style);   //列2  
                   myCreateCell(3, order.getMilitary(), row, cell, style);    //列3   
                   myCreateCell(4, order.getDivisions(), row, cell, style);     
                   myCreateCell(5, order.getMilitary(), row, cell, style);     
                   myCreateCell(6, order.getRegimental(), row, cell, style);      
                   myCreateCell(7, order.getBattalion(), row, cell, style);     
                   myCreateCell(8, order.getCompany(), row, cell, style);      
                   myCreateCell(9, order.getPlatoon(), row, cell, style);      
                   myCreateCell(10, order.getClasss(), row, cell, style);
                   myCreateCell(11, order.getBdid(), row, cell, style);   
                   rownum++;  
               }
			   workbook.write(out);  
//               os.flush();  
//               os.close(); 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值