导出Excel文件支持数据流下载方式
This commit is contained in:
parent
3283955284
commit
4b59c590e0
@ -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;
|
||||||
@ -339,6 +341,23 @@ public class ExcelUtil<T>
|
|||||||
return exportExcel();
|
return exportExcel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对list数据源将其里面的数据导入到excel表单
|
||||||
|
*
|
||||||
|
* @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());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对list数据源将其里面的数据导入到excel表单
|
* 对list数据源将其里面的数据导入到excel表单
|
||||||
*
|
*
|
||||||
@ -351,6 +370,43 @@ public class ExcelUtil<T>
|
|||||||
return exportExcel();
|
return exportExcel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对list数据源将其里面的数据导入到excel表单
|
||||||
|
*
|
||||||
|
* @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());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对list数据源将其里面的数据导入到excel表单
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对list数据源将其里面的数据导入到excel表单
|
* 对list数据源将其里面的数据导入到excel表单
|
||||||
*
|
*
|
||||||
@ -360,6 +416,29 @@ public class ExcelUtil<T>
|
|||||||
{
|
{
|
||||||
OutputStream out = null;
|
OutputStream out = null;
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
writeSheet();
|
||||||
|
String filename = encodingFilename(sheetName);
|
||||||
|
out = new FileOutputStream(getAbsoluteFile(filename));
|
||||||
|
wb.write(out);
|
||||||
|
return AjaxResult.success(filename);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.error("导出Excel异常{}", e.getMessage());
|
||||||
|
throw new BusinessException("导出Excel失败,请联系网站管理员!");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
IOUtils.closeQuietly(wb);
|
||||||
|
IOUtils.closeQuietly(out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建写入数据到Sheet
|
||||||
|
*/
|
||||||
|
public void writeSheet()
|
||||||
{
|
{
|
||||||
// 取出一共有多少个sheet.
|
// 取出一共有多少个sheet.
|
||||||
double sheetNo = Math.ceil(list.size() / sheetSize);
|
double sheetNo = Math.ceil(list.size() / sheetSize);
|
||||||
@ -382,41 +461,6 @@ public class ExcelUtil<T>
|
|||||||
addStatisticsRow();
|
addStatisticsRow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String filename = encodingFilename(sheetName);
|
|
||||||
out = new FileOutputStream(getAbsoluteFile(filename));
|
|
||||||
wb.write(out);
|
|
||||||
return AjaxResult.success(filename);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
log.error("导出Excel异常{}", e.getMessage());
|
|
||||||
throw new BusinessException("导出Excel失败,请联系网站管理员!");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (wb != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
wb.close();
|
|
||||||
}
|
|
||||||
catch (IOException e1)
|
|
||||||
{
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (out != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
catch (IOException e1)
|
|
||||||
{
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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))
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user