阅读背景:

JAVA 生成CSV文件并导出_猫七姑娘 °的博客_poi导出csv文件

来源:互联网 

service层:

/**
     * CSV文件生成方法
     * @param head 文件头
     * @param dataList 数据列表
     * @param outPutPath 文件输出路径
     * @param filename 文件名
     * @return
     */
public static File createCSVFile(List<Object> head, List<List<Object>> dataList, String outPutPath, String filename) {

        File csvFile = null;
        BufferedWriter csvWtriter = null;
        try {
            csvFile = new File(outPutPath + File.separator + filename + ".csv");
            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            csvFile.createNewFile();

            // GB2312使正确读取分隔符","
            csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                    csvFile), "GB2312"), 1024);
            // 写入文件头部
            writeRow(head, csvWtriter);

            // 写入文件内容
            for (List<Object> row : dataList) {
                writeRow(row, csvWtriter);
            }
            csvWtriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                csvWtriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return csvFile;
    }



 /**
     * 写一行数据方法
     * @param row
     * @param csvWriter
     * @throws IOException
     */
    private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
        // 写入文件头部
        for (Object data : row) {
            StringBuffer sb = new StringBuffer();
            String rowStr = sb.append("\"").append(data).append("\",").toString();
            csvWriter.write(rowStr);
        }
        csvWriter.newLine();
    }

   /**
     * 定义标题的list:此处你应该查询,然后把标题返回成list
     */
   public List<Object>  DefinitionTitle(){
        List<Object> titleList = new ArrayList<>();
        titleList.add("债券票编号");
        titleList.add("债券票名称");
        titleList.add("发行人代码");
        titleList.add("债券票种类");
        titleList.add("操作日期");
        titleList.add("操作时间");
        titleList.add("操作状态");
        return titleList;
    }

    /**
     * 数据的list
     */
    @Transactional
    public  List<List<Object>> dataList(){
        List<List<Object>> mapList = new ArrayList<>();
        //查询sql
        List<Bond> bySql = (List<Bond>) basedao.findByHql("from Bond");
        if(bySql !=null){
            for (int i = 0; i <bySql.size() ; i++) {
                List<Object> tempList = new ArrayList<>();
                tempList.add(bySql.get(i).getBondNo());
                tempList.add(bySql.get(i).getBondName());
                tempList.add(bySql.get(i).getIssueId());
                tempList.add(bySql.get(i).getBondType());
                tempList.add(bySql.get(i).getSystemDate());
                tempList.add(bySql.get(i).getSystemTime());
                tempList.add(bySql.get(i).getModifyType());
                mapList.add(tempList);
            }
        }
        return mapList;
    }

/**
     * CSV文件生成方法
     * 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: