MyEyes

Logging my life

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

转载自http://www.cnblogs.com/luxh/archive/2012/07/09/2582942.html,感觉蛮有用

后缀为csv的文件可以直接用excell打开,但要满足一定的格式:

例如:在Excel中的格式:

                                   

那么,在csv文件中格式就必须为:

                                  

那么直接用流就可以用了:

package com.snow.open.excell;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FileOpenByExcell {

    private class Novel {
        String bookName;
        String bookAuthor;
        Date time;

        public String getBookName() {
            return bookName;
        }

        public void setBookName(String bookName) {
            this.bookName = bookName;
        }

        public String getBookAuthor() {
            return bookAuthor;
        }

        public void setBookAuthor(String bookAuthor) {
            this.bookAuthor = bookAuthor;
        }

        public Date getTime() {
            return time;
        }

        public void setTime(Date time) {
            this.time = time;
        }

        public Novel(String bookName, String bookAuthor, Date time) {
            super();
            this.bookName = bookName;
            this.bookAuthor = bookAuthor;
            this.time = time;
        }
    }

    private List<Novel> getNovels() {
        List<Novel> novels = new ArrayList<Novel>();
        Novel novel1 = new Novel("风云第一刀", "古龙", new Date());
        Novel novel2 = new Novel("书剑恩仇录", "金庸", new Date());
        Novel novel3 = new Novel("陆小凤传奇", "古龙", new Date());
        Novel novel4 = new Novel("鹿鼎记", "金庸", new Date());
        novels.add(novel1);
        novels.add(novel2);
        novels.add(novel3);
        novels.add(novel4);
        return novels;
    }

    private void writeData2CSV(ArrayList<Novel> novels, String fileName) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(fileName);
            // 输出标题头
            // 注意列之间用","间隔,写完一行需要回车换行"\r\n"
            String title = "序号,小说名称,作者,出版日期\r\n";
            fw.write(title);
            String content = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < novels.size(); i++) {
                Novel novel = novels.get(i);
                // 注意列之间用","间隔,写完一行需要回车换行"\r\n"
                content = (i + 1) + "," + novel.getBookName() + ","
                        + novel.getBookAuthor() + ","
                        + sdf.format(novel.getTime()) + "\r\n";
                fw.write(content);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String fileName = "D:/novels.csv";
        FileOpenByExcell result = new FileOpenByExcell();
        ArrayList<Novel> novels = (ArrayList<Novel>) result.getNovels();
        result.writeData2CSV(novels, fileName);

    }

}
posted on 2012-07-09 18:55  MyEyes  阅读(586)  评论(0编辑  收藏  举报