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

pull/298/head
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.UUID;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
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.WorkbookFactory;
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.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
@ -339,6 +341,23 @@ public class ExcelUtil<T>
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
*
@ -351,6 +370,43 @@ public class ExcelUtil<T>
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
*
@ -361,27 +417,7 @@ public class ExcelUtil<T>
OutputStream out = null;
try
{
// 取出一共有多少个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)
{
Excel excel = (Excel) os[1];
this.createCell(excel, row, column++);
}
if (Type.EXPORT.equals(type))
{
fillExcelData(index, row);
addStatisticsRow();
}
}
writeSheet();
String filename = encodingFilename(sheetName);
out = new FileOutputStream(getAbsoluteFile(filename));
wb.write(out);
@ -394,27 +430,35 @@ public class ExcelUtil<T>
}
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
{
wb.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
Excel excel = (Excel) os[1];
this.createCell(excel, row, column++);
}
if (out != null)
if (Type.EXPORT.equals(type))
{
try
{
out.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
fillExcelData(index, row);
addStatisticsRow();
}
}
}
@ -550,8 +594,7 @@ public class ExcelUtil<T>
}
else if (ColumnType.IMAGE == attr.cellType())
{
ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
cell.getRow().getRowNum() + 1);
ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1);
String imagePath = Convert.toStr(value);
if (StringUtils.isNotEmpty(imagePath))
{