POI操作Excell表,导入,导出

POI操作Excell表

1,导入jar包
POI所需要的jar包
2,jsp页面 ,使用了一键上传的插件
一键上传插件

  • 导出数据 请选择 导出当前页数据 导出全部数据
//导入数据 $(function () { $("#button-import").upload({ action:"${ctx}/workOrderController/importExcel", name: 'myFile', onComplete: function(data) { if(data == '1'){ //上传成功 /* $.messager.alert("提示信息","区域数据导入成功!","info"); alert(1);*/ alert("上传成功"); $("#recieveListFrom").load("${ctx}/workOrderController/toRecieveList"); }else{ alert("上传失败"); /* //失败 $.messager.alert("提示信息","区域数据导入失败!","warning");*/ } }
    });
})
/**
 * 将数导出到excel表格
 */
function exportExcel() {
    var exportExcels= $("#exportExcel").val();
    //alert(exportExcel);
    if(exportExcels!=''&& exportExcels !=null && exportExcels!='null'){
    location.href = "${ctx}/workOrderController/exportExcel?exportExcels="+exportExcels;
    }
}

3,Controller层,将Excell表中的数据导入并保存到数据库
@RequestMapping(value="/importExcel")
@ResponseBody
public String importExcel(HttpServletRequest request, HttpServletResponse response)throws Exception{
String flag=“1”;
MultipartHttpServletRequest multipartRequest= (MultipartHttpServletRequest) request;
MultipartFile myFile = multipartRequest.getFile(“myFile”); 通过参数名获取指定文件 文件本身 变量名和文件上传时的 名称保持一致
String myFileName = myFile.getOriginalFilename();//文件的名字
List list = workOrderService.importExcel(myFile,request,response,myFileName);
if(list.isEmpty()){
flag=“0”;
}
//批量保存
workOrderService.batchAdd(list);
response.setContentType(“text/html;charset=UTF-8”);
response.getWriter().print(flag);
return null;
}
4,Service层,导入的主要业务逻辑
public List importExcel(MultipartFile myFile, HttpServletRequest request, HttpServletResponse response,String myFileName){
List list = new ArrayList();
Workbook workbook=null;
String fileType=myFileName.substring(myFileName.lastIndexOf("."));
//使用POI解析Excel文件
try {
InputStream in = myFile.getInputStream();
if(".xls".equals(fileType)){
workbook=new HSSFWorkbook(in);
}
if(".xlsx".equals(fileType)){
workbook=new XSSFWorkbook(in);
}
//获得第一个sheet页
Sheet sheet = null;
for(int j=0;j<workbook.getNumberOfSheets();j++){
sheet=workbook.getSheetAt(j);
for (Row row : sheet) {
int rowNum = row.getRowNum();
if (rowNum == 0) {
//第一行,标题行,忽略
continue;
}
List communityList = findAllCommunity(TableRoute.getUserCode());
String communityName = ExcelUtil.getCellValue(row.getCell(0));
String communityCode = “”;
if (null != communityList && communityList.size() > 0) {
int size = communityList.size();
for (int i = 0; i < size; i++) {
Community community = communityList.get(i);
String name = community.getCommunityName();
if (name.equals(communityName)) {
communityCode = community.getId();
break;
}
}
}
//来电人
String phoneMan =ExcelUtil.getCellValue(row.getCell(1));
//来电人号码
String phoneNum=ExcelUtil.getCellValue(row.getCell(2));
//地址
String address =ExcelUtil.getCellValue(row.getCell(3));
//问题分类
String problemName =ExcelUtil.getCellValue(row.getCell(4));
//工单,描述
String description =ExcelUtil.getCellValue(row.getCell(5));
//工单编号
String recieveNum =ExcelUtil.getCellValue(row.getCell(6));
//地图定位ID
String caseObjectId=ExcelUtil.getCellValue(row.getCell(7));
Recieve recieve = new Recieve();
recieve.setCommunityCode(communityCode);
recieve.setPhoneMan(phoneMan);
recieve.setPhoneNum(phoneNum);
recieve.setAddress(address);
recieve.setProblemName(problemName);
recieve.setDescription(description);
recieve.setRecieveNum(recieveNum);
recieve.setCaseObjectId(caseObjectId);
recieve.setIsDelete(0);
;//设置日期格式
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String createTime = df.format(new Date());
recieve.setCreateTime(createTime);
recieve.setUpdateTime("");
list.add(recieve);
}
}
}
catch (IOException e) {
e.printStackTrace();
}
return list;
}
将数据库中的数据导出到Excell表
1,Controller
@RequestMapping(value = “exportExcel”)
public void exportExcel(HttpServletRequest request,HttpServletResponse response){
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”);
String datatime = df.format(new Date());
String exportExcels=request.getParameter(“exportExcels”);
String filename = “花乡数据”+datatime+".xlsx";
String agent = request.getHeader(“User-Agent”);
filename = FileUtils.encodeDownloadFilename(filename, agent);
//一个流两个头
try {
ServletOutputStream outputStream = response.getOutputStream();
String contentType = request.getServletContext().getMimeType(filename);
response.setContentType(contentType);
response.setHeader(“content-disposition”, “attchment;filename=”+filename);
workOrderService.exportExcel(outputStream,request,exportExcels);
} catch (IOException e) {
e.printStackTrace();
}
}
2,service
public void exportExcel(ServletOutputStream outputStream,HttpServletRequest request,String exportExcels){
Map<String, Object> map=(Map<String, Object>)request.getSession().getAttribute(“mapSession”);
List list=null;
XSSFSheet sheet=null;
XSSFWorkbook workbook= new XSSFWorkbook();
//导出当前页的数据
if(exportExcels.equals(“currentPage”)){
list=recieveMapper.selectRecieveList(map);
sheet=workbook.createSheet(“花乡导出数据”);
sheet.setDefaultColumnWidth((short)20);
// 创建标题行
XSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue(“属地”);
headRow.createCell(1).setCellValue(“来电人姓名”);
headRow.createCell(2).setCellValue(“来电人号码”);
headRow.createCell(3).setCellValue(“地址”);
headRow.createCell(4).setCellValue(“问题分类”);
headRow.createCell(5).setCellValue(“工单描述”);
headRow.createCell(6).setCellValue(“工单编号”);
headRow.createCell(7).setCellValue(“地图定位id”);
headRow.createCell(8).setCellValue(“工单创键时间”);
headRow.createCell(9).setCellValue(“修改工单时间”);
for (Recieve recieve: list) {
//从第二行开始,去掉标题行
Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
//属地 通过code查找name值
String communityCode=recieve.getCommunityCode();
String communityName=communityMapper.findCommunityNameByCode(communityCode);
dataRow.createCell(0).setCellValue(communityName);
//来电人
dataRow.createCell(1).setCellValue(recieve.getPhoneMan());
//来电号码
dataRow.createCell(2).setCellValue(recieve.getPhoneNum());
//地址
dataRow.createCell(3).setCellValue(recieve.getAddress());
//问题分类
dataRow.createCell(4).setCellValue(recieve.getProblemName());
//工单描述
dataRow.createCell(5).setCellValue(recieve.getDescription());
//工单编号
dataRow.createCell(6).setCellValue(recieve.getRecieveNum());
//地图定位id
dataRow.createCell(7).setCellValue(recieve.getCaseObjectId());
//工单创建时间
dataRow.createCell(8).setCellValue(recieve.getCreateTime());
//工单修改时间
dataRow.createCell(9).setCellValue(recieve.getUpdateTime());
}
}
//导出全部的数据 分为全部导出,和导出当前页
else if(exportExcels.equals(“All”)){
//list=recieveMapper.findRecieveList(map);
int count = recieveMapper.selectRecieveCount(map);
int size=count/10+1;
for(int i=1;i<=size;i++){
page.setPageNo(i);
map.put(“page”, page);
list=recieveMapper.selectRecieveList(map);
sheet=workbook.createSheet(“花乡导出数据”+i);
sheet.setDefaultColumnWidth((short)20);
// 创建标题行
XSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue(“属地”);
headRow.createCell(1).setCellValue(“来电人姓名”);
headRow.createCell(2).setCellValue(“来电人号码”);
headRow.createCell(3).setCellValue(“地址”);
headRow.createCell(4).setCellValue(“问题分类”);
headRow.createCell(5).setCellValue(“工单描述”);
headRow.createCell(6).setCellValue(“工单编号”);
headRow.createCell(7).setCellValue(“地图定位id”);
headRow.createCell(8).setCellValue(“工单创键时间”);
headRow.createCell(9).setCellValue(“修改工单时间”);
for (Recieve recieve: list) {
//从第二行开始,去掉标题行
Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
//属地 通过code查找name值
String communityCode=recieve.getCommunityCode();
String communityName=communityMapper.findCommunityNameByCode(communityCode);
dataRow.createCell(0).setCellValue(communityName);
//来电人
dataRow.createCell(1).setCellValue(recieve.getPhoneMan());
//来电号码
dataRow.createCell(2).setCellValue(recieve.getPhoneNum());
//地址
dataRow.createCell(3).setCellValue(recieve.getAddress());
//问题分类
dataRow.createCell(4).setCellValue(recieve.getProblemName());
//工单描述
dataRow.createCell(5).setCellValue(recieve.getDescription());
//工单编号
dataRow.createCell(6).setCellValue(recieve.getRecieveNum());
//地图定位id
dataRow.createCell(7).setCellValue(recieve.getCaseObjectId());
//工单创建时间
dataRow.createCell(8).setCellValue(recieve.getCreateTime());
//工单修改时间
dataRow.createCell(9).setCellValue(recieve.getUpdateTime());
}
}
}

    try {
        //将数据写入
        workbook.write(outputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }

}
3,Mapper  分页
<!-- 查询列表 -->
<select id="selectRecieveList" parameterType="Map" resultMap="BaseResultMap">
    select
    <include refid="select_column_list" />
    from t_h_recieve t
    where t.isDelete = 0
    <if test="recieve.communityCode != null and recieve.communityCode != '' and recieve.communityCode !='null'">
        AND
        t.communityCode = #{recieve.communityCode}
    </if>
    <if test="recieve.recieveNum != null and recieve.recieveNum != ''">
        AND
        t.recieveNum  like  CONCAT(CONCAT('%',#{recieve.recieveNum},'%'))
    </if>
    <if test="recieve.phoneMan != null and recieve.phoneMan != ''">
        AND
        t.phoneMan like CONCAT(CONCAT('%',#{recieve.phoneMan},'%'))
    </if>
    <if test="recieve.phoneNum != null and recieve.phoneNum != ''">
        AND
        t.phoneNum like CONCAT(CONCAT('%',#{recieve.phoneNum},'%'))
    </if>
    <if test="recieve.problemName != null and recieve.problemName !=''">
        AND
        t.problemName=#{recieve.problemName}
    </if>
    <if test="recieve.monthCategory !=null and recieve.monthCategory !='' and recieve.monthCategory !='null' ">
        AND
        t.createTime like CONCAT(CONCAT(LEFT(t.createTime,5),#{recieve.monthCategory},'%'))
    </if>
    order by id desc
	<include refid="Oracle_Dialect_Suffix"></include>
</select>
4,展示效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值