java技术--SSM+Maven使用POI实现Excel导出

1.环境:Spring+SpringMVC+Mybatis+Maven
2.导入POI对应的包

<!--excel解析poi包 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>

3.POI导出Excel日期格式的出来,如果不处理日期将会是一串数字
例如:正确显示2019-01-01,不处理显示:2365489652

1)第一种:在日期字段添加注解(推荐)
     <1>使用maven引入@JsonFormat,@DateTimeFormat所需要的jar包
     <!--JsonFormat-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
      <!-- DateTimeFormat:joda-time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.3</version>
        </dependency>
       <2>@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
           1,pattern:是需要转换的时间日期的格式
           2.timezone:是时间设置为东八区,避免时间在转换中有误差
           3.@JsonFormat注解可以在属性的上方,同样可以在属性对应的get方法上,两种方式没有区别
       <3>@DateTimeFormat(pattern = "yyyy-MM-dd")
           1.@DateTimeFormat的使用和@JsonFormat差不多
       <4>注解@JsonFormat主要是后台到前台的时间格式的转换
       <5>注解@DataFormAT主要是前后到后台的时间格式的转换
       <6>一般需要取数据到前台,也需要前台数据传到后台,需要同时使用    
(2)第二种:在Excel设置日期字段时进行格式化处理
      // 日期格式化
	  HSSFCell cell = rowDate.createCell(0);
	  DateFormat from_date = new SimpleDateFormat("yyyy-MM-dd");
		if (null != user.getSensor_desc()) {
		   cell.setCellValue(from_date.format(user.getFrom_date()));
			}

3.实体类User

package com.itheima.entity
public class  User  implements Serializable {
	private static final long serialVersionUID = 1L;
	private int id;//主键
	private int age;//客户年龄
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
	private String userdate;//日期
	private String username;//客户名称
	private String usertype;//类型
	private String userage;	//状态
	省略Get/Set方法......
}
注意:当使用注解格式化日期时,必须是String类型的

4.持久层接口:DAO层

package com.itheima.mapper
@Repository
public interface UserDao {
	List<User> queryExcelInfo();
	List<User> findByAge(int age);
    User findById(long id);
    int insertUser(User user);
    int updateUser(User user);
    int deleteUser(long id);
}

5.DAO层的配置文件mapper.xml:实现持久层接口

<mapper namespace="com.itheima.mapper.UserDao ">
<select id="findById" parameterType="long" resultType="com.itheima.entity.User">
		select * from table_user WHERE id = #{id}
</select>
</mapper>1)namespace:填写映射当前的Mapper接口类路径
(2)id:填写在XxxMapper接口中的方法名, id 和接口方法名要一致
(3)parameterType:填写参数的类型
(4)resultType:填写方法中返回值的类型,表示输出结果的类型
(5)#{ } 表示传入的动态参数的占位符

6.输入参数 parameterType

(1)这个属性是可选的,MyBatis 可以通过TypeHandler推断出具体传入语句的参数,这个属性的默认值是unset
(2)往往在开发的时候还是主动进行了限定,因为很多时候传入的将会是一个封装了参数的类
(3)其值是将会传入这条语句的参数类的 “完全限定名” 或 “别名”(参见mybatis-config.xml全局配置文件说明中的typeAliases)

7.输出结果 resultType / resultMap

(1)要么使用 resultType,要么使用 resultMap,两者不能同时出现在相同的语句标签中
(2)resultType - 返回的期望类型的类的完全限定名或别名(参见mybatis-config.xml全局配置文件说明中的typeAliases)
(3)如果不管传入的参数还是返回类型,只要是对类操作,就和parameterType的使用性质是一样的,可以使用别名
(4)resultMap - 外部 resultMap 的命名引用
   1.在select标签之外,有一个同级的标签,也叫resultMap(刚提到的resultMap是作为select标签的属性出现)
   2.该标签主要针对一些复杂的结果映射,用来 “描述语句和对象之间的映射关系”

8.resultType / resultMap区别

(1)使用ResultType作为输出映射,只有查询出来的列名和对象属性名一致才可映射成功
    <1>如果对象属性名和数据库字段映射一致,使用ResultType
    <2>ResultType使用实例:
        1.语法:ResultType="对象的完整包路径"
        2.示例:ResultType="com.entity.User"
(2)根据实际情况,如果列名和对象属性名映射不一致,需要定义一个resultMap来描述两者之间的关系
<mapper namespace="com.itheima.mapper.UserDao">
    <!--定义resultMap-->
    <resultMap id="userResultMap" type="com.itheima.entity.User">
        <!--id表查询结果中的唯一标识-->
        <id property="id" column="id" />
        <!--result表示对普通列名的映射,property表示对象属性名,column表查询出的列名-->
        <result property="age" column="age" />
        <result property="cupSize" column="cup_size" />
    </resultMap>
    <!--引用外部resultMap,即girlResultMap-->
    <select id="findById" parameterType="long" resultMap="userResultMap">
        SELECT * FROM table_user WHERE id = #{id}
    </select>
