java poi导出Excel Demo

public class Date2Excel {
	
	public static void main(String[] args) throws Exception {
		
		HSSFWorkbook wb = new HSSFWorkbook();// 第一步,创建workbook
		HSSFSheet sheet = wb.createSheet("页名称");// 第二步,创建sheet
		HSSFRow row = sheet.createRow(0);// 第三步,创建row
		HSSFCellStyle style = wb.createCellStyle();// 第四步,创建单元格类型
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("学号");
		cell.setCellStyle(style);
		
		List<User> list = GetList.getTestList();// 获取数据
		for (int i = 0; i < list.size(); i++) {
			
			row = sheet.createRow(i+1);
			User u = (User) list.get(i);
			row.createCell(0).setCellValue(u.getUserId());// 第五步,创建单元格
		}
		try {// 第六步,将文件存到指定位置
			
			FileOutputStream fout = new FileOutputStream("E:/students.xls");
			wb.write(fout);
			fout.close();
		}catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(java poi导出Excel Demo)