189 8069 5689

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

在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 list = new ArrayList();
    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 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代码如下:

设置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实现导出问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


名称栏目:在JavaWeb中Excel文件如何使用POI实现导出
标题网址:http://cdxtjz.cn/article/gdjded.html

其他资讯