导出Excel文件支持数据流下载方式

master
RuoYi 2021-06-03 19:19:06 +08:00
parent 3283955284
commit 4b59c590e0
1 changed files with 94 additions and 51 deletions

View File

@ -18,6 +18,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
@ -38,6 +39,7 @@ import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation; import org.apache.poi.xssf.usermodel.XSSFDataValidation;
@ -116,12 +118,12 @@ public class ExcelUtil<T>
* *
*/ */
private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
/** /**
* *
*/ */
private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
/** /**
* *
*/ */
@ -339,6 +341,23 @@ public class ExcelUtil<T>
return exportExcel(); return exportExcel();
} }
/**
* listexcel
*
* @param response
* @param list
* @param sheetName
* @return
* @throws IOException
*/
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) throws IOException
{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
this.init(list, sheetName, Type.EXPORT);
exportExcel(response.getOutputStream());
}
/** /**
* listexcel * listexcel
* *
@ -351,6 +370,43 @@ public class ExcelUtil<T>
return exportExcel(); return exportExcel();
} }
/**
* listexcel
*
* @param sheetName
* @return
*/
public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException
{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
this.init(null, sheetName, Type.IMPORT);
exportExcel(response.getOutputStream());
}
/**
* listexcel
*
* @return
*/
public void exportExcel(OutputStream out)
{
try
{
writeSheet();
wb.write(out);
}
catch (Exception e)
{
log.error("导出Excel异常{}", e.getMessage());
}
finally
{
IOUtils.closeQuietly(wb);
IOUtils.closeQuietly(out);
}
}
/** /**
* listexcel * listexcel
* *
@ -361,27 +417,7 @@ public class ExcelUtil<T>
OutputStream out = null; OutputStream out = null;
try try
{ {
// 取出一共有多少个sheet. writeSheet();
double sheetNo = Math.ceil(list.size() / sheetSize);
for (int index = 0; index <= sheetNo; index++)
{
createSheet(sheetNo, index);
// 产生一行
Row row = sheet.createRow(0);
int column = 0;
// 写入各个字段的列头名称
for (Object[] os : fields)
{
Excel excel = (Excel) os[1];
this.createCell(excel, row, column++);
}
if (Type.EXPORT.equals(type))
{
fillExcelData(index, row);
addStatisticsRow();
}
}
String filename = encodingFilename(sheetName); String filename = encodingFilename(sheetName);
out = new FileOutputStream(getAbsoluteFile(filename)); out = new FileOutputStream(getAbsoluteFile(filename));
wb.write(out); wb.write(out);
@ -394,27 +430,35 @@ public class ExcelUtil<T>
} }
finally finally
{ {
if (wb != null) IOUtils.closeQuietly(wb);
IOUtils.closeQuietly(out);
}
}
/**
* Sheet
*/
public void writeSheet()
{
// 取出一共有多少个sheet.
double sheetNo = Math.ceil(list.size() / sheetSize);
for (int index = 0; index <= sheetNo; index++)
{
createSheet(sheetNo, index);
// 产生一行
Row row = sheet.createRow(0);
int column = 0;
// 写入各个字段的列头名称
for (Object[] os : fields)
{ {
try Excel excel = (Excel) os[1];
{ this.createCell(excel, row, column++);
wb.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
} }
if (out != null) if (Type.EXPORT.equals(type))
{ {
try fillExcelData(index, row);
{ addStatisticsRow();
out.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
} }
} }
} }
@ -486,7 +530,7 @@ public class ExcelUtil<T>
headerFont.setColor(IndexedColors.WHITE.getIndex()); headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont); style.setFont(headerFont);
styles.put("header", style); styles.put("header", style);
style = wb.createCellStyle(); style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER);
@ -550,8 +594,7 @@ public class ExcelUtil<T>
} }
else if (ColumnType.IMAGE == attr.cellType()) else if (ColumnType.IMAGE == attr.cellType())
{ {
ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1);
cell.getRow().getRowNum() + 1);
String imagePath = Convert.toStr(value); String imagePath = Convert.toStr(value);
if (StringUtils.isNotEmpty(imagePath)) if (StringUtils.isNotEmpty(imagePath))
{ {
@ -561,7 +604,7 @@ public class ExcelUtil<T>
} }
} }
} }
/** /**
* *
*/ */
@ -804,7 +847,7 @@ public class ExcelUtil<T>
} }
return StringUtils.stripEnd(propertyString.toString(), separator); return StringUtils.stripEnd(propertyString.toString(), separator);
} }
/** /**
* *
* *
@ -830,7 +873,7 @@ public class ExcelUtil<T>
{ {
return DictUtils.getDictValue(dictType, dictLabel, separator); return DictUtils.getDictValue(dictType, dictLabel, separator);
} }
/** /**
* *
*/ */
@ -867,7 +910,7 @@ public class ExcelUtil<T>
cell = row.createCell(0); cell = row.createCell(0);
cell.setCellStyle(styles.get("total")); cell.setCellStyle(styles.get("total"));
cell.setCellValue("合计"); cell.setCellValue("合计");
for (Integer key : keys) for (Integer key : keys)
{ {
cell = row.createCell(key); cell = row.createCell(key);
@ -985,7 +1028,7 @@ public class ExcelUtil<T>
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList()); this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
this.maxHeight = getRowHeight(); this.maxHeight = getRowHeight();
} }
/** /**
* *
*/ */
@ -1099,7 +1142,7 @@ public class ExcelUtil<T>
} }
return val; return val;
} }
/** /**
* *
* *
@ -1112,7 +1155,7 @@ public class ExcelUtil<T>
{ {
return true; return true;
} }
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++)
{ {
Cell cell = row.getCell(i); Cell cell = row.getCell(i);
if (cell != null && cell.getCellType() != CellType.BLANK) if (cell != null && cell.getCellType() != CellType.BLANK)