Workbook的使用导出到excel文本

[java]  view plain  copy
  1. public static void main(String[] args) throws Exception {  
  2.     //输入流。a.xls为要读取的excel文件名,不可为xlsx后缀  
  3.     Workbook book = Workbook.getWorkbook(new File("E:\\a.xls"));  
  4.     //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)  
  5.     Sheet sheet = book.getSheet(0);  
  6.     //输出流  
  7.     WritableWorkbook wwb = Workbook.createWorkbook(new FileOutputStream("E:\\b.xls"));  
  8.     //创建第一个工作表对象,名为("Sheet 1)  
  9.     WritableSheet ws = wwb.createSheet("Sheet 1"0);  
  10.     //单元格对象  
  11.     Label label;  
  12.   
  13.     //label里面的样式  
  14.     WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);  
  15.     WritableCellFormat wcfFC = new WritableCellFormat(wfc);  
  16.     //前三行数据,列不用加条件  
  17.     for (int i = 0; i < 13; i++) {  
  18.         label = new Label(i, 0, sheet.getCell(i, 0).getContents(), wcfFC);  
  19.         ws.addCell(label);  
  20.         label = new Label(i, 1, sheet.getCell(i, 1).getContents());  
  21.         ws.addCell(label);  
  22.         label = new Label(i, 2, sheet.getCell(i, 2).getContents());  
  23.         ws.addCell(label);  
  24.     }  
  25.     int i = 3;  
  26.     //共821行数据  
  27.     while (i < 821) {  
  28.         //共10列数据  
  29.         for (int j = 0; j < 10; j++) {  
  30.             //此处可以根据列的不同加条件  
  31.   
  32.             label = new Label(j, i, sheet.getCell(j, i).getContents());  
  33.             ws.addCell(label);  
  34.         }  
  35.         i++;  
  36.     }  
  37.     book.close();  
  38.     wwb.write();  
  39.     wwb.close();  
  40.     System.out.println("结束");  

案例2:
public void uploadExcel(InputStream is)  throws Exception{
		//从文件流中得到工作薄
		Workbook book = Workbook.getWorkbook(is);
		//得到工作薄中的第一个sheet
		Sheet sheet = book.getSheet(0);
		for(int i = 1; i < sheet.getRows(); i++){
			UserInfo userInfo = new UserInfo();
			
			//姓名
			Cell nameCell = sheet.getCell(0, i);//column,row第i行,第0个单元格
			String userName = nameCell.getContents();//取出单元格的内容
			userInfo.setUserName(userName);
			
			//性别
			Cell sexCell = sheet.getCell(1, i);
			String userSex = sexCell.getContents();
			userInfo.setUserSex(userSex);
			
			//电话号码
			Cell phoneCell = sheet.getCell(2, i);
			String userPhone = phoneCell.getContents();
			userInfo.setUserPhone(userPhone);
			
			//密码
			Cell pwCell = sheet.getCell(3, i);
			String userPw = pwCell.getContents();
			userInfo.setUserPw(userPw);
			
			//类型
			Cell typeCell = sheet.getCell(4, i);
			String userType = typeCell.getContents();
			userInfo.setUserType(userType);
			
			userInfoDao.add(userInfo);
		}
		book.close();
	}


	public File downloadFile(UserInfo userInfo) {
		if(userInfo != null){
			if(userInfo.getUserName() != null && !userInfo.getUserName().equals("")){
				userInfo.setUserName("%" + userInfo.getUserName() + "%");
			}
		}
		List list = userInfoDao.getUserList(userInfo);
		try {
			//生成一个临时文件
			File file = File.createTempFile("tmpusers", ".xls");
			//向临时文件中保存用户信息(以excel的格式-行、列)
			WritableWorkbook book = Workbook.createWorkbook(file);//excel
			WritableSheet sheet = book.createSheet("sheet1", 0);
			//表头
			String[] titles = {"姓名","性别","电话号码","密码","用户类型"};
			for(int i = 0; i < titles.length ; i++){
				Label label = new Label(i, 0, titles[i]);
				sheet.addCell(label);
			}
			//数据
			for(int i = 0 ; i < list.size();i++){
				sheet.addCell(new Label(0, i+1, list.get(i).getUserName()));
				sheet.addCell(new Label(1, i+1, list.get(i).getUserSex()));
				sheet.addCell(new Label(2, i+1, list.get(i).getUserPhone()));
				sheet.addCell(new Label(3, i+1, list.get(i).getUserPw()));
				sheet.addCell(new Label(4, i+1, list.get(i).getUserType()));
			}
			
			//把信息写入到临时文件中
			book.write();
			book.close();
			return file;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


你可能感兴趣的:(java)