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();
时间: 2024-01-15 13:02:55 浏览: 174
无法解析的外部符号”private: char * __cdecl cv::String::allocate(unsigned __int64)” (?allocate@String@cv@@AEAA
这段代码是一个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();
}
```
阅读全文