导出时候遇到的问题:
- poi导出excel不下载:不可以使用ajax提交(jp.post/jp.get);否则会无法出现文件自动下载的那个框,后台也不会报错,执行完就完了,没有任何反应;(使用的是window.location.href=url;参数也拼接在url路径中了);
- 不在方法上指定提交方式,就会既允许get,又允许post;所以就不指定了;
- 如果合并单元格的话,注意合并单元格前为每个格子都设置对应的样式,否则若设置边框的话,只有第一个格子有边框,其他的都没有;
- 想要导出到同一个excel中的多个sheet使用 workbook.createSheet();
前端js:
//导出
function reportExport(){
var ids = getIdSelections();
if(ids==''){
jp.error("请先选择要导出的报表!");
}else{
jp.confirm('确认要导出选中报表的数据吗?',function(){
var url = "${ctx}/pri/select/priReportExport/export?ids=" + ids;
window.location.href=url;
});
}
}
后台代码:
/**
* 导出选中的报表到excel文件
*/
@ResponseBody
@RequestMapping(value = "export")
public AjaxJson export(HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes) {
AjaxJson json = new AjaxJson();
//处理导出——begin
String p = "项目路径";
String name = "userfiles/exportMoudle/export.xlsx";//模板的路径
File newFile = FileUtils.createNewFile(p+"/"+name, p);
FileInputStream is = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
try {
is = new FileInputStream(newFile);// 将excel文件转为输入流
workbook = new XSSFWorkbook(is);// 创建个workbook
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
//cellStyle.setWrapText(true); //设置换行
//获得第一个sheet
sheet = workbook.getSheetAt(0);
//创建表头--begin
XSSFRow titleRow = sheet.createRow(0);
XSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellValue("xx监测表");//还需要设置具体的样式
//需要合并的单元格--起始行,结束行,起始列,结束列
CellRangeAddress cellRangeAddress = new CellRangeAddress(0,3,0,1);
sheet.addMergedRegion(cellRangeAddress);
//创建表头--end
//导出内容--begin
//从第二行开始
int rowNum = 4;
for(int i = 0; i < 1; i++){
//创建一行
XSSFRow row = sheet.createRow((rowNum + i));
XSSFCell cell = row.createCell(0);
cell.setCellValue(i);
cell.setCellStyle(cellStyle);
cell = row.createCell(1);
cell.setCellValue(i+1);
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellValue(i+2);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue(i+3);
cell.setCellStyle(cellStyle);
}
//导出内容--end
} catch (Exception e1) {
e1.printStackTrace();
}
if (sheet != null) {
try {
//写数据
FileOutputStream fos = new FileOutputStream(newFile);
workbook.write(fos);
fos.flush();
fos.close();
//下载
InputStream fis = new BufferedInputStream(new FileInputStream(newFile));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setContentType("text/html;charset=UTF-8");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/x-msdownload");
//定义名字
SimpleDateFormat formt = new SimpleDateFormat("yyyy-MM-dd");
String newName = "价格报送_" + formt.format(new Date()) + ".xlsx";
newName = URLEncoder.encode(newName, "UTF-8");
response.addHeader("Content-Disposition","attachment;filename=\"" + newName + "\"");
response.addHeader("Content-Length", "" + newFile.length());
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//删除创建的新文件
FileUtils.deleteFile(newFile);
//处理导出 ——end
return json;
}
工具类:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
public class FileUtils {
/**
* 读取excel模板,并复制到新文件中供写入和下载
* @param tempPath 模板路径+名称
* @param rPath 新路径
* @return
*/
public static File createNewFile(String tempPath, String rPath) {
// 读取模板,并赋值到新文件************************************************************
// 文件模板路径
File file = new File(tempPath);
// 保存文件的路径
String realPath = rPath;
// 新的文件名
String newFileName = System.currentTimeMillis() + ".xlsx";
// 判断路径是否存在
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdirs();
}
// 写入到新的excel
File newFile = new File(realPath, newFileName);
try {
newFile.createNewFile();
// 复制模板到新文件
fileChannelCopy(file, newFile);
} catch (Exception e) {
e.printStackTrace();
}
return newFile;
}
/**
* 复制文件
*
* @param s
* 源文件
* @param t
* 复制到的新文件
*/
public static void fileChannelCopy(File s, File t) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(s), 1024);
out = new BufferedOutputStream(new FileOutputStream(t), 1024);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下载成功后删除
*
* @param files
*/
public static void deleteFile(File... files) {
for (File file : files) {
if (file.exists()) {
file.delete();
}
}
}
/**
* 读取任意模板,并复制到新文件中供写入和下载
* @param file 源文件
* @param rPath 新路径
* @param newFileName 新文件名称
* @return
*/
public sta