|
|
@@ -170,7 +170,6 @@ public class NewExcelUtil<T> {
|
|
|
* 导出CXCEL表到指定目录
|
|
|
* @param list 导出数据集合
|
|
|
* @param sheetName 工作表的名称
|
|
|
- * @param response
|
|
|
*/
|
|
|
public Result exportExcel(List<T> list, String sheetName, String uploadPath) {
|
|
|
this.init(list, sheetName);
|
|
|
@@ -180,35 +179,41 @@ public class NewExcelUtil<T> {
|
|
|
|
|
|
private void exportExcel(HttpServletResponse response) {
|
|
|
try {
|
|
|
- OutputStream out = response.getOutputStream();
|
|
|
- // 取出一共有多少个sheet.
|
|
|
- int sheetNo = (int) Math.ceil(list.size() / sheetSize);
|
|
|
-
|
|
|
- String dateFilename =DateFilename(sheetName);
|
|
|
- for (int index = 0; index <= sheetNo; index++) {
|
|
|
- createSheet(sheetNo, index);
|
|
|
- // 产生一行
|
|
|
- Row title = sheet.createRow(0);
|
|
|
- Cell cell = title.createCell(0);
|
|
|
- cell.setCellValue(sheetName);
|
|
|
- CellRangeAddress region = new CellRangeAddress(0, 0, 0, fields.size()-1);
|
|
|
- sheet.addMergedRegion(region);
|
|
|
- cell.setCellStyle(styles.get("total"));
|
|
|
- Row row = sheet.createRow(1);
|
|
|
- int column = 0;
|
|
|
- // 写入各个字段的列头名称
|
|
|
- for (Object[] os : fields) {
|
|
|
- Excel excel = (Excel) os[1];
|
|
|
- this.createCell(excel, row, column++);
|
|
|
+ log.debug("导出至本地开始");
|
|
|
+
|
|
|
+ if (list.isEmpty()){
|
|
|
+ log.debug("导出数据为空");
|
|
|
+ }else{
|
|
|
+ log.debug("导出数据"+list.size());
|
|
|
+ OutputStream out = response.getOutputStream();
|
|
|
+ // 取出一共有多少个sheet.
|
|
|
+ int sheetNo = (int) Math.ceil(list.size() / sheetSize);
|
|
|
+ String dateFilename =DateFilename(sheetName);
|
|
|
+ for (int index = 0; index <= sheetNo; index++) {
|
|
|
+ createSheet(sheetNo, index);
|
|
|
+ // 产生一行
|
|
|
+ Row title = sheet.createRow(0);
|
|
|
+ Cell cell = title.createCell(0);
|
|
|
+ cell.setCellValue(sheetName);
|
|
|
+ CellRangeAddress region = new CellRangeAddress(0, 0, 0, fields.size()-1);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ cell.setCellStyle(styles.get("total"));
|
|
|
+ Row row = sheet.createRow(1);
|
|
|
+ int column = 0;
|
|
|
+ // 写入各个字段的列头名称
|
|
|
+ for (Object[] os : fields) {
|
|
|
+ Excel excel = (Excel) os[1];
|
|
|
+ this.createCell(excel, row, column++);
|
|
|
+ }
|
|
|
+ fillExcelData(index, row);
|
|
|
+ addStatisticsRow();
|
|
|
}
|
|
|
- fillExcelData(index, row);
|
|
|
- addStatisticsRow();
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + new String(dateFilename.getBytes(),"iso-8859-1"));
|
|
|
+ response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
+ wb.write(out);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
}
|
|
|
- response.addHeader("Content-Disposition", "attachment;filename=" + new String(dateFilename.getBytes(),"iso-8859-1"));
|
|
|
- response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
- wb.write(out);
|
|
|
- out.flush();
|
|
|
- out.close();
|
|
|
} catch (Exception e) {
|
|
|
log.error("导出Excel异常{}", e.getMessage());
|
|
|
throw new BusinessException("导出Excel失败,请联系网站管理员!");
|
|
|
@@ -1021,7 +1026,6 @@ public class NewExcelUtil<T> {
|
|
|
/**
|
|
|
* 读取文件为字节数据
|
|
|
*
|
|
|
- * @param key 地址
|
|
|
* @return 字节数据
|
|
|
*/
|
|
|
public static byte[] readFile(String url) {
|