public void exportFile(DayDTO dayDTO, Page<DayDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception { String fileName = options.getFilename(); QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition(dayDTO, DayDTO.class); if (ExportMode.current.equals(options.getMode())) { // 导出当前页数据 } else if (ExportMode.selected.equals(options.getMode())) { // 导出选中数据 queryWrapper.in("a.id", options.getSelectIds()); } else { // 导出全部数据 page.setSize(-1); page.setCurrent(0); } List<DayDTO> result = dayService.findPage(page, queryWrapper).getRecords(); EasyExcelUtils.newInstance(dayService, dayWrapper).exportExcel(result, options.getSheetName(), DayDTO.class, fileName, options.getExportFields(), response); }
时间: 2024-04-18 17:33:46 浏览: 13
这段代码是一个导出文件的方法。它接收DayDTO对象、Page对象、ExcelOptions对象和HttpServletResponse对象作为参数。首先,根据ExcelOptions对象的模式(ExportMode),判断是导出当前页数据、导出选中数据还是导出全部数据。根据不同的情况,使用QueryWrapper对象生成查询条件,然后调用dayService的findPage方法查询数据,并将结果导出为Excel文件。最后,使用HttpServletResponse对象将Excel文件返回给前端。
相关问题
public void exportFile(DayDTO dayDTO, Page<DayDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception { String fileName = options.getFilename(); QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition(dayDTO, DayDTO.class); if (ExportMode.current.equals(options.getMode())) { // 导出当前页数据 } else if (ExportMode.selected.equals(options.getMode())) { // 导出选中数据 queryWrapper.in("a.id", options.getSelectIds()); } else { // 导出全部数据 page.setSize(-1); page.setCurrent(0); } List<DayDTO> result = dayService.findPage(page, queryWrapper).getRecords(); EasyExcelUtils.newInstance(dayService, dayWrapper).exportExcel(result, options.getSheetName(), DayDTO.class, fileName, options.getExportFields(), response); }
这段代码是一个导出文件的方法。它接收DayDTO对象、Page对象、ExcelOptions对象和HttpServletResponse对象作为参数。
首先,通过ExcelOptions对象获取文件名。
然后,根据ExcelOptions对象的模式(ExportMode),判断是导出当前页数据、导出选中数据还是导出全部数据。
- 如果是导出当前页数据,那么不需要做任何操作。
- 如果是导出选中数据,使用QueryWrapper对象的in方法,传入属性名 "a.id" 和选中的id列表 options.getSelectIds()。
- 如果是导出全部数据,将Page对象的大小设为-1,当前页设为0,以获取所有数据。
接下来,使用QueryWrapper对象生成查询条件,并调用dayService的findPage方法查询数据,并通过getRecords()方法获取查询结果列表。
最后,使用EasyExcelUtils工具类的exportExcel方法将查询结果列表导出为Excel文件。该方法的参数包括结果列表result,工作表名options.getSheetName(),实体类DayDTO.class,文件名fileName,导出字段列表options.getExportFields()和HttpServletResponse对象response。
rivate static void export(HttpServletResponse response, File file, String fileName, if (response != null) { // 前端导出 try { write(response, book, fileName); } catch (IOException e) { e.printStackTrace(); } } else { // 本地导出 FileOutputStream fos; try { fos = new FileOutputStream(file); ByteArrayOutputStream ops = new ByteArrayOutputStream(); book.write(ops); fos.write(ops.toByteArray()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) { // 整个 Excel 表格 book 对象 SXSSFWorkbook book = new SXSSFWorkbook(); // 每个 Sheet 页 Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet(); for (Entry<String, List<List<Object>>> entry : entries) { List<List<Object>> sheetDataList = entry.getValue(); Sheet sheet = book.createSheet(entry.getKey()); Drawing<?> patriarch = sheet.createDrawingPatriarch();
这段代码是一个Excel导出的实现,其中包含了两种导出方式:前端导出和本地导出。具体实现如下:
1. 首先创建一个SXSSFWorkbook对象,表示整个Excel表格。
2. 遍历sheetMap中的每个Sheet页,创建对应的Sheet对象,并将Sheet页的名称设置为entry.getKey()。
3. 对于每个Sheet页,遍历其对应的数据列表sheetDataList,创建对应的行和单元格,并将数据填充到单元格中。
4. 对于每个Sheet页,如果有需要下拉选择的单元格,将其加入到数据验证中。
5. 如果response不为空,则表示前端导出,将book写入response流中。否则,表示本地导出,将book写入文件中。
下面是代码的具体实现:
```java
private static void export(HttpServletResponse response, File file, String fileName,
Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) {
// 整个 Excel 表格 book 对象
SXSSFWorkbook book = new SXSSFWorkbook();
// 每个 Sheet 页
Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet();
for (Entry<String, List<List<Object>>> entry : entries) {
List<List<Object>> sheetDataList = entry.getValue();
Sheet sheet = book.createSheet(entry.getKey());
Drawing<?> patriarch = sheet.createDrawingPatriarch();
// 遍历每一行数据
for (int i = 0; i < sheetDataList.size(); i++) {
Row row = sheet.createRow(i);
List<Object> rowDataList = sheetDataList.get(i);
// 遍历每个单元格
for (int j = 0; j < rowDataList.size(); j++) {
Cell cell = row.createCell(j);
Object cellData = rowDataList.get(j);
// 判断单元格数据类型,并设置单元格格式
if (cellData instanceof String) {
cell.setCellValue((String) cellData);
} else if (cellData instanceof Number) {
cell.setCellValue(((Number) cellData).doubleValue());
} else if (cellData instanceof Date) {
cell.setCellValue((Date) cellData);
CellStyle style = book.createCellStyle();
style.setDataFormat(book.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
cell.setCellStyle(style);
} else if (cellData instanceof Calendar) {
cell.setCellValue((Calendar) cellData);
CellStyle style = book.createCellStyle();
style.setDataFormat(book.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
cell.setCellStyle(style);
} else if (cellData instanceof Boolean) {
cell.setCellValue((Boolean) cellData);
} else if (cellData == null) {
cell.setCellValue("");
}
// 设置下拉选择
if (selectMap.containsKey(j)) {
List<String> selectList = selectMap.get(j);
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(selectList.toArray(new String[0]));
CellRangeAddressList addressList = new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex());
DataValidation validation = helper.createValidation(constraint, addressList);
sheet.addValidationData(validation);
}
}
}
}
// 导出Excel
if (response != null) {
// 前端导出
try {
write(response, book, fileName);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// 本地导出
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ByteArrayOutputStream ops = new ByteArrayOutputStream();
book.write(ops);
fos.write(ops.toByteArray());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
其中,write()方法用于将book写入response流中,实现前端导出。具体实现如下:
```java
private static void write(HttpServletResponse response, SXSSFWorkbook book, String fileName) throws IOException {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream os = response.getOutputStream();
book.write(os);
os.flush();
os.close();
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
请把下面这段代码改成rest风格 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); List productList = (List) getServletContext().getAttribute("products"); for (Product product : productList) { String item = response.encodeURL(request.getContextPath() + "/item?id=" + product.getId()); response.getWriter().println("" + product.getName() + ""); } response.getWriter().println("浏览足迹为:"); History.showRecentViews(request, response); }
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)