excel导出poi处理及table转为excel

poi导出

使用poi导出大量数据时,可以使用 SXSSFWorkbook

使用方法:

  1. 导入poi包及poi-ooxml包
1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
  1. 具体代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.liuyy.excel;

import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
* @author liuyy
* @className PoiExcelDemo
* @description TODO
* @date 2019/7/26 16:55
**/
public class PoiExcelDemo {
public static void main(String[] args) {
exportExcel("数据导出.xls");
}
public static void exportExcel(String fileName){
SXSSFWorkbook workbook = new SXSSFWorkbook();
//输出Excel文件
FileOutputStream output = null;
try {
//添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
String sheetName = "数据导出";
SXSSFSheet sheet = workbook.createSheet(sheetName);
// 设置缺省列高
sheet.setDefaultRowHeightInPoints(20);
// 设置缺省列宽
sheet.setDefaultColumnWidth(20);
// 设置字体
Font fontStyle = workbook.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short) 20);
// 创建XSSFRow对象
// 创建第一行-表头
SXSSFRow rowHead = sheet.createRow(0);
for (int i = 0; i < 8; i++) {
//创建XSSFCell对象
SXSSFCell cellHead = rowHead.createCell(i);
//设置单元格的值
cellHead.setCellValue(i);
}

// 创建具体数据
for (int i = 0; i < 100; i++) {
// 第i+1行
SXSSFRow row = sheet.createRow(i + 1);
row.createCell(0).setCellValue("第1列");
row.createCell(1).setCellValue("第2列");
row.createCell(2).setCellValue("第3列");
row.createCell(3).setCellValue("第4列");
row.createCell(4).setCellValue("第5列");
row.createCell(5).setCellValue("第6列");
}
output = new FileOutputStream(fileName);
workbook.write(output);
output.flush();
workbook.close();
output.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
workbook.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
  1. 结果如果所示:
{% asset_img pasted-5.png excel %}

table导出

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//获取表格
var exportFileContent = document.getElementById("showTable").outerHTML;
//设置格式为Excel,表格内容通过btoa转化为base64,此方法只在文件较小时使用(小于1M)
//exportFileContent = window.btoa(unescape(encodeURIComponent(exportFileContent)));
//var link = "data:"+MIMEType+";base64," + exportFileContent;

//使用Blob
var blob = new Blob([exportFileContent], {type: "text/plain;charset=utf-8"});
//解决中文乱码问题
blob = new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
//设置链接
var link = window.URL.createObjectURL(blob);

var a = document.createElement("a"); //创建a标签
a.download = filenName+".xls"; //设置被下载的超链接目标(文件名)
a.href = link; //设置a标签的链接
document.body.appendChild(a); //a标签添加到页面
a.click(); //设置a标签触发单击事件
document.body.removeChild(a); //移除a标签