java统计报表,导出excle,自定义存储目录
做一个将查询出来的数据写入excel,并且点击导出按钮是,可以选择导出路径
**直接上完整代码
1.utils工具类,里面可以自己设置样式,详细看注释
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ExcelUtil {
/*sheet Name表名, titleName标题名, file Name文件名,
columnNumber列号, columnWidth列宽, columnName列名
*
*/
public static ResponseEntity<byte[]> ExportNoResponse(String sheetName, String titleName,String fileName, int columnNumber, int[] columnWidth,String[] columnName, String[][] dataList, HttpServletRequest request,HttpServletResponse response) throws Exception {
if (columnNumber == columnWidth.length && columnWidth.length == columnName.length) {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// sheet.setDefaultColumnWidth(15); //统一设置列宽
for (int i = 0; i < columnNumber; i++) {
for (int j = 0; j <= i; j++) {
if (i == j) {
sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
}
}
}
// 创建第0行 也就是标题
HSSFRow row1 = sheet.createRow((int) 0);
row1.setHeightInPoints(20);// 设备标题的高度
// 第三步创建标题的单元格样式style2以及字体样式headerFont1
HSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);