import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class PoiUtil {
//文件名 准备数据 数据Class 文件保存路径
public static String getUrl(String excelName, List listInfo, Class c, String savePath) throws IllegalAccessException {
// 创建Excel文件
Workbook workbook = new HSSFWorkbook();
// 创建sheet
Sheet sheet = workbook.createSheet(excelName);
//设置默认列宽
sheet.setDefaultColumnWidth(30);
// style 为单元格设置样式
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setColor(Font.COLOR_RED);
// 加粗
font.setBold(true);
font.setFontName("宋体");
// 设置居中
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font);
// 准备数据
// listInfo
/**
* 获得所有属性名作为标题
*/
Field[] fields = c.getDeclaredFields();
// 创建行
Row titleRow = sheet.createRow(0);
for (int i = 0; i < fields.length; i++) {
com.manage.model.Comment comment = fields[i].getAnnotation( com.manage.model.Comment.class);
String value = comment.value();
// 创建单元格
Cell cell = titleRow.createCell(i);
cell.setCellValue(value);
//设置样式
cell.setCellStyle(style);
}
/**
* 设置内容
*/
for (int i = 0; i < listInfo.size(); i++) {
Row row = sheet.createRow(i + 1);
for (int j = 0; j < fields.length; j++) {
// 打破封装
fields[j].setAccessible(true);
Object val = fields[j].get(listInfo.get(i));
// System.out.println(val);
Cell cell = row.createCell(j);
// 创建单元格
if (fields[j].getType() == Date.class) {
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(val);
cell.setCellValue(format);
} else {
cell.setCellValue(val.toString());
}
}
}
// 通过流输出
// HttpServletRequest request;
// String imgPath = request.getSession().getServletContext().getRealPath("/admin/page/productImages");
String title = excelName + "表.xls";
File file = new File(savePath, title);
try {
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
System.out.println("信息导出成功!");
} catch (FileNotFoundException e) { // TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { // TODO Auto-generated
e.printStackTrace();
}
return savePath;
}
}
使用:
@RequestMapping("/getPrint")
@ResponseBody
@Log(operationName = "打印订单",operationType = "print")
public Result getPrint(HttpServletRequest request) {
Result result = new Result();
List<OrderPoi> orderPoiList = orderService.printOrder();
//获取存放路径
String savePath = request.getSession().getServletContext().getRealPath("/admin/page");
String path = null;
try {
path = PoiUtil.getUrl("订单信息",orderPoiList,new OrderPoi().getClass(),savePath);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.println(path);
if (path != null) {
result.setCode(1);
result.setError(false);
result.setMessage("订单打印成功");
result.setData("/manage/admin/page/订单信息表.xls");
} else {
result.setCode(0);
result.setError(true);
result.setMessage("订单打印失败");
result.setData(null);
}
return result;
}