多平台统一管理软件接口,如何实现多平台统一管理软件接口
442
2023-01-14
Java中实现简单的Excel导出
简单介绍一下java中的Excel文件导出功能(基于HttpServletResponse实现下载)
首先,引入需要依赖的jar包:
编写一个工具类:
package exceloutput;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;
/**
* @author haozz
* @date 2018/6/6 9:57
* @description excel导出抽象工具类
**/
public abstract class ExportAbstractUtil {
public void write(HttpServletResponse response, Workbook workbook){
String fileName = UUID.randomUUID().toString()+".xls";
pwrite(response,workbook,fileName);
}
public void write(HttpServletResponse response,Workbook workbook,String fileName){
if(StringUtils.isEmpty(fileName)){
fileName = UUID.randomUUID().toString()+".xls";
}
pwrite(response,workbook,fileName);
}
public void write(HttpServletResponse response, List> lists,String fileName){
if(StringUtils.isEmpty(fileName)){
fileName = UUID.randomUUID().toString()+".xls";
}
SXSSFWorkbook workbook = new SXSSFWorkbook(lists.size());
SXSSFSheet sheet = workbook.createSheet(fileName.substring(0,fileName.indexOf(".xls")));
Integer rowIndex = 0;
Row row = null;
Cell cell = null;
for(List
Integer columnIndex = 0;
row = sheet.createRow(rowIndex++);
for(String columnVal:rowData){
cell = row.createCell(columnIndex++);
cell.setCellValue(columnVal);
}
}
pwrite(response,workbook,fileName);
}
private void pwrite(HttpServletResponse response,Workbook workbook,String fileName){
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
try {
response.addHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("UTF-8"),"ISO8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
fileName= UUID.randomUUID().toString()+".xls";
response.addHeader("Content-Disposition", "attachment; filename="+fileName);
}
try {
workbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}
有了这个工具类就可以实现Excel导出了,代码不难,这里就不多解释了。
在SpringBoot项目中编写一个导出Excel的Controller,并继承上面的ExportAbstractUtil,给出一个接口用作测试:
package com.csdn.myboot.controller;
import com.csdn.myboot.utils.ExportAbstractUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author haozz
* @date 2018/6/6 10:14
* @description
**/
@Controller
@RequestMapping(value = "/index")
public class HelloCtrl extends ExportAbstractUtil{
@RequestMapping(value = "/testExcelOutPut")
@ResponseBody
public void testExcelOutPut(HttpServletResponse response){
//拼接数据start
List> lists = new ArrayList
>();
String rows[] = {"year","month","day"};
List
lists.add(rowsTitle);
for(int i = 0; i<=9;i++){
String [] rowss = {"1","2","3"};
List
lists.add(rowssList);
}
//拼接数据end
write(response,lists,"导出Excel.xls");
}
}
浏览器输入链接:
http://localhost:8099/index/testExcelOutPut
即可自动下载测试数据组成的Excel:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~