JAVA基于POI导入导出Excel

源码下载

https://download.csdn.net/download/qq_39706570/12524634

分析

1、本项目只集成了导入导出Excel功能,不需要配置相关文件;
2、工具类高内聚封装,没有其他多余组件的集成;
3、适用导出List>和List格式的数据;
4、导入导出只需要几行代码。

调用导出List案例

/**
 * @author SargerasWang
 */
package com.sargeraswang.util.ExcelUtil;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

/**
 * 以List>作为导出格式
 * 
 * @author SargerasWang
 * Created at 2014年9月21日 下午4:38:42
 */
public class TestExportMap {
  @Test
  public void exportXls() throws IOException {
    List> list = new ArrayList<>();
    Map map =new LinkedHashMap<>();
    map.put("name", "");
    map.put("age", "");
    map.put("createTime","");
    map.put("className","");
    Map map2 =new LinkedHashMap();
    map2.put("name", "测试是否是中文长度不能自动宽度.测试是否是中文长度不能自动宽度.");
    map2.put("age", null);
    map2.put("className", null);
    map.put("createTime",null);
    Map map3 =new LinkedHashMap();
    map3.put("name", "张三");
    map3.put("age", 12);
    map3.put("className", "1班");
    map3.put("createTime",new Date());
    list.add(map);
    list.add(map2);
    list.add(map3);
    Map map1 = new LinkedHashMap<>();
    map1.put("name","姓名");
    map1.put("age","年龄");
    map1.put("createTime","出生日期");
    map1.put("className","班级");
    String currentTimeMillis = System.currentTimeMillis()+"";
    File f= new File("C:\\Users\\Administrator\\Desktop\\"+currentTimeMillis+".xls");
    OutputStream out = new FileOutputStream(f);
    try {
    	 ExcelUtil.exportExcel(map1,list, out );
	} finally {
		 out.close();
	}
  }
}

调用导出List案例

package com.sargeraswang.util.ExcelUtil;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

public class TestExportBean {
    @Test
    public void exportXls() throws IOException {
        //用排序的Map且Map的键应与ExcelCell注解的index对应
        Map map = new LinkedHashMap<>();
        //这里注意与实体类的@ExcelCell中的index顺序对应。
        map.put("name","姓名");
        map.put("age","年龄");
        map.put("className","班级");
        map.put("createTime","出生日期");
        Collection dataset=new ArrayList();
        dataset.add(new Student("", "", "",null));
        dataset.add(new Student(null, null, null,null));
        dataset.add(new Student("王五", "34", "1班",new Date()));
        String currentTimeMillis = System.currentTimeMillis()+"";
        File f= new File("C:\\Users\\Administrator\\Desktop\\"+currentTimeMillis+".xls");
        OutputStream out=new FileOutputStream(f);
        try {
             ExcelUtil.exportExcel(map, dataset, out);
		} finally {
			 out.close();
		}
    }
}
 
  

调用导入案例

/**
 * @author SargerasWang
 */
package com.sargeraswang.util.ExcelUtil;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

/**
 * 测试导入Excel 97/2003
 */
public class TestImportExcel {

  @Test
  public void importXls() throws FileNotFoundException {
    File f=new File("C:\\Users\\Administrator\\Desktop\\a.xlsx");
    InputStream inputStream= new FileInputStream(f);
    ExcelLogs logs =new ExcelLogs();
    Collection importExcel ;
    try {
    	importExcel = ExcelUtil.importExcel(Map.class, inputStream, "yyyy/MM/dd HH:mm:ss", logs , 0);
	} finally {
		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    for(Map m : importExcel){
        System.out.println(m);
      }
  }
}

 

你可能感兴趣的:(JAVA基础)