java 实现csv文件导入导出

一.文件导入

  1. 前台页面
<a class="easyui-linkbutton" data-options="iconCls:'icon-undo'"
            onclick="importCsv()">导入</a>
function exportCsv() {
    
        window.location = ctx+"/system/exportCsv";  
    }
  1. controller代码
/**
     * 导出
     */
    @RequestMapping("exportCsv")
    @ResponseBody
    public void exportCsv(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        List<String> dataList = codeDictManagementService.exportCsv();
        response.setCharacterEncoding("UTF-8");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
        Date time = new Date();
        String tStamp = format.format(time);
        String filename = "Export" + tStamp + ".csv";
        response.setHeader("contentType", "text/html; charset=UTF-8");
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + filename);
        String cp = request.getSession().getServletContext().getRealPath("/");
        String path = cp + "download/" + filename;
        File file = new File(path);
        /*
         * if (!file.exists() || !file.isDirectory()) { file.mkdir(); }
         */
        BufferedInputStream bis = null;
        BufferedOutputStream out = null;
        FileWriterWithEncoding fwwe = new FileWriterWithEncoding(file, "UTF-8");
        BufferedWriter bw = new BufferedWriter(fwwe);
        if (dataList != null && !dataList.isEmpty())
        {
            for(String data : dataList)
            {
                bw.write(data);
                // 换行
                bw.write("\n");
            }
        }
        bw.close();
        fwwe.close();
        try
        {
            bis = new BufferedInputStream(new FileInputStream(file));
            out = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            while(true)
            {
                int bytesRead;
                if (-1 == (bytesRead = bis.read(buff, 0, buff.length)))
                {
                    break;
                }
                out.write(buff, 0, bytesRead);
            }
            file.deleteOnExit();
        }
        catch(IOException e)
        {
            throw e;
        }
        finally
        {
            try
            {
                if (bis != null)
                {
                    bis.close();
                }
                if (out != null)
                {
                    out.flush();
                    out.close();
                }
            }
            catch(IOException e)
            {
                
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中可以使用开源库 Apache Commons CSV实现 CSV导入导出。以下是一个简单的示例代码,演示如何在 Java 中使用 Apache Commons CSV导入导出 CSV 文件导入 CSV 文件示例代码: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; public class CSVReader { public static void main(String[] args) { String csvFile = "data.csv"; try (BufferedReader br = new BufferedReader(new FileReader(csvFile)); CSVParser parser = new CSVParser(br, CSVFormat.DEFAULT)) { for (CSVRecord record : parser) { String id = record.get(0); String name = record.get(1); String age = record.get(2); // 处理 CSV 数据 // ... } } catch (IOException e) { e.printStackTrace(); } } } ``` 导出 CSV 文件示例代码: ```java import java.io.FileWriter; import java.io.IOException; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; public class CSVWriter { public static void main(String[] args) { String csvFile = "data.csv"; try (FileWriter writer = new FileWriter(csvFile); CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT)) { printer.printRecord("1", "John", "Doe"); printer.printRecord("2", "Jane", "Doe"); printer.printRecord("3", "Bob", "Smith"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在实际的应用中,需要根据实际需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值