最近有同事反映,使用OA系统过程中需要使用导出功能,说是有时间让我做一下,所以今天抽时间做一个,又想到导出功能以后是一个常用的功能,最好不要写的太死,能根据参数进行一些配置就好了,当需要其它导出内容时,能减少自己的开发时间。
ToolPoi.java
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;
import com.pointlion.sys.mvc.common.model.ZProject;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
import java.util.*;
/**
* poi工具类
* Created by 阳 on 2019/2/20.
*/
public class ToolPoi{
private String fileName;
private String sheetName;
private List<Map<String, String>> headName;
private List<Record> recordData;
public ToolPoi(int fileNum, int sheetNum, int headNum){
createFileName(fileNum);
createSheetName(sheetNum);
createHeadName(headNum);
}
/**
* 创建导出excel的文件名称
* @param num
*/
private void createFileName(int num){
String path = PathKit.getWebRootPath() + File.separator;
switch (num) {
case 0:
fileName = "项目台账";
break;
default:
fileName = "未知";
}
fileName = path + fileName + "_" + com.pointlion.sys.mvc.common.utils.DateUtil.dateToString(new Date(), 9) + ".xls";
}
/**
* 创建sheet名称
* @param num
*/
private void createSheetName(int num){
switch (num) {
case 0:
sheetName = "项目台账";
break;
default:
sheetName = "未知";
}
}
/**
* 创建表头模板:key数据库字段,value表头名称
* 最好可以利用Java的反射机制把数据库数据写入excel
* @param num
*/
public void createHeadName(int num){
headName = new ArrayList<>();
Map<String, String> map;
switch (num) {
case 0:
map = new HashMap<>();
map.put("headName", "项目编号");
map.put("columnName", "pro_no");
headName.add(map);
map = new HashMap<>();
map.put("headName", "项目名称");
map.put("columnName", "pro_name");
headName.add(map);
map = new HashMap<>();
map.put("headName", "项目类型");
map.put("columnName", "p_type");
headName.add(map);
map = new HashMap<>();
map.put("headName", "客户经理");
map.put("columnName", "user_name");
headName.add(map);
map = new HashMap<>();
map.put("headName", "申请日期");
map.put("columnName", "start_time");
headName.add(map);
map = new HashMap<>();
map.put("headName", "项目状态");
map.put("columnName", "pro_flag");
headName.add(map);
break;
default:
}
}
/**
* 获取想要写入excel的数据
* @param dataType 数据类型
* @param parameter 参数
*/
public void queryData(int dataType, String... parameter){
switch (dataType) {
case 0:
recordData = ZProject.dao.queryExcelData(parameter[0]);
break;
default:
recordData = new ArrayList<>();
}
}
/**
* 写入excel数据
* @return
*/
public File writeExcel(){
File file = new File(fileName);
// 创建工作薄
Workbook workbook = new HSSFWorkbook();
// 生成一个sheet页面
Sheet sheet = workbook.createSheet(sheetName);
// 设置表格默认列宽度为20个字节
sheet.setDefaultColumnWidth(20);
sheet.setDefaultRowHeightInPoints(24);
// 产生表格标题行
Row row = sheet.createRow(0);
// 生成一个表格标题行样式
CellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
// 生成一个字体
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setFontHeightInPoints((short) 12);
font.setBold(true);
// 把字体应用到当前的样式
style.setFont(font);
// 写入表头
for (int i = 0; headName != null && i < headName.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(style);
RichTextString text = new HSSFRichTextString(headName.get(i).get("headName"));
cell.setCellValue(text);
}
// 写入数据
for (int j = 0; recordData != null && j < recordData.size(); j++) {
row = sheet.createRow(j + 1);
Record record = recordData.get(j);
for (int k = 0; k < headName.size(); k++) {
Cell cell = row.createCell(k);
cell.setCellValue((String)record.get(headName.get(k).get("columnName")));
}
}
try {
OutputStream outputStream = new FileOutputStream(file);
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
}
ExportController.java
import com.jfinal.aop.Before;
import com.pointlion.sys.interceptor.MainPageTitleInterceptor;
import com.pointlion.sys.mvc.common.base.BaseController;
import com.pointlion.sys.mvc.zUtils.ToolPoi;
/**
* 导出表格
* Created by 阳 on 2019/2/20.
*/
@Before(MainPageTitleInterceptor.class)
public class ExportController extends BaseController {
/**
* 项目台账导出
*/
public void exportProjectExcel(){
String year = getPara("year");
ToolPoi toolPoi = new ToolPoi(0, 0, 0);
toolPoi.queryData(0, year);
renderFile(toolPoi.writeExcel());
}
}
虽说可以实现下载功能,但是会在项目根目录产生文件。
(若有什么错误,请留言指正,3Q)