java 中常用的POI
文档导入
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(myFile));
//读物表格文件中的第一个表
//首先你需要读取到一个文件mmFile
//创建一个HSSFWorkbook传入读取到的文件
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(myFile));
//根据index获取第一张表
HSSFSheet regionSheet = hssfWorkbook.getSheetAt(0);
//或者根据名字获取需要读取的那张表
// hssfWorkbook.getSheet("Sheet1");
List<Region> regionList = new ArrayList<>();
for (Row row : regionSheet) { // 遍历sheet表的每一行,
int rowNum = row.getRowNum();
if (rowNum == 0) { //过滤掉标题
continue;
}
// row表示一行,cell表示一行的每个单元格
String id = row.getCell(0).getStringCellValue();
String province = row.getCell(1).getStringCellValue();
String city = row.getCell(3).getStringCellValue();
String district = row.getCell(3).getStringCellValue();
String postcode = row.getCell(4).getStringCellValue();
//封装成需要封装的对象
Region region = new Region(id, province, city, district, postcode, null, null, null);
province = province.substring(0, province.length() - 1);
city = city.substring(0, city.length() - 1);
district = district.substring(0, district.length() - 1);
String info = province + city + district;
String[] headByString = PinYin4jUtils.getHeadByString(info);
String shortcode = StringUtils.join(headByString);
// 城市编码---->>shijiazhuang
String citycode = PinYin4jUtils.hanziToPinyin(city, "");
region.setShortcode(shortcode);
region.setCitycode(citycode);
regionList.add(region);
}
//保存读取到的数据
regionService.save(regionList);
将数据导出到excel文档
// 使用POI写入Excel数据到表格中
// 在内存中创建一个Excel文件
HSSFWorkbook workBook = new HSSFWorkbook();
// 表格中创建一个标签页面
HSSFSheet subAreaSheet = workBook.createSheet("分区数据");
// 初始化第一行的 名字
HSSFRow firstRow = subAreaSheet.createRow(0);
firstRow.createCell(0).setCellValue("分区编号");
firstRow.createCell(1).setCellValue("开始编号");
firstRow.createCell(2).setCellValue("结束编号");
firstRow.createCell(3).setCellValue("位置信息");
firstRow.createCell(4).setCellValue("省市区");
// 遍历数据库查询的信息,写到表格中
for (Subarea subarea : dataList) {
// 创建新的一行
HSSFRow newRow = subAreaSheet.createRow(subAreaSheet.getLastRowNum() +1);
// 写入数据
newRow.createCell(0).setCellValue(subarea.getId());
newRow.createCell(1).setCellValue(subarea.getStartnum());
newRow.createCell(2).setCellValue(subarea.getEndnum());
newRow.createCell(3).setCellValue(subarea.getPosition());
if (subarea.getRegion() != null) {
newRow.createCell(4).setCellValue(subarea.getRegion().getName());
}
}
//保存到本地,寫出
//保存文件的名字
String fileName = "分区数据.xls";
String contextType = ServletActionContext.getServletContext().getMimeType(fileName);
//获取输出流
ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
ServletActionContext.getResponse().setContentType(contextType);
//获取浏览器的类型
String agent = ServletActionContext.getRequest().getHeader("User-Agent");
//根据不同的浏览器设置名字
fileName = FileUtils.encodeDownloadFilename(fileName, agent);
ServletActionContext.getResponse().setHeader("disposition", "attachment;filename="+fileName);
workBook.write(out);
其中FileUtils是一个工具类,根据不同的浏览器类型转文件名字和后缀
public class FileUtils {
/**
* 下载文件时,针对不同浏览器,进行附件名的编码
*
* @param filename
* 下载文件名
* @param agent
* 客户端浏览器
* @return 编码后的下载附件名
* @throws IOException
*/
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
if (agent.contains("Firefox")) { // 火狐浏览器
filename = "=?UTF-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
} else { // IE及其他浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}
POI是Apache开源的库,需要依赖相应的jar包,具体操作详见 apache.org上查看相关的文档