`
Hermosa_Young
  • 浏览: 249653 次
  • 来自: 上海
社区版块
存档分类
最新评论

java poi导出Excel Demo

    博客分类:
  • Java
阅读更多
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();
		}
	}
}

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics