代码
导入的包
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.7</version>
</dependency>
Dao层
@Mapper
public interface NeInfoDao {
List<Map<String,Object>> getUserNeList(Map<String,Object> userInfo);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.dao.NeInfoDao">
<select id="getUserNeList" resultType="java.util.Map">
select * from ne_info n
where n.USERID = #{USERID}
AND n.COUNTRYCODE = #{COUNTRYCODE}
AND n.CITYCODE = #{CITYCODE}
AND n.OPERATORCODE = #{OPERATORCODE}
<if test="type=='view'">
and n.COOR_X <= #{coor.coorXMax}
and n.COOR_X >= #{coor.coorXMin}
and n.COOR_Y <= #{coor.coorYMax}
and n.COOR_Y >= #{coor.coorYMin}
</if>
</select>
</mapper>
@Autowired
NeInfoDao neInfoDao;
@Test
public void test() throws IOException {
HashMap<String, Object> userInfo = new HashMap<>();
userInfo.put("USERID","001");
userInfo.put("COUNTRYCODE","001");
userInfo.put("CITYCODE","002");
userInfo.put("OPERATORCODE","003");
userInfo.put("type","view");
HashMap<String, Double> coor = new HashMap<>();
coor.put("coorXMin",75.6);
coor.put("coorXMax",85.6);
coor.put("coorYMin",75.7);
coor.put("coorYMax",95.7);
userInfo.put("coor",coor);
List<Map<String, Object>> userNeList = neInfoDao.getUserNeList(userInfo);
File file = new File("e:/UserInfo.xlsx");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
XSSFWorkbook sheets = new XSSFWorkbook();
XSSFSheet sheet1 = sheets.createSheet("sheet1");
XSSFRow titleRow = sheet1.createRow(0);
titleRow.createCell(0).setCellValue("CountryCode");
titleRow.createCell(1).setCellValue("CityCode");
titleRow.createCell(2).setCellValue("OperatorCode");
titleRow.createCell(3).setCellValue("NeName");
titleRow.createCell(4).setCellValue("NeType");
titleRow.createCell(5).setCellValue("COOR_X");
titleRow.createCell(6).setCellValue("COOR_Y");
for (int i = 0; i < userNeList.size(); i++) {
int lastRowNumber = sheet1.getLastRowNum();
XSSFRow row = sheet1.createRow(lastRowNumber + 1);
row.createCell(0).setCellValue(userNeList.get(i).get("COUNTRYCODE").toString());
row.createCell(1).setCellValue(userNeList.get(i).get("CITYCODE").toString());
row.createCell(2).setCellValue(userNeList.get(i).get("OPERATORCODE").toString());
row.createCell(3).setCellValue(userNeList.get(i).get("NENAME").toString());
row.createCell(4).setCellValue(userNeList.get(i).get("NETYPE").toString());
row.createCell(5).setCellValue(userNeList.get(i).get("COOR_X").toString());
row.createCell(6).setCellValue(userNeList.get(i).get("COOR_Y").toString());
}
sheets.write(new FileOutputStream(file));
sheets.close();
}
@Test
public void exportCsvFile() throws IOException {
HashMap<String, Object> userInfo = new HashMap<>();
userInfo.put("USERID","001");
userInfo.put("COUNTRYCODE","001");
userInfo.put("CITYCODE","002");
userInfo.put("OPERATORCODE","003");
userInfo.put("type","view");
HashMap<String, Double> coor = new HashMap<>();
coor.put("coorXMin",75.6);
coor.put("coorXMax",85.6);
coor.put("coorYMin",75.7);
coor.put("coorYMax",95.7);
userInfo.put("coor",coor);
List<Map<String, Object>> userNeList = neInfoDao.getUserNeList(userInfo);
File file = new File("e:/UserInfo.csv");
// File file1 = File.createTempFile("UserInfo", ".csv");
CsvWriter csvWriter = new CsvWriter(file.getCanonicalPath(), ',', StandardCharsets.UTF_8);
String[] headers = {"CountryCode","CityCode","OperatorCode","NeName","NeType","COOR_X","COOR_Y"};
csvWriter.writeRecord(headers);
for (int i = 0; i < userNeList.size(); i++) {
csvWriter.write(userNeList.get(i).get("COUNTRYCODE").toString());
csvWriter.write(userNeList.get(i).get("CITYCODE").toString());
csvWriter.write(userNeList.get(i).get("OPERATORCODE").toString());
csvWriter.write(userNeList.get(i).get("NENAME").toString());
csvWriter.write(userNeList.get(i).get("NETYPE").toString());
csvWriter.write(userNeList.get(i).get("COOR_X").toString());
csvWriter.write(userNeList.get(i).get("COOR_Y").toString());
csvWriter.endRecord();
}
csvWriter.close();
}
private final static String NEW_LINE_SEPARATOR="\n";
@Test
public void apachCsvTest() throws IOException {
HashMap<String, Object> userInfo = new HashMap<>();
userInfo.put("USERID","001");
userInfo.put("COUNTRYCODE","001");
userInfo.put("CITYCODE","002");
userInfo.put("OPERATORCODE","003");
userInfo.put("type","view");
HashMap<String, Double> coor = new HashMap<>();
coor.put("coorXMin",75.6);
coor.put("coorXMax",85.6);
coor.put("coorYMin",75.7);
coor.put("coorYMax",95.7);
userInfo.put("coor",coor);
List<Map<String, Object>> userNeList = neInfoDao.getUserNeList(userInfo);
String[] headers = {"CountryCode","CityCode","OperatorCode","NeName","NeType","COOR_X","COOR_Y"};
CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
FileWriter fileWriter = new FileWriter("e:/UserInfo1.csv");
CSVPrinter csvPrinter = new CSVPrinter(fileWriter, csvFormat);
csvPrinter.printRecord(headers);
for (int i = 0; i < userNeList.size(); i++) {
Map<String, Object> Map = userNeList.get(i);
Object[] thisline ={Map.get("COUNTRYCODE"),
Map.get("CITYCODE"),
Map.get("OPERATORCODE"),
Map.get("NENAME"),
Map.get("NETYPE"),
Map.get("COOR_X"),
Map.get("COOR_Y")
};
csvPrinter.printRecord(thisline);
}
csvPrinter.close();
}
```