简单的表格导出

public class ExportExcel {
private static Logger log =LogManager.getLogger(ExportExcel.class);
public static void Export(){
//首先声明一个工作簿
HSSFWorkbook wb = new HSSFWorkbook ();
//声明一个单表(表明)
HSSFSheet sheet = wb.createSheet("用户信息表");
//给单表设定长度
sheet.setDefaultColumnWidth(10);
// 生成一个样式  
HSSFCellStyle style = wb.createCellStyle();
//创建第一行(也可以称为表头)
HSSFRow row = sheet.createRow(4);
//样式字体居中
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) (10));//字体大小
font.setFontName("黑体");//字体
font.setColor(HSSFFont.COLOR_NORMAL);//字体颜色
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//水平居中
style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);//垂直居中
//给表头第一行一次创建单元格
HSSFCell cell =null;
cell = row.createCell(0);
//设置表格title
cell.setCellValue(new HSSFRichTextString("用户详情"));
cell.setCellStyle(style);
CellRangeAddress  addr = new CellRangeAddress(0,0,0,4);
sheet.addMergedRegion(addr);

cell = row.createCell(1);
cell.setCellStyle(style);
cell.setCellValue("用户编号");

cell = row.createCell(2);
cell.setCellValue("用户姓名");
cell.setCellStyle(style);

cell = row.createCell(3);
cell.setCellValue("性别");
cell.setCellStyle(style); 

cell = row.createCell(4);
cell.setCellValue("年龄");
cell.setCellStyle(style); 

List<User> list = new ArrayList<User>();
User user = new User();
user.setId("0001");
user.setName("张三");
user.setSex("男");
user.setAge("22");
User user2 = new User();
user2.setId("0002");
user2.setName("小芳");
user2.setSex("女");
user2.setAge("24");
list.add(user);
list.add(user2);
//向表格中循环插入数据
for(int i=0;i<list.size();i++){
row = sheet.createRow(i+1);
row.createCell(0).setCellValue(list.get(i).getId());
row.createCell(1).setCellValue(list.get(i).getName());
row.createCell(2).setCellValue(list.get(i).getSex());
row.createCell(3).setCellValue(list.get(i).getAge());
}
try {
FileOutputStream fo = new FileOutputStream("f:\\用户.xls");
try {
wb.write(fo);
fo.close();
JOptionPane.showMessageDialog(null, "导出成功");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "导出失败");
e.printStackTrace();
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "导出失败");
e.printStackTrace();
}
}
public static void main(String[] args) {
log.info("开始执行");
Export();
}

}


文中数据直接写死,只需要传过来一个list然后直接遍历放入表格即可,如有错误请斧正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值