在html文件中,我有一张与员工联系的表格。 该表具有不同的过滤器,还有一个下载文件按钮。
按下按钮时,将调用该功能
function ParseDocument() {
var table = document.getElementById('allContact');
var contactDTOList = new Array();
var i =0;
for (var r = 2, n = table.rows.length; r < n; r++) {
if (table.rows[r].style.display !== 'none') {
var s = table.rows[r].cells[0].innerHTML;
var bits = s.split(/\D/);
var date = new Date(bits[0], --bits[1], bits[2], bits[3], bits[4], bits[5]);
var contactDTO = {
dateOfBirth:date.getTime(),
firstName:table.rows[r].cells[1].innerHTML,
lastName:table.rows[r].cells[2].innerHTML,
email:table.rows[r].cells[3].innerHTML
};
contactDTOList.push(contactDTO);
i++;
}
}
$.ajax({
type: 'POST',
url: "/rest/bot5",
data: JSON.stringify(contactDTOList),
contentType: "application/json;charset=UTF-8",
async: true,
success: function () {
}
});
}
接下来,所有通过过滤器的联系人都将落入控制器java
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(value = "/test")
public class BotRestController {
@PostMapping(value = "/bot5", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public FileOutputStream postOrderingTask(@RequestBody List<Contact> contactList) throws IOException {
String[] columns = {"First Name", "Last Name", "Email", "Date Of Birth"};
List<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact("Sylvain", "Saurel", "sylvain.saurel@gmail.com", "2018-09-25 16:28:47"));
contacts.add(new Contact("Albert", "Dupond", "sylvain.saurel@gmail.com", "2018-09-25 17:28:47"));
contacts.add(new Contact("Pierre", "Dupont", "sylvain.saurel@gmail.com", "2018-09-25 18:28:47"));
contacts.add(new Contact("Mariano", "Diaz", "sylvain.saurel@gmail.com", "2018-09-25 19:28:47"));
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Contacts");
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 14);
headerFont.setColor(IndexedColors.RED.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
// Create a Row
Row headerRow = sheet.createRow(0);
for (int i = 0; i < columns.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columns[i]);
cell.setCellStyle(headerCellStyle);
}
// Create Other rows and cells with contacts data
int rowNum = 1;
for (Contact contact : contacts) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(contact.firstName);
row.createCell(1).setCellValue(contact.lastName);
row.createCell(2).setCellValue(contact.email);
row.createCell(3).setCellValue(contact.dateOfBirth);
}
// Resize all columns to fit the content size
for (int i = 0; i < columns.length; i++) {
sheet.autoSizeColumn(i);
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("contacts.xlsx");
workbook.write(fileOut);
fileOut.close();
return fileOut;
}
}
单击html中的按钮时,如何下载带有联系人的文件?
答案 0 :(得分:1)
您已将所有详细信息保存在 contacts.xlsx 中。现在作为响应,从部署目录发送文件的路径。 说路径= /projectname/contacts.xlsx 在成功函数中,使用window.open(ip:port:// path);
在ajax通话中 成功:函数()应该为成功:函数(数据)在数据变量中,您将从服务器获得响应
答案 1 :(得分:0)
返回内容类型为"application/octet-stream"
的文件将提示您的浏览器下载文件。
Path path = Paths.get(file.getAbsolutePath());
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);