</mapper>

9.insert, update 和 delete

<!--使用了useGeneratedKeys和keyProperty来将生成的主键设置到对象属性中-->
<insert id="insertUser" parameterType="com.itheima.mapper.UserDao" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO table_user(age)
    VALUES (#{age})
</insert>
<update id="updateUser" parameterType="com.itheima.mapper.UserDao">
    UPDATE table_user
    SET age = #{age}
    WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="long">
    DELETE FROM table_user
    WHERE id = #{id}
</delete>
(1)insert / update / delete 都没有 resultType 或 resultMap,他们的返回值是受影响的行数
(2)若数据库支持自动生成主键,可设置 useGeneratedKeys 为 true,并使用 keyProperty 将生成的主键值设置到目标属性上

10.sql标签

(1)sql元素用来定义可重用的SQL代码片段,其他语句可以用过 <include> 标签来将之包含其中
<mapper namespace="com.itheima.mapper.UserDao">
    <resultMap id="userResultMap" type="com.itheima.entity.User">
        <id property="id" column="id" />
        <result property="age" column="age" />
        <result property="cupSize" column="cup_size" />
    </resultMap>
    <!--定义可重用的sql片段-->
    <sql id="userColumn">
      id, age
    </sql>
    <select id="findById" parameterType="long" resultMap="userResultMap">
        <!--通过include引用外部sql片段-->
        SELECT <include refid="userColumn" />
        FROM girl WHERE id = #{id}
    </select>
</mapper>

11.业务层接口:Service层

package com.itheima.service
public interface UserService {
	List<User> queryExcelInfo();
	List<User> findByAge(int age);
    User findById(long id);
    int insertUser(User user);
    int updateUser(User user);
    int deleteUser(long id);
}

11.业务层接口实现类:ServiceImpl

package com.itheima.service.impl
@Service
public class UserService Impl implements UserService {
 @Resource
 private UserDao userDao;
public List<User> queryExcelInfo(){
		return UserDao.queryExcelInfo();
	}
}

12.控制层:Contorller层

package com.itheima.controller
@Controller
@RequestMapping("/user")
public class UserContorller {
private static final Logger logger = LoggerFactory.getLogger(UserContorller .class);
    @Resource
	private UserService userService;
	 @RequestMapping("/excel")
	public void excel(HttpServletResponse response )throws IOException {
		 response.setCharacterEncoding("UTF-8");  
		 // 从数据库读取数据
		 List<User> list = uaerService.queryExcelInfo();
	     System.out.println(list);
	      //创建excel文件
	      HSSFWorkbook wb = new HSSFWorkbook();
	      //创建sheet页
	      HSSFSheet sheet = wb.createSheet("客户信息表");
	      //创建标题行
	      HSSFRow titleRow = sheet.createRow(0);
	      //创建单元格
		 HSSFCell cell = null;
        // excel标题表头
		String[] titles = { "日期", "客户年龄", "客户名称", "类型", "状态" };
        // 设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
		//方式一:使用for循环在创建的标题行中,设置标题
		for(int i = 0;i<titles.length;i++){
		//创建标题行的单元格
		cell = titleRow.createCell(i);
		//单元格赋值
		cell.setCellValue(titles[i]);
		//设置单元格居中
		cell.setCellStyle(style);
         }
         //方式二:直接创建单元格并赋值
	     titleRow.createCell(0).setCellValue("日期");
	     titleRow.createCell(2).setCellValue("客户年龄");
	     titleRow.createCell(3).setCellValue("客户名称");
	     titleRow.createCell(4).setCellValue("类型");
	     titleRow.createCell(5).setCellValue("状态");
	      //遍历将数据放到excel列中
            for (User user : list) {
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
                dataRow.createCell(0).setCellValue(user.getCustomdate());
                dataRow.createCell(1).setCellValue(user.getAge());
                dataRow.createCell(2).setCellValue(user.getCustomname());
                dataRow.createCell(3).setCellValue(user.getCustomtype());
                dataRow.createCell(4).setCellValue(user.getCustomstatus());
            }
               // 设置下载时客户端Excel的名称 
                response.setContentType("application/octet-stream;charset=utf-8");
                response.setHeader("Content-Disposition", "attachment;filename="
                           + new String("客户名单".getBytes(),"iso-8859-1") + ".xls");
               OutputStream ouputStream = response.getOutputStream();  
               wb.write(ouputStream);  
               ouputStream.flush();  
               ouputStream.close();
}
	}

13.前端

<div>
            <button onclick="d()">导出</button>
</div> 
<script type="text/javascript">
          function d(){window.location.href="<%=basePath%>sensor/exportExcel";}     
</script>
<!--调用即可-->
<a href="${pageContext.request.contextPath}/sensor/exportExcel">导出Excel表格</a>

注:参考网址https://www.cnblogs.com/deng-cc/p/9329192.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值