Rely on the Hutool toolkit

public static void main(String[] args) {
        GeneralXML generalXML = new GeneralXML();
        String resource = GeneralXML.class.getResource("/opcTemplate.xml").getPath();
        String parent = new File(resource).getParent() + "/opcConfig";
        System.out.println(resource);
        System.out.println(System.lineSeparator());
        String s = GeneralXML.readFileByer(resource);
        MessageFormat mf = new MessageFormat(s);
        StringBuffer sb = new StringBuffer();
        sb.append(generalXML.head);
        sb.append(generalXML.root_start);
        sb.append(mf.format(new Object[]{""."".""."".""."".""."/opcConfig/testXml.xml"}));
        sb.append(generalXML.root_end);
        System.out.println(sb.toString());
        String path = "/Users/lihongxing/Downloads/";
        File file = new File(parent);
        if (file.exists()) {
            file.mkdirs();
        }
        // Specify the path and encoding
        Document document = XmlUtil.parseXml(sb.toString());
        XmlUtil.toFile(document, parent + "/testXml.xml");
        CsvWriter writer = CsvUtil.getWriter(parent + "/testWrite.csv", CharsetUtil.CHARSET_UTF_8);
        Field[] declaredFields = PointInfoExportCSV.class.getDeclaredFields();
        List<String> collect = Arrays.stream(declaredFields).sequential().map(fi -> fi.getName()).collect(Collectors.toList());
        String[] strings = collect.toArray(new String[collect.size()]);
        List<Object> list = new ArrayList<>();
        list.add(strings);
        list.add(new String[]{"a2"."b2"."c2"});
        list.add(new String[]{"a3"."b3"."c4"});
        writer.write(
                list
        );

    }
Copy the code
public class GeneralXML {
    public final String head = "
      \n";
    public final String root_start = "<config>\n";
    public final String root_end = "\n</config>";

    public static String readFileByer(String filePath) {
        StringBuffer sb = new StringBuffer();
        File file = new File(filePath);
        FileReader fr = null;

        try {
            fr = new FileReader(file);
        } catch (FileNotFoundException var16) {
            var16.printStackTrace();
        }

        BufferedReader br = new BufferedReader(fr);
        String strLine = "";

        try {
            while((strLine = br.readLine()) ! =null) {
                sb.append(strLine);
                sb.append("\n"); }}catch (IOException var17) {
            var17.printStackTrace();
        } finally {
            try {
                fr.close();
                br.close();
            } catch(IOException var15) { var15.printStackTrace(); }}returnsb.toString(); }}Copy the code

These are some of the dependencies and methods used to test the process. Here are the formal business functions to write;

@requestMapping (name = "export protocol details template data ", value = "/v1/exportTempExcel", method = requestmethod.get)
    public JsonResult exportTempExcel(ExportXmlAndCsvDTO dto, HttpServletResponse response) throws UnsupportedEncodingException {
        / / random number
        String random = RandomUtil.randomNumbers(6);
        String separetor = File.separator;
        String resource = GeneralXML.class.getResource(separetor + "opcTemplate.xml").getPath();
        String parent = new File(resource).getParent() + separetor + "opcConfig" + separetor;
        String xmlPath = parent + random + "opc.xml";
        String csvPath = parent + "pointFile" + separetor + random + ".csv";
        List<PointInfoExportCSV> pointinfoList = clagreementInfoService.getPointByParentId(dto);
        // Start assembling the XML data
        GeneralXML generalXML = new GeneralXML();
        String s = GeneralXML.readFileByer(resource);
        MessageFormat mf = new MessageFormat(s);
        StringBuffer sb = new StringBuffer();
        sb.append(generalXML.head);
        sb.append(generalXML.root_start);
        sb.append(mf.format(new Object[]{"".""."".""."".""."", separetor + "opcConfig" + separetor + "pointFile" + separetor + random + ".csv"}));
        sb.append(generalXML.root_end);
        Document document = XmlUtil.parseXml(sb.toString());
        XmlUtil.toFile(document, xmlPath);
        // The XML assembly is complete
        // Start assembling the CSV data
        CsvWriter writer = CsvUtil.getWriter(csvPath, CharsetUtil.CHARSET_UTF_8);
        Field[] declaredFields = PointInfoExportCSV.class.getDeclaredFields();
        // Get the field name
        List<String> fieldName = Arrays.stream(declaredFields).sequential().map(fi -> fi.getName()).collect(Collectors.toList());
        // Convert data to map type for assembly data use
// List sourMap = pointinfoList.stream().map(po -> JSONUtil.toBean(JSONUtil.toJsonStr(po), Map.class)).collect(Collectors.toList());
        List<String[]> sourceList = pointinfoList.stream().map(po -> po.toString().split(",")).collect(Collectors.toList());
        List<Object> resultList = new ArrayList<>();
        // Add the first behavior title;
        resultList.add(fieldName);
        // Add data to list
        resultList.addAll(sourceList);
        writer.write(resultList);
        // The CSV assembly is complete
        File zip = ZipUtil.zip(parent);
        // Write to the browser
        downloadFile(response, zip);
        / / delete the zip
        FileUtil.del(zip);
        // Delete path files
        FileUtil.del(parent);
        return success();
    }
Copy the code
 /*** * for browser download file *@param response
     * @param zip
     * @throws UnsupportedEncodingException
     */
    private void downloadFile(HttpServletResponse response, File zip) throws UnsupportedEncodingException {
        response.setHeader("Content-Disposition"."attachment; filename=" + URLEncoder.encode(zip.getName(), "UTF-8"));
        response.setCharacterEncoding("UTF-8");
        BufferedInputStream bis = FileUtil.getInputStream(zip);
        byte[] buffer = new byte[1024];
        try (OutputStream os = response.getOutputStream()) {
            int i = bis.read(buffer);
            while(i ! = -1) {
                os.write(buffer, 0, i); i = bis.read(buffer); }}catch(IOException e) { e.printStackTrace(); }}Copy the code