java网站建设导出excel表格方法

    当前位置:深圳APP开发 > APP开发知识
    2018/8/4 16:09:46

     深圳网站建设公司,一般会使用asp.net,java,php做网站底层开发,由于网站使用过程中,会产生大量数据,这些数据包含留言信息,或者新闻信息,由于我们今天是做java网站建设,来开发网站,所以我们就来讲一下java导出excel表格的方法。

    1、准备工作,下载且导入poi.jar
    由于我们今天讲的导出excel表格的方法是使用第三方jar来导出的,所以需要先导入第三方poi.jar包。

    2、制作导出excel的方法
    public static HSSFWorkbook getHSSFWorkbook(String sheetName,
    String[] title, ArrayList> list,
    HSSFWorkbook wb) {

    // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
    if (wb == null) {
    wb = new HSSFWorkbook();
    }

    // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
    HSSFSheet sheet = wb.createSheet(sheetName);

    // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
    HSSFRow row = sheet.createRow(0);

    // 第四步,创建单元格,并设置值表头 设置表头居中
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

    // 声明列对象
    HSSFCell cell = null;

    // 创建标题
    for (int i = 0; i < title.length; i++) {
    cell = row.createCell(i);
    cell.setCellValue(title[i]);
    cell.setCellStyle(style);
    }

    int i = 0;

            //判断是否有内容
    if (list != null) { 
    // 创建内容
    for (ArrayList mlist : list) {
                    //得到行号
    row = sheet.createRow(i + 1);
                    //循环所有信息
    for (TableInfo model : mlist) {

    if (model.getName().equals("subject")) {
                            //向行号插入信息
    row.createCell(0).setCellValue(model.getValue());
    } else if (model.getName().equals("name")) {

    row.createCell(1).setCellValue(model.getValue());
    } else if (model.getName().equals("sex")) {

    row.createCell(2).setCellValue(model.getValue());
    } else if (model.getName().equals("birdate")) {

    row.createCell(3).setCellValue(model.getValue());
    } else if (model.getName().equals("company")) {

    row.createCell(4).setCellValue(model.getValue());
    } else if (model.getName().equals("position")) {

    row.createCell(5).setCellValue(model.getValue());
    } else if (model.getName().equals("tel")) {

    row.createCell(6).setCellValue(model.getValue());
    }else if (model.getName().equals("email")) {

    row.createCell(7).setCellValue(model.getValue());
    } else if (model.getName().equals("address")) {

    row.createCell(8).setCellValue(model.getValue());
    }

    }
    i++;
    }
    }

    // 让列宽随着导出的列长自动适应
    for (int colNum = 0; colNum < 9; 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);
    }
    }
    return wb;
    }

    3、第三步下载信息
    public void setResponseHeader(HttpServletResponse response, String fileName) {
    try {
    try {
    fileName = new String(fileName.getBytes(), "ISO8859-1");
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    response.setContentType("application/octet-stream;charset=ISO8859-1");
    response.setHeader("Content-Disposition", "attachment;filename="
    + fileName);
    response.addHeader("Pargam", "no-cache");
    response.addHeader("Cache-Control", "no-cache");
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    通过上面两个方法我们就可以制作java网站建设导出excel表格的功能。


联系电话

QQ咨询:App01

QQ咨询:App02

QQ咨询:App03

QQ咨询:App05

微信账号

深圳软件开发服务项目

  • 移动办公、移动政务
  • 沟通互动
  • 行业信息门户
  • 销售工具&内部沟通
  • 移动电商
  • 企业展示&品牌营销
  • 深圳app开发
  • 罗湖区app
  • 龙华APP开发公司
  • 宝安软件公司
Copyright©2013-2015 主营业务:深圳APP开发 版权所有 工粤ICP备65165265号 致力于专业的app开发定制服务,包括android APP开发、IOS APP开发、企业APP定制服务公司