1. View export examples
2. Main export code
public void ex2(String start, String end, HttpServletResponse response) {
String[] title = {
"时间", "内容1", "内容2", "内容3"};
String fileName = "文件名称.xls";
String sheetName = "Sheet1";
ArrayList<String> strings = null;
if (ToolUtil.isNotEmpty(start) && ToolUtil.isNotEmpty(end)) {
strings = DateUtils.getDays(start, end);
} else {
strings = DateUtils.lastDay(7);
Collections.reverse(strings);
}
String[][] content = new String[strings.size()][];
for (int x = 0; x < strings.size(); x++) {
content[x] = new String[title.length];
content[x][0] = strings.get(x);
for (int i = 1; i <= 7; i++) {
content[x][1] = "内容1";
content[x][2] = "内容2";
content[x][3] = "内容3";
}
}
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3. This is the return time collection method
public static ArrayList<String> getDays(String startTime, String endTime) {
ArrayList<String> days = new ArrayList<String>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
4. Time method for the last seven days
public static ArrayList<String> lastDay(int intervals) {
ArrayList<String> pastDaysList = new ArrayList<>();
for (int i = 0; i < intervals; i++) {
pastDaysList.add(getPastDate(i));
}
return pastDaysList;
}
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
return result;
}
5. Export Excel tool class
package cn.stylefeng.guns.sys.core.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
public class ExcelUtil {
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String []title, String [][]values, HSSFWorkbook wb){
if(wb == null){
wb = new HSSFWorkbook();
}
HSSFSheet sheet = wb.createSheet(sheetName);
HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
HSSFCell cell = null;
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
for(int j=0;j<values[i].length;j++){
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
6. The last step is to send the response stream method
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
7. Import dependencies
<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>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>