1. 程式人生 > >java使用poi生成excel

java使用poi生成excel

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import 
java.io.OutputStream; import java.util.*;
public class ExcelUtils {

    public static void exportExcel(List<Map<String, List<Map<String, String>>>> list, OutputStream out) throws IOException {

        /*
         * list{map("學生1"=list{map("name"="xiaoming","age"="24"),
         * map("name"="xiaoming","age"="24")}),
         * map("學生2"=list{map("name"="xiaoming","age"="24"),
         * map("name"="xiaoming","age"="24")})}
         */

        //建立workbook
        HSSFWorkbook workbook = new HSSFWorkbook();

        //遍歷集合,獲取sheet頁名
        for (int i = 0; i < list.size(); i++) {
            Set<String> sheetNames = list.get(i).keySet();
            String sheetName = null;
            for (String string : sheetNames) {
                sheetName = string;
            }

            //根據集合元素個數生成對應sheet頁
            HSSFSheet sheet = workbook.createSheet();
            workbook.setSheetName(i, sheetName);

            //格局sheet頁名獲取內層的集合
            List<Map<String, String>> tableList = list.get(i).get(sheetName);
            //內層集合中的map的keyset
            Set<String> headNames = tableList.get(0).keySet();
            HSSFSheet sheetInput = workbook.getSheetAt(i);
            HSSFRow headRow = sheetInput.createRow(0);
            //遍歷keyset填寫表頭
            for (int j = 0; j < headNames.size(); j++) {
                HSSFCell cell = headRow.createCell(j);
                cell.setCellValue((String) headNames.toArray()[j]);

            }

            //填寫標的內容
            for (int k = 0; k < tableList.size(); k++) {
                HSSFRow bodyRow = sheetInput.createRow(k + 1);
                for (int j = 0; j < headNames.size(); j++) {
                    HSSFCell cell = bodyRow.createCell(j);
                    cell.setCellValue(tableList.get(k).get((String) headNames.toArray()[j]));

                }
            }
        }
        workbook.write(out);
    }

    //測試main方法
    public static void main(String[] args) {

        /*
         * list{map("高等級治理"=list{map("name"="xiaoming","age"="24"),map("name"="xiaoming","age"="24")}),
         * map("中等級治理"=list{map("name"="xiaoming","age"="24"), map("name"="xiaoming","age"="24")})}
         */
        List<Map<String, List<Map<String, String>>>> oList = new ArrayList<>();
        List<Map<String, String>> iList = new ArrayList<>();
        Map<String, List<Map<String, String>>> oMap1 = new HashMap<>();
        Map<String, List<Map<String, String>>> oMap2 = new HashMap<>();
        Map<String, String> iMap1 = new HashMap<>();
        Map<String, String> iMap2 = new HashMap<>();
        iMap1.put("name", "xiaoming");
        iMap1.put("age", "24");
        iMap2.put("name", "小紅");
        iMap2.put("age", "25");
        iList.add(iMap1);
        iList.add(iMap2);
        oMap1.put("中等級治理", iList);
        oMap2.put("高等級治理", iList);
        oList.add(oMap1);
        oList.add(oMap2);

        OutputStream out = null;
        try {
            out = new FileOutputStream("D://a.xls");

            exportExcel(oList, out);
            JOptionPane.showMessageDialog(null, "匯出成功!");
            System.out.println("excel匯出成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}