excel导入导出的两种方式:csv和XLS

依赖

 <dependency>             <groupId>net.sourceforge.jexcelapi</groupId>             <artifactId>jxl</artifactId>             <version>2.6.12</version></dependency>

  

XLS

package export;import java.io.File;import java.io.IOException;import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.List;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class XLSUtil implements Serializable {    public static boolean createXls(String tableName,List<Object> head, List<List<Object>> dataList,            String outPutPath, String filename){        WritableWorkbook book = null;         try {             String filePath = outPutPath+filename+".xls";             XLSUtil.checkFile(filePath);             // 打开文件             book = Workbook.createWorkbook(new File(filePath));             WritableSheet sheet = book.createSheet(filename, );             ){                 ; i < head.size(); i++) {                     Label label = , (String) head.get(i));                     // 将定义好的单元格添加到工作表中                     sheet.addCell(label);                }             }             ){                 ; i < dataList.size(); i++) {                     List<Object> list = dataList.get(i);                     // 保存数字的单元格必须使用Number的完整包路径                     ; j < list.size(); j++) {                         Label label = ,  (String) list.get(j));                         sheet.addCell(label);                    }                }             }             // 写入数据并关闭文件             book.write();         } catch (Exception e) {             System.out.println(e);         }finally{             if(book!=null){                 try {                     book.close();                 } catch (Exception e) {                     e.printStackTrace();                 }             }         }        return true;    }    public static void checkFile(String filename) {        File file = new File(filename);        if(!file.getParentFile().exists()){            file.getParentFile().mkdirs();        }        if(!file.exists()){            try {                file.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }    }    public static void main(String[] args) {        String tableName = "学生";        List<Object> head = new ArrayList<Object>();        head.add("你好");        head.add("我好");        head.add("大家好");        List<List<Object>> dataList = new ArrayList<List<Object>>();        List<Object> head1 = new ArrayList<Object>();        head1.add(");        head1.add(");        head1.add(");        dataList.add(head1);        dataList.add(head1);        dataList.add(head1);        String filename = "我的文件";        String outPutPath = "F:/test/aqa/";        XLSUtil.createXls(tableName, head, dataList, outPutPath, filename);//        XLSUtil.xlsExport(tableName , head , dataList, outPutPath , filename );    }}

csv

package export;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Serializable;import java.util.ArrayList;import java.util.List;public class CSVUtil implements Serializable {      /**     * 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");            System.out.println(File.separator);            File parent = csvFile.getParentFile();            if (parent != null && !parent.exists()) {                parent.mkdirs();            }            csvFile.createNewFile();            csvWtriter =  new BufferedWriter(new OutputStreamWriter(new FileOutputStream( csvFile)));            // 写入文件头部            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();    }    public static void main(String[] args) {         List<Object> exportData = new ArrayList<Object>();         exportData.add("第一列");         exportData.add("第二列");         exportData.add("第三列");         List<List<Object>> datalist = new ArrayList<List<Object>>();         List<Object> data=new ArrayList<Object>();         data.add("111");         data.add("222");         data.add("333");         List<Object> data1=new ArrayList<Object>();         data1.add("444");         data1.add("555");         data1.add("666");         datalist.add(data);         datalist.add(data1);         String path = "d:/export/";         String fileName = "文件导出";         File file = CSVUtil.createCSVFile(exportData, datalist, path, fileName);         String fileName2 = file.getName();         System.out.println("文件名称:" + fileName2);    }}