qq_34472071 2016-04-20 08:27 采纳率: 0%
浏览 2866

csv文件java的导入与导出

如何把csv文件的数据从一个文件导入另一个文件,java实现?

  • 写回答

1条回答

  • xiaoyao880609 2016-04-20 08:40
    关注

    导入:
    BufferedReader br = null;
    String line = "";
    try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(fullFilePath), "EUC-KR"));
    Integer index = 0;
    while ((line = br.readLine()) != null) {
    index++;
    if (index == 1) {
    continue;
    }
    String[] lines = line.split(",");
    XXX xxx = new XXX();
    xxx.setXxx(lines[0]);
    xxx.setXxx(lines[1]);
    xxx.setXxx(lines[2]);
    }
    } catch (UnsupportedEncodingException e1) {
    log.error("UnsupportedEncodingException" + e1.getMessage());
    e1.printStackTrace();
    } catch (FileNotFoundException e1) {
    log.error("FileNotFoundException" + e1.getMessage());
    } catch (IOException e) {
    log.error("IOException" + e.getMessage());
    } finally {
    if (br != null) {
    try {
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    导出:
    List list = (List) model
    .get("list");
    //org.springframework.context.support.MessageSourceAccessor
    XSSFSheet sheet = workbook.createSheet("fileName");
    setText(getCell(sheet, 0, 1), "title1");
    setText(getCell(sheet, 0, 2), "title2");
    setText(getCell(sheet, 0, 3), "title3");
    setText(getCell(sheet, 0, 4), "title4");
    setText(getCell(sheet, 0, 5), "title5");
    // set date for excel
    int i = 1;
    for (Iterator it = list.iterator(); it.hasNext(); ) {
    Object obj = it.next();
    setText(getCell(sheet, i, 0), obj.getXxx());
    setText(getCell(sheet, i, 1), obj.getXxx());
    setText(getCell(sheet, i, 2), obj.getXxx());
    setText(getCell(sheet, i, 3), obj.getXxx());
    setText(getCell(sheet, i, 4), obj.getXxx());
    setText(getCell(sheet, i, 5), obj.getXxx());
    i++;
    }
    手敲的 有帮助的话请采纳~

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