Commit c19d6b89 authored by wujipeng's avatar wujipeng
Browse files

导出全部模板

parent f766d49d
......@@ -65,6 +65,12 @@ public class ExcelTemplateController extends BaseController {
excelTemplateService.export(ids, response);
}
@ApiOperation(value = "导出全部")
@GetMapping("/export/all")
public void exportAll(HttpServletResponse response) {
excelTemplateService.exportAll(response);
}
@ApiOperation(value = "导入")
@PostMapping("/import")
public Result<Boolean> importExcel(@RequestParam("file") MultipartFile file) {
......
......@@ -40,6 +40,12 @@ public interface ExcelTemplateService extends IService<ExcelTemplateEntity> {
*/
void export(List<String> ids, HttpServletResponse response);
/**
* 导出所有
* @param response response
*/
void exportAll(HttpServletResponse response);
/**
* 导入
* @param file file
......
......@@ -19,7 +19,10 @@ import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
......@@ -134,6 +137,69 @@ public class ExcelTemplateServiceImpl extends ServiceImpl<ExcelTemplateMapper, E
}
}
@Override
public void exportAll(HttpServletResponse response) {
// 设置响应头
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = "模板.xlsx";
try {
// 设置响应头,告诉浏览器这是文件下载
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("文件名编码异常", e);
}
// 创建一个excel文件对象
try (XSSFWorkbook workbook = new XSSFWorkbook();
OutputStream outputStream = response.getOutputStream()) {
List<ExcelTemplateEntity> list = list();
for (int i = 0; i < list.size(); i++) {
XSSFSheet sheet = workbook.createSheet("sheet" + i);
sheet.createRow(0);
sheet.createRow(1);
sheet.createRow(2);
sheet.createRow(3);
String json = list.get(i).getJson();
List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class);
for (int j = 0; j < tableHeaderList.size(); j++) {
TableHeaderVO tableHeaderVO = tableHeaderList.get(j);
if (ObjectUtils.isEmpty(tableHeaderVO)) {
sheet.getRow(0).createCell(j).setCellValue("-");
sheet.getRow(1).createCell(j).setCellValue("-");
} else {
sheet.getRow(0).createCell(j).setCellValue(tableHeaderVO.getName());
sheet.getRow(1).createCell(j).setCellValue(tableHeaderVO.getIndex());
}
}
// 自动调整列宽
for (int j = 0; j < tableHeaderList.size(); j++) {
sheet.autoSizeColumn(j);
}
String allHeader = list.get(i).getAllHeader();
List<TableHeaderVO> tableHeaderList1 = JSON.parseArray(allHeader, TableHeaderVO.class);
for (int j = 0; j < tableHeaderList1.size(); j++) {
TableHeaderVO tableHeaderVO = tableHeaderList1.get(j);
if (ObjectUtils.isEmpty(tableHeaderVO)) {
sheet.getRow(2).createCell(j).setCellValue("-");
sheet.getRow(3).createCell(j).setCellValue("-");
} else {
sheet.getRow(2).createCell(j).setCellValue(tableHeaderVO.getName());
sheet.getRow(3).createCell(j).setCellValue(tableHeaderVO.getIndex());
}
}
// 自动调整列宽
for (int j = 0; j < tableHeaderList1.size(); j++) {
sheet.autoSizeColumn(j);
}
}
// 写入输出流
workbook.write(outputStream);
outputStream.flush();
} catch (IOException e) {
throw new RuntimeException("导出Excel失败", e);
}
}
@Override
@Transactional
public Boolean importExcel(MultipartFile file) {
......@@ -165,7 +231,7 @@ public class ExcelTemplateServiceImpl extends ServiceImpl<ExcelTemplateMapper, E
);
for (int cellIndex = 0; cellIndex < maxCells; cellIndex++) {
String name = getCellValueAsString(row0.getCell(cellIndex));
int index = getCellValueAsInt(row1.getCell(cellIndex),0);
int index = getCellValueAsInt(row1.getCell(cellIndex), 0);
if (!"-".equals(name)) {
TableHeaderVO header = new TableHeaderVO();
......@@ -185,7 +251,7 @@ public class ExcelTemplateServiceImpl extends ServiceImpl<ExcelTemplateMapper, E
);
for (int cellIndex = 0; cellIndex < maxCells; cellIndex++) {
String name = getCellValueAsString(row2.getCell(cellIndex));
int index = getCellValueAsInt(row3.getCell(cellIndex),0);
int index = getCellValueAsInt(row3.getCell(cellIndex), 0);
if (!"-".equals(name)) {
TableHeaderVO header = new TableHeaderVO();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment