ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POI导出

2020-03-06 14:05:22  阅读:243  来源: 互联网

标签:sheet addr 导出 HSSFCell createCell POI import staff


package com.wkrj.info.controller;



import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.wkrj.info.controller.ExprotCellStyle.*;

/**
 * 导出用户数据
 * @author LJH
 *
 */
public class ExportUserUtils {


    /**
     * 把数据导出到path
     */
    @SuppressWarnings("deprecation")
    public static void exportD(List<Map<String ,Object>> datalist, HttpServletRequest request, HttpServletResponse response) {
        //创建工作簿
        HSSFWorkbook workbook=new HSSFWorkbook();
        //从工作簿里面创建sheet
//		workbook.createSheet();
        HSSFSheet sheet = workbook.createSheet("用户列表");
        //设置sheet
        //sheet.setColumnWidth(2, 100);//设置某一个下标的列宽
        //sheet.setDefaultColumnStyle(column, style);//设置某一列的默认样式
        sheet.setDefaultColumnWidth(20);//设置所有列的列宽
//		sheet.setColumnHidden(columnIndex, hidden);//设置某一列是否隐藏
        //sheet.setDefaultRowHeight((short)(30*20));//设置行高60
        //sheet.setDefaultRowHeightInPoints(30);//设置行高30
        //的sheet上创建行
        int rownum=0;
        /*HSSFRow row01 = sheet.createRow(rownum);
        //在row01上创建单元格
        HSSFCell cell_row01 = row01.createCell(0);
        //向cell_row01写东西
        cell_row01.setCellValue("用户数据");
        //设置标题样式
        HSSFCellStyle titleStyle=createTitleCellStyle(workbook);
        cell_row01.setCellStyle(titleStyle);

        //第二行
        rownum++;*/
        HSSFRow row03 = sheet.createRow(rownum);
        //员工姓名	公司名称	性别	手机号	办公楼宇	办公室	体温	扫码地点	扫码时间
        String[] titles={"员工姓名","公司名称","性别","手机号","办公楼宇","办公室","体温","扫码地点","扫码时间"};
        //得到表头的样式
        HSSFCellStyle tableTitleStyle=createTableTitleStyle(workbook);

        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row03.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(tableTitleStyle);
        }

        HSSFCellStyle tableBodyStyle=setRowCellCenter(workbook);
        for (int i = 0; i < datalist.size(); i++) {
            //${item.staff_name}	${item.enterprise_name}	${item.staff_sex}	${item.staff_tel}	${item.staff_addr}	${item.addr_roomno}	${item.info_tem}	${item.addr_name}	${item.time}
            rownum++;
            HSSFRow row = sheet.createRow(rownum);
            Map<String,Object> entity =datalist.get(i);
            //创建idcell
            HSSFCell staff_nameCell = row.createCell(0);
            staff_nameCell.setCellValue(entity.get("staff_name").toString());

            staff_nameCell.setCellStyle(tableBodyStyle);
            //创建namecell
            HSSFCell enterprise_nameCell = row.createCell(1);
            enterprise_nameCell.setCellValue(entity.get("enterprise_name").toString());
            enterprise_nameCell.setCellStyle(tableBodyStyle);

            //创建addresscell
            HSSFCell staff_sexCell = row.createCell(2);
            staff_sexCell.setCellValue(entity.get("staff_sex").toString());
            staff_sexCell.setCellStyle(tableBodyStyle);


            HSSFCell staff_telCell = row.createCell(3);
            staff_telCell.setCellValue(entity.get("staff_tel").toString());
            staff_telCell.setCellStyle(tableBodyStyle);


            HSSFCell staff_addrCell = row.createCell(4);
            staff_addrCell.setCellValue(entity.get("staff_addr").toString());
            staff_addrCell.setCellStyle(tableBodyStyle);

            HSSFCell addr_roomnoCell = row.createCell(5);
            addr_roomnoCell.setCellValue(entity.get("addr_roomno").toString());
            addr_roomnoCell.setCellStyle(tableBodyStyle);

            HSSFCell info_temCell = row.createCell(6);
            info_temCell.setCellValue(entity.get("info_tem").toString());
            info_temCell.setCellStyle(tableBodyStyle);
            HSSFCell addr_nameCell = row.createCell(7);
            addr_nameCell.setCellValue(entity.get("addr_name").toString());
            addr_nameCell.setCellStyle(tableBodyStyle);
            HSSFCell timeCell = row.createCell(8);
            timeCell.setCellValue(entity.get("time").toString());
            timeCell.setCellStyle(tableBodyStyle);
        }

        //导出数据
            //workbook.write(new File(path));
            String fileName = System.currentTimeMillis() + ".xls";
            /*String path = getClass().getClassLoader().getResource("/").getPath()*/
            String path =request.getSession().getServletContext().getRealPath("/")
                    + "upload/" + fileName;
            //System.out.println(path);
            File file = new File(path);
            try {
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                OutputStream fOut = new FileOutputStream(file);
                // 设置response参数,可以打开下载页面
                response.reset();
                //设置响应文本格式
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + new String(("扫码记录" + ".xls").getBytes(), "iso-8859-1"));
                //将文件输出到页面
                ServletOutputStream out = response.getOutputStream();
                workbook.write(out);
                fOut.flush();
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("导出完成");
    }
}


标签:sheet,addr,导出,HSSFCell,createCell,POI,import,staff
来源: https://blog.csdn.net/ivy_doudou/article/details/104694557

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有