温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Java Web中Excel文件如何使用POI实现导出

发布时间:2020-11-21 15:59:29 来源:亿速云 阅读:203 作者:Leah 栏目:编程语言

在Java Web中Excel文件如何使用POI实现导出?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

采用Spring mvc架构:

Controller层代码如下

@Controller
public class StudentExportController{
  @Autowired
  private StudentExportService studentExportService;
  @RequestMapping(value = "/excel/export")
  public void exportExcel(HttpServletRequest request, HttpServletResponse response)
  throws Exception {
    List<Student> list = new ArrayList<Student>();
    list.add(new Student(1000,"zhangsan","20"));
    list.add(new Student(1001,"lisi","23"));
    list.add(new Student(1002,"wangwu","25"));
    HSSFWorkbook wb = studentExportService.export(list);
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment;filename=student.xls");
    OutputStream ouputStream = response.getOutputStream();
    wb.write(ouputStream);
    ouputStream.flush();
    ouputStream.close();
  }
}

Service层代码如下:

@Service
public class StudentExportService {
  String[] excelHeader = { "Sno", "Name", "Age"};
  public HSSFWorkbook export(List<Campaign> list) {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Campaign");
    HSSFRow row = sheet.createRow((int) 0);
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    for (int i = 0; i < excelHeader.length; i++) {
      HSSFCell cell = row.createCell(i);
      cell.setCellValue(excelHeader[i]);
      cell.setCellStyle(style);
      sheet.autoSizeColumn(i);
    }
    for (int i = 0; i < list.size(); i++) {
      row = sheet.createRow(i + 1);
      Student student = list.get(i);
      row.createCell(0).setCellValue(student.getSno());
      row.createCell(1).setCellValue(student.getName());
      row.createCell(2).setCellValue(student.getAge());
    }
    return wb;
  }
}

前台的js代码如下:

<script>
function exportExcel(){
   location.href="excel/export" rel="external nofollow" ;
   <!--这里不能用ajax请求,ajax请求无法弹出下载保存对话框-->
 }
</script>

设置Excel样式以及注意点:

String[] excelHeader = { "所属区域(地市)", "机房", "机架资源情况", "", "", "", "",
  "", "端口资源情况", "", "", "", "", "", "机位资源情况", "", "", "设备资源情况",
  "", "", "IP资源情况", "", "", "", "", "网络设备数" };
String[] excelHeader1 = { "", "", "总量(个)", "空闲(个)", "预占(个)", "实占(个)",
  "自用(个)", "其它(个)", "总量(个) ", "在用(个)", "空闲(个)", "总带宽(M)",
  "在用带宽(M)", "空闲带宽(M)", "总量(个)", "在用(个)", "空闲(个)", "设备总量(个)",
  "客户设备(个)", "电信设备(个)", "总量(个)", "空闲(个)", "预占用(个)", "实占用(个)",
  "自用(个)", "" };
// 单元格列宽
int[] excelHeaderWidth = { 150, 120, 100, 100, 100, 100, 100, 100, 100,
  100, 100, 120, 120, 120, 120, 120, 120, 150, 150, 150, 120,
  120, 150, 150, 120, 150 };
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("机房报表统计");
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
// 设置居中样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 设置合计样式
HSSFCellStyle style1 = wb.createCellStyle();
Font font = wb.createFont();
font.setColor(HSSFColor.RED.index);
font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体
style1.setFont(font);
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 合并单元格
// first row (0-based) last row (0-based) first column (0-based) last
// column (0-based)
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 7));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 8, 13));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 14, 16));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 17, 19));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 20, 24));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 25, 25));
// 设置列宽度(像素)
for (int i = 0; i < excelHeaderWidth.length; i++) {
  sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
}
// 添加表格头
for (int i = 0; i < excelHeader.length; i++) {
  HSSFCell cell = row.createCell(i);
  cell.setCellValue(excelHeader[i]);
  cell.setCellStyle(style);
}
row = sheet.createRow((int) 1);
for (int i = 0; i < excelHeader1.length; i++) {
  HSSFCell cell = row.createCell(i);
  cell.setCellValue(excelHeader1[i]);
  cell.setCellStyle(style);
}

注意点1:合并单元格    new CellRangeAddress(int,int,int,int)
first row (0-based) ,last row (0-based), first column (0-based),last column (0-based)

注意点2:合并单元格
String[] excelHeader = { "所属区域(地市)", "机房", "机架资源情况", "", "", "", "","", "端口资源情况", "", "", "", "", "", "机位资源情况", "", "", "设备资源情况","", "", "IP资源情况", "", "", "", "", "网络设备数" };

合并以后的单元格虽然是一个,但是仍然要保留其单元格内容,此处用空字符串代替,否则后续表头显示不出

注意点3:填充单元格

正确写法:

HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader1[i]);
cell.setCellStyle(style);

错误写法:

row.createCell(i).setCellValue(excelHeader1[i]);
row.createCell(i).setCellStyle(style);

关于在Java Web中Excel文件如何使用POI实现导出问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI