package com.yitian.ssm.contorllers;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.synth.Region;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class BcRegionCon {
@Autowired
private BcRegionService rs;
@Autowired
private BcRegion b;
@Autowired
private BcRegionExample re;
/*
* excel导入
* shortcode 简码 :HBSJZQX
*
*/
@RequestMapping("/regionImportXls.action")
public void importXls(MultipartFile file) throws IOException{//参数是文件上传MultipartFile
InputStream in=file.getInputStream();//输入流 自动拿到file文件的输入流
HSSFWorkbook workbook=new HSSFWorkbook(in);//创建一个excel表格将输入流里的文件放入
HSSFSheet sheet=workbook.getSheet("sheet1");//得到表格的sheet1页
for (Row row : sheet) {//得到页之后遍历出每一行
if(row.getRowNum()==0){//如果行号是0的话直接跳过,为了是避免把ecsel里第一行的表字段给加入数据库中
continue;
}
/*System.out.println(cell.getStringCellValue());*/
String id=row.getCell(0).getStringCellValue();//把每行里的每个单元格里的对象一个个取出来付给一个属性
String province=row.getCell(1).getStringCellValue();//把每行里的每个单元格里的对象一个个取出来付给一个属性
String city=row.getCell(2).getStringCellValue();//把每行里的每个单元格里的对象一个个取出来付给一个属性
String district=row.getCell(3).getStringCellValue();//把每行里的每个单元格里的对象一个个取出来付给一个属性
String postcode=row.getCell(4).getStringCellValue();//把每行里的每个单元格里的对象一个个取出来付给一个属性
BcRegion record=new BcRegion(id,province,city,district,postcode,null,null);//将属性封装到有参构造中
//简码操作
province=province.substring(0,province.length()-1);//取省份然后截串从0开始,到省份的长度-1,把最后一位截掉
city=city.substring(0,city.length()-1);//和省同理
district=district.substring(0,district.length()-1);//和省同理
//将截好的省市区拼接在一起付给info
String info=province+city+district;
//取info的所有文字的首字母
//想要文字转拼音需要在Util中加入 pinyin4j文件 并且在父类pom里导入拼音4j依赖 ,如下
/* <dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
*/
String[] headBys= PinYin4jUtils.getHeadByString(info);//getHeadByString通过字符串截取info里每个文字的首字母,得到字符串数组
String shortcode = StringUtils.join(headBys); //将得到的字母数组进行拼接shortcode
record.setShortcode(shortcode);//将shortcode 设置到数据库中简码字段
//简码完成
//城市编码
String cityCode = PinYin4jUtils.hanziToPinyin(city,"");//将市转换为拼音赋值给cityCode
record.setCitycode(cityCode);//将cityCode设置到数据库中的城市编码字段中
rs.saveOrUpdate(record);//将带参数值(record)放入导入的增加方法中OK
}
}
/*
* Excel 导出
//@RequestHeader就是为了给文件取名 ("User-Agent") String agent
* 想要知道用户使用的什么浏览器,需要在Util里导入FileUtrls.java文件
*/
@ResponseBody
@RequestMapping("/exportXls.action")
public void exportXls(@RequestHeader ("User-Agent") String agent ,HttpServletResponse response ) throws IOException{
//查询出所有数据库中数据
List<BcRegion> list=rs.selectByExample();
//创建输出流
ServletOutputStream out=response.getOutputStream();
//在内存中创建Excel表
HSSFWorkbook workbook=new HSSFWorkbook();
//在表里创件一个页
HSSFSheet sheet=workbook.createSheet();
//根据页创建出一个行 创建0个行
HSSFRow 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("城市编码");
//将数据库里的所有值遍历出来
for (BcRegion region :list) {
//创建最后一行,如果list还有值最后一行就自动加1 getLastRowNum()最后一行
HSSFRow row=sheet.createRow(sheet.getLastRowNum()+1);
String id=region.getId();
String province=region.getProvince();
String city=region.getCity();
String district=region.getDistrict();
String postcode=region.getPostcode();
String shortcode=region.getShortcode();
String citycode=region.getCitycode();
//给表格里的数据附上值
row.createCell(0).setCellValue(id);
row.createCell(1).setCellValue(province);
row.createCell(2).setCellValue(city);
row.createCell(3).setCellValue(district);
row.createCell(4).setCellValue(postcode);
row.createCell(5).setCellValue(shortcode);
row.createCell(6).setCellValue(citycode);
}
//设置导出的文件格式,需要什么文件格式,直接在tomcat文件夹里conf下找到web-xml里找到类似下面的文件复制application/vnd.ms-excel然后粘贴在代码里
/* <mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>*/
//
//用file文件把分区进行转码一下
String filename=FileUtils.encodeDownloadFilename("分区", agent);
response.setContentType("application/vnd.ms-excel");//设置文件格式
//内容(content-disposition), 文件名(attachment;filename)
response.setHeader("content-disposition","attachment;filename="+filename+".xls");// 告诉它想应一个内容 文件名叫什么
workbook.write(out);
}
}
Excel导入导出
最新推荐文章于 2023-08-01 16:40:22 发布