• poi查询数据导出生成Excel文件


    1.直接上代码(导出数据成excel文件的方法)

    
    
    /**
    * 导出excel
    * @param title 导出表的标题
    * @param rowName 导出表的列名
    * @param dataList 需要导出的数据
    * @param fileNames 生成excel文件的文件名
    * @param response
    */
    public static void exportExcel(String title, String[] rowName, List<List<Object>> dataList, String fileNames, HttpServletResponse response) throws Exception{
    String fileName= new String(fileNames.getBytes("UTF-8"),"iso-8859-1");
    OutputStream output = response.getOutputStream();
    response.reset();
    response.setHeader("Content-disposition", "attachment; filename="+fileName);
    response.setContentType("application/msexcel");
    try {
    HSSFWorkbook workbook; // 创建工作簿对象
    workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet(title); // 创建工作表
    HSSFRow rowm = sheet.createRow(0); // 产生表格标题行
    HSSFCell cellTiltle = rowm.createCell(0); //创建表格标题列
    // sheet样式定义;
    // 设置字体
    HSSFFont font = workbook.createFont();
    // 设置字体大小
    font.setFontHeightInPoints((short) 11);
    // 设置字体名字
    font.setFontName("Courier New");
    // 设置样式;
    HSSFCellStyle style = workbook.createCellStyle();
    // 在样式用应用设置的字体;
    style.setFont(font);
    // 设置自动换行;
    style.setWrapText(false);
    //横向居中
    style.setAlignment(HorizontalAlignment.CENTER);
    //纵向居中
    style.setVerticalAlignment( VerticalAlignment.CENTER);
    // 设置底边框;
    style.setBorderBottom(BorderStyle.THIN);
    // 设置左边框;
    style.setBorderLeft(BorderStyle.THIN);
    // 设置右边框;
    style.setBorderRight(BorderStyle.THIN);
    // 设置顶边框;
    style.setBorderTop(BorderStyle.THIN);
    //合并表格标题行,合并列数为列名的长度,第一个0为起始行号,第二个1为终止行号,第三个0为起始列好,第四个参数为终止列号
    sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));
    cellTiltle.setCellStyle(style); //设置标题行样式
    cellTiltle.setCellValue(title); //设置标题行值
    int columnNum = rowName.length; // 定义所需列数
    HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行)
    // 将列头设置到sheet的单元格中
    for (int n = 0; n < columnNum; n++) {
    HSSFCell cellRowName = rowRowName.createCell(n); // 创建列头对应个数的单元格
    cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置列头单元格的数据类型
    HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
    cellRowName.setCellValue(text); // 设置列头单元格的值
    cellRowName.setCellStyle(style); // 设置列头单元格样式
    }
    // 将查询出的数据设置到sheet对应的单元格中
    for (int i = 0; i < dataList.size(); i++) {
    List<Object> obj = dataList.get(i); // 遍历每个对象
    HSSFRow row = sheet.createRow(i + 3); // 创建所需的行数
    for (int j = 0; j < obj.size(); j++) {
    HSSFCell cell = null; // 设置单元格的数据类型
    if (j == 0) {
    cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);
    cell.setCellValue(i + 1);
    } else {
    cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
    if (!"".equals(obj.get(j)) && obj.get(j) != null) {
    cell.setCellValue(obj.get(j).toString()); // 设置单元格的值
    }
    }
    cell.setCellStyle(style); // 设置单元格样式
    }
    }

    // 让列宽随着导出的列长自动适应
    for (int colNum = 0; colNum < columnNum; colNum++) {
    int columnWidth = sheet.getColumnWidth(colNum) / 256;
    for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
    HSSFRow currentRow;
    // 当前行未被使用过
    if (sheet.getRow(rowNum) == null) {
    currentRow = sheet.createRow(rowNum);
    } else {
    currentRow = sheet.getRow(rowNum);
    }
    if (currentRow.getCell(colNum) != null) {
    HSSFCell currentCell = currentRow.getCell(colNum);
    if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
    int length = currentCell.getStringCellValue()
    .getBytes().length;
    if (columnWidth < length) {
    columnWidth = length;
    }
    }
    }
    }
    if (colNum == 0) {
    sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
    } else {
    sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
    }
    }
    workbook.write(output);
    } catch (Exception e) {
    e.printStackTrace();
    }
    if (output != null) {
    try {
    output.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    
    

    2.然后准备好你的所要导出的数据,文件名参数,调用该方法
    @Override
    public void listDownload(HttpServletResponse response, Map<String, Object> params) throws Exception {
    // 表头
    String[] header = {
    "编号",
    "列名称一",
    "列名称二",
    "列名称三",
    "列名称四",
    "列名称五",
    "列名称六",
    "列名称七",
    "列名称八",
    "列名称九"
    };

    //数据
    List<List<Object>> data = new ArrayList<>();
    List<Object> list1 = Lists.newArrayList();
    list1.add("1");
    list1.add("列名称一");
    list1.add("列名称二");
    list1.add("列名称三");
    list1.add("列名称四");
    list1.add("列名称五");
    list1.add("列名称六");
    list1.add("列名称七");
    list1.add("列名称八");
    list1.add("列名称九");
    data.add(list1);
    List<Object> list2 = Lists.newArrayList();
    list2.add("2");
    list2.add("列名称一");
    list2.add("列名称二");
    list2.add("列名称三");
    list2.add("列名称四");
    list2.add("列名称五");
    list2.add("列名称六");
    list2.add("列名称七");
    list2.add("列名称八");
    list2.add("列名称九");
    data.add(list2);
    ExcelTests.exportExcel("测试明细表",header,data,"测试明细表.xlsx",response);

    }

    3.结果如图

     4.引用的poi 版本

    <poi.version>3.17</poi.version>
  • 相关阅读:
    Fiddler抓包使用教程-安装配置
    Fiddler抓包使用教程-扫盲篇
    【Python3 爬虫】14_爬取淘宝上的手机图片
    【Python3 爬虫】13_爬取博客园首页所有文章
    【Python3 爬虫】12_代理IP的使用
    【Python】self的用法扫盲
    【Python3 爬虫】11_报错No module named 'requests'
    【Python3 爬虫】10_Beautiful Soup库的使用
    【DB2】国标行业分类存储,通过SQL查询出层级关系
    【Python3 爬虫】09_正则表达式(re.math()、re.search()、re.sub()、全局匹配函数)
  • 原文地址:https://www.cnblogs.com/xianshen/p/12937000.html
Copyright © 2020-2023  润新知