代码

代码
导入的包

<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 &lt;= #{coor.coorXMax}
            and  n.COOR_X &gt;= #{coor.coorXMin}
            and  n.COOR_Y &lt;= #{coor.coorYMax}
            and  n.COOR_Y &gt;= #{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();

    }
    ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值