1、技术概要,说明这个技术要做什么? 学习这个技术的原因、技术的难点在哪里?
1.1技术概述
使用Apache POI实现JAVA后端并导出Excel文件。
1.2原因
学习这个技术的主要原因是,我负责的部分是完成Java后端并将Excel文件导出到前端,然后学习以完成这个功能。
1.3难点
我认为该技术的难点在于Apache POI的引用jar包的导入和Excel导出文件格式的设计。
2、技术详细说明
2.1下载和导入Apache poi
或使用Maven仓库管理将坐标添加到pom文件
org.apache.poi
波伊
4.0.1
org.apache.poi
poi-ooxml
4.0.1
2.2依赖软件包
commons-io-2.4.jar
poi-4.0.1.jar
2.3Excel生成工具类代码
package com.example.team.util;
import com.example.team.util.data VO;
importorg.Apache.poi.hssf.usermodel.hssf workbook;
importorg.Apache.poi.ss.usermodel.*;
importorg.Apache.poi.xssf.streaming.sxsfworkbook;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
公共类excel writer {
private static List CELL_HEADS; //列头
静态{
//类加载时加载指定的列标题信息,可以根据需要制作动态生成的列标题
//这里是我个人使用的类的名称,可以修改
CELL_HEADS=new ArrayList (;
CELL_HEADS.add (用户名);
cell _ heads.add (待办事项);
CLL _ heads.add (待办事项);
CLL _ heads.add (待办事项完成情况);
cell _ heads.add (待办事项完成情况);
}
//*
生成Excel并写入数据信息
*
* @param dataList数据列表
* @return数据写入后的工作簿对象
*/
publicstatichssfworkbookexportdata (list datalist ) {
为xlsx生成Excel
hssfworkbookworkbook=newhssfworkbook (;
请注意,要为//xls生成Excel,必须使用以下工作簿对象,以便在后续输出时将文件扩展名也更改为xls
//workbook workbook=newhssfworkbook (;
生成Sheet表,写入第1行的列标题
sheet sheet=build data sheet (workbook );
//构建各行的数据内容
int rowNum=1;
迭代器it=datalist .迭代器(for ); it.hasNext (; () )。
DataVo data=it.next (;
if (data==空值) {
连续;
}
//输出行数据
rowrow=sheet.createrow(rownum;
convertdatatorow(data,row;
}
返回工作簿;
}
//*
生成sheet表并将其写入第一行数据(列标题)
*
* @ param工作簿中的对象
* @return已写入列开头的Sheet
*/
privatestaticsheetbuilddatasheet (工作簿工作簿) {
sheet sheet=workbook.create sheet (;
//设置列头宽度
for(intI=0; i CELL_HEADS.size (; I ) {
sheet.setColumnWidth (
i, 4000);}
// 设置默认行高
sheet.setDefaultRowHeight((short) 400);
// 构建头单元格样式
CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook());
// 写入第一行各列的数据
Row head = sheet.createRow(0);
for (int i = 0; i < CELL_HEADS.size(); i++) {
Cell cell = head.createCell(i);
cell.setCellValue(CELL_HEADS.get(i));
cell.setCellStyle(cellStyle);
}
return sheet;
}
/**
* 设置第一行列头的样式
*
* @param workbook 工作簿对象
* @return 单元格样式对象
*/
private static CellStyle buildHeadCellStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
//对齐方式设置
style.setAlignment(HorizontalAlignment.CENTER);
//边框颜色和宽度设置
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下边框
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左边框
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右边框
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边框
//设置背景颜色
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//粗体字设置
Font font = workbook.createFont();
font.setBold(true);
style.setFont(font);
return style;
}
/**
* 将数据转换成行
*
* @param data 源数据
* @param row 行对象
* @return
*/
private static void convertDataToRow(DataVo data, Row row) {
int cellNum = 0;
Cell cell;
// 用户名
cell = row.createCell(cellNum++);
if (null != data.getUserName()) {
cell.setCellValue(data.getUserName());
} else {
cell.setCellValue("");
}
// 待办集名
cell = row.createCell(cellNum++);
if (null != data.getSName()) {
cell.setCellValue(data.getSName());
} else {
cell.setCellValue("");
}
// 待办名
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getName() ? "" : data.getName());
// 待办情况记录(0未完成,1完成)
cell = row.createCell(cellNum++);
if (null != data.getRecord()) {
cell.setCellValue(data.getRecord());
} else {
cell.setCellValue("");
}
// 待办集情况记录(0未完成,1完成)
cell = row.createCell(cellNum++);
if (null != data.getSetRecord()) {
cell.setCellValue(data.getSetRecord());
} else {
cell.setCellValue("");
}
}
}
dataVo类
package com.example.team.util;
/**
* 读取Excel时,封装读取的每一行的数据
*/
public class DataVo {
/**
* 用户名
*/
private String userName;
/**
* 待办名
*/
private String name;
/**
* 待办集名
*/
private String sName;
/**
* 待办情况记录
*/
private String record;
/**
* 待办情况记录
*/
private String setRecord;
/**
* 待办完成度
*/
private String completion;
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSName() {
return sName;
}
public void setSName(String sName) {
this.sName = sName;
}
public String getRecord() {
return record;
}
public void setRecord(String record) {
this.record = record;
}
public String getSetRecord() {
return setRecord;
}
public void setSetRecord(String setRecord) {
this.setRecord = setRecord;
}
public String getCompletion(){
return completion;
}
public void setCompletion(String completion){
this.completion = completion;
}
}
3、技术使用中遇到的问题和解决过程。
1.在技术使用过程中,因为我下载的是较新版本的Apache POI的jar包,导致我从网上找到的工具类和版本不匹配,这导致我需要按这我的版本去修改这个工具类,这样才可以使用。
2.在第一次写好后,使用时发现,excel导出只能导出到本地的文件夹里,不可以进行传输,后面有上网去查找资料,将其改成了HSSFWorkbook的,传递的是数据流,这样就可以将后端的excel打包传到异地的前端。
4、进行总结。
之前没有进行过后端的Excel生成,起初的时候感觉挺难的,但是当我看了网上的其他人写的解析以后,对于Java后端导出Excel有了概念,并且借助于网上热心网友提供的工具类,让我可以更好的借鉴和应用。现在,我将这些东西进行一个总结,在这谢谢那些cmdxy的帮助。
5、列出参考文献、参考博客