JAVAはPOIに基づいてExcelを導入します。

4359 ワード

ダウンロード
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);
      }
  }
}