java解析,生成excel文件

阅读更多

 1使用jxl.jar  读execl.xls文件

 

	public static List drawOrderParseJxlToList(String path) {
		List list = new ArrayList();
		try {
			Workbook book = Workbook.getWorkbook(new File(path));
			Sheet sheet = book.getSheet(0);
			for (int i = 1; i < sheet.getRows(); i++) {
				DrawOrder drawOrder = new DrawOrder();
				drawOrder.setOrderId(sheet.getCell(0,i).getContents());
				drawOrder.setUserName(sheet.getCell(1,i).getContents());
				drawOrder.setMobileNo(sheet.getCell(2,i).getContents());
				list.add(drawOrder);
			}
			book.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		for (DrawOrder dor : list) {
//			System.out.println(dor.getUserName() +"     "+dor.getMobileNo()+"          "+dor.getOrderId());
//		}
		return list;
	}

  2 写execl.jar文件

 

/**
	 * 导出抽奖结果
	 * @param list
	 * @param path
	 * @param fileName
	 * @return
	 */
	public static boolean  writeDrawAwardListToExecl(List list,String path,String fileName ){

        try {   
            //open file.   
        	File file=new File(path);
        	if (!file.exists()) {
        		file.mkdirs();
			}
            WritableWorkbook book = Workbook.createWorkbook(new File(path+fileName));   
            WritableSheet sheet = book.createSheet("drawAward", 0);   
            
            //define cell column and row in Label Constructor, and cell content write "test".   
            //cell is 1st-Column,1st-Row. value is "test".   
            Label labell = new Label(0, 0, "奖品等级");   
            Label label2 = new Label(1, 0, "奖品名称"); 
            Label label3 = new Label(2, 0, "中奖订单号"); 
            sheet.addCell(labell);   
            sheet.addCell(label2);
            sheet.addCell(label3);   
            for (int i = 0; i < list.size(); i++) {
            	DrawAward dAward=list.get(i);
            	 Label label=new Label(0,i+1,dAward.getAwardGrade()+"");
            	 Label labe2=new Label(1,i+1,dAward.getAwardName()+"");
            	 Label labe3=new Label(2,i+1,dAward.getOrderId()+"");
            	 sheet.addCell(label);   
                 sheet.addCell(labe2);
                 sheet.addCell(labe3);   
			}
            book.write();   
            book.close();   
            return true;
        } catch (Exception e) {   
            e.printStackTrace();
            return false;
        }   
	}

 

  • jxl.jar (708.2 KB)
  • 下载次数: 0

你可能感兴趣的:(java解析,生成excel文件)