SSM使用poi导出excel



1.  pom.xml         SSM搭建的dependency不贴了


org.apache.poi
poi-ooxml
3.8


2.JSP 片段

导出用户信息

3.controller      Usercontroller

/**
* 写一个导出excel的功能
*/
@RequestMapping("/export")
@ResponseBody
public void export(HttpServletResponse response) {
response.setContentType("application/binary;charset=UTF-8");
String[] titles = { "用户编号", "用户姓名", "用户密码", "用户邮箱","用户手机" };
userService.export(titles);

}

4.service      UserService

     void export(String[] titles);//导出excel

5.serviceImpl          UserServiceImpl

// 页面展示excel或者是导出excel
public void export(String[] titles) {
try {
// 创建excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 建立新的sheet对象
HSSFSheet hssfSheet = workbook.createSheet("sheet1");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow hssfRow = hssfSheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
// 居中样式
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell hssfCell = null;
for (int i = 0; i < titles.length; i++) {
hssfCell = hssfRow.createCell(i);// 列索引从0开始
hssfCell.setCellValue(titles[i]);// 列名1
hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
}
// 第五步,写入实体数据
List users = userDao.queryUser();
if (users != null && !users.isEmpty()) {
for (int i = 0; i < users.size(); i++) {
hssfRow = hssfSheet.createRow(i + 1);
User user = users.get(i);
// 第六步,创建单元格,并设置值
int userid = 0;
if (user.getId() != 0) {
userid = user.getId();
}
hssfRow.createCell(0).setCellValue(userid);
String userName = "";
if (user.getUserName() != null) {
System.out.println(user.getUserName());
userName = user.getUserName();
}
hssfRow.createCell(1).setCellValue(userName);
String password = "";
if (user.getPassword() != null) {
password = user.getPassword();
}
hssfRow.createCell(2).setCellValue(password);


String email = "";
if (user.getEmail() != null) {
email = user.getEmail();
}
hssfRow.createCell(3).setCellValue(email);


int phoneNumber = 0;
if (user.getPhoneNumber() != 0) {
phoneNumber = user.getPhoneNumber();
}
hssfRow.createCell(4).setCellValue(phoneNumber);
}
}


// 第七步,将文件输出到客户端浏览器 || 使用下载
try {
FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.xls");// 指定路径与名字和格式
workbook.write(fileOutputStream);// 将数据写出去
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();


}
}

6.Dao接口               UserDao

   List    queryUser();//查询用户

7.xxxMapper.xml   自己写个名字就行了 demo:UserMapper.xml


   
   
   
   
   
 


 


看个人心情,查询的字段可以抽出来写到中,然后使用的时候过来就行了

resultMap和resultType的区别请自行百度。

8这个建表语句   贴出来吧 也不费事

  CREATE TABLE user_t(
  id int(11) NOT NULL AUTO_INCREMENT,
  user_name varchar(40) NOT NULL,
  password varchar(255) NOT NULL,
  email varchar(40) NOT NULL,
  phoneNumber int(50) NOT NULL,
  PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8


insert 略

9.用户的实体类

public class User {
    private Integer id;


    private String userName;


    private String password;


    private String email;
    

    private Integer phoneNumber;

   get set方法略 


不足:不足的地方就是生成的excel单元格之间因为数据量有的长的比如密码我用的MD5加盐存储,所以长,就是生成的excel的单元格格式有待调整。

good luck!

备忘









你可能感兴趣的:(Java,linux,大数据,分布式)