【问题标题】:How to export CSV file in Struts2如何在 Struts2 中导出 CSV 文件
【发布时间】:2019-11-07 01:12:23
【问题描述】:

我正在导出 .xlsx 文件,现在我想导出 .csv 文件。

我试图为此找到解决方案,但我认为这些都不适合我的情况。

下面是我的代码

动作映射:

<action name="export" class="com.xxx.xxx.xxx.action.myAction" method="excel">
    <result type="excel">
        <param name="template">/xlsTemplate/excel_temaplate.xls</param>
        <param name="beans">gridModel</param>
        <param name="filenameKey">filename</param>
    </result>
</action>

行动:

public String excel() {
        ArrayList<MyVo> resultList = myService.myFunction();
        setGridModel(resultList);
        return SUCCESS;
    }catch(Exception e){
        return ERROR;
    }
}

这些效果很好,我正在尝试为网络导出 .csv 文件而不是 .xlsx 文件(不保存在本地),以便用户可以下载它。

我应该从哪里开始?

任何评论将不胜感激。谢谢。

【问题讨论】:

    标签: java struts2 export-to-csv


    【解决方案1】:

    这是一个操作 CSV 的 util 类:

    
    import java.io.IOException;
    import java.io.Writer;
    import java.util.List;
    
    public class CSVUtils {
    
        private static final char DEFAULT_SEPARATOR = ',';
    
        public static void writeLine(Writer w, List<String> values) throws IOException {
            writeLine(w, values, DEFAULT_SEPARATOR, ' ');
        }
    
        public static void writeLine(Writer w, List<String> values, char separators) throws IOException {
            writeLine(w, values, separators, ' ');
        }
    
        //https://tools.ietf.org/html/rfc4180
        private static String followCVSformat(String value) {
    
            String result = value;
            if (result.contains("\"")) {
                result = result.replace("\"", "\"\"");
            }
            return result;
    
        }
    
        public static void writeLine(Writer w, List<String> values, char separators, char customQuote) throws IOException {
    
            boolean first = true;
    
            //default customQuote is empty
    
            if (separators == ' ') {
                separators = DEFAULT_SEPARATOR;
            }
    
            StringBuilder sb = new StringBuilder();
            for (String value : values) {
                if (!first) {
                    sb.append(separators);
                }
                if (customQuote == ' ') {
                    sb.append(followCVSformat(value));
                } else {
                    sb.append(customQuote).append(followCVSformat(value)).append(customQuote);
                }
    
                first = false;
            }
            sb.append("\n");
            w.append(sb.toString());
    
    
        }
    
    }
    

    所以你可以在你的代码中使用它:

        public String excel() throws Exception{
            String csvFile = "/Users/mkyong/csv/abc.csv";
            FileWriter writer = new FileWriter(csvFile);
            ArrayList<MyVo> resultList = myService.myFunction();
            List<String> strings = resultList.stream()
           .map(obj -> obj.getYourAttr())
           .collect(Collectors.toList());
            CSVUtils.writeLine(writer, strings);
           writer.flush();
            writer.close();
    
            return SUCCESS;
            }catch(Exception e){
                return ERROR;
            }
        }
    
    

    【讨论】:

      猜你喜欢
      • 2019-09-27
      • 2012-08-27
      • 2017-10-24
      • 2021-08-22
      • 1970-01-01
      • 2011-12-26
      • 2023-03-23
      • 2022-11-14
      • 2018-02-15
      相关资源
      最近更新 更多