Java-list形式客戶端保存為excel文件
package com.HeiBeiEDU.test2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

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

public class ListToExcel {

    /**
     * 
     * @param name
     *            Excel保存的主題名
     * @param data
     *            里邊有Map和List Map存放字段對應關系(ziDuan,字段的第一個字符是序號)
     *            List存放對象數據(listData)
     * @return [0]是fileName [1]是filePath
     */
    public static String[] objListToExcel(String name, Map data, String path) {
        Map<String, String> ziDuan = (Map<String, String>) data.get("ziDuan");
        List listData = (List) data.get("listData");
        Object[] keys = ziDuan.keySet().toArray();
        String[] ziDuanKeys = new String[keys.length];
        for (int k = 0; k < keys.length; k++) {
            String temp = keys[k].toString();
            int xuHao = Integer.valueOf(temp.substring(0, 1));
            ziDuanKeys[xuHao] = temp.substring(1);
        }
        try {
            File newFile = new File(path);

            if (newFile.exists()) {
                int ii = JOptionPane.showConfirmDialog(null, "文件已存在,你確定要覆蓋嗎?", "文件已存在", JOptionPane.YES_NO_OPTION);
                if (ii == 0) {// 確定
                    newFile.createNewFile();
                    System.out.println("創建文件成功:" + path);
                    HSSFWorkbook wb = new HSSFWorkbook();
                    HSSFSheet sheet = wb.createSheet();

                    for (int i = 0; i < listData.size(); i++) {
                        HSSFRow row = sheet.createRow(i);
                        Object obj = listData.get(i);
                        for (int j = 0; j < ziDuanKeys.length; j++) {
                            HSSFCell cell = row.createCell(j);
                            if (i == 0) {
                                sheet.setColumnWidth(j, 6000);
                                cell.setCellValue(new HSSFRichTextString(ziDuan.get(j + ziDuanKeys[j])));
                            } else {
                                String ziDuanName = (String) ziDuanKeys[j];
                                System.out.println(ziDuanName);
                                ziDuanName = ziDuanName.replaceFirst(ziDuanName.substring(0, 1),
                                        ziDuanName.substring(0, 1).toUpperCase());
                                ziDuanName = "get" + ziDuanName;
                                Class clazz = Class.forName(obj.getClass().getName());
                                Method[] methods = clazz.getMethods();
                                Pattern pattern = Pattern.compile(ziDuanName);
                                Matcher mat = null;
                                for (Method m : methods) {
                                    mat = pattern.matcher(m.getName());
                                    if (mat.find()) {
                                        Object shuXing = m.invoke(obj, null);
                                        if (shuXing != null) {
                                            cell.setCellValue(shuXing.toString());// 這里可以做數據格式處理
                                        } else {
                                            cell.setCellValue("");
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    OutputStream out = new FileOutputStream(path);
                    wb.write(out);// 寫入File
                    out.flush();
                    out.close();
                }
            } else {
                newFile.createNewFile();
                System.out.println("創建文件成功:" + path);
                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet();

                for (int i = 0; i < listData.size(); i++) {
                    HSSFRow row = sheet.createRow(i);
                    Object obj = listData.get(i);
                    for (int j = 0; j < ziDuanKeys.length; j++) {
                        HSSFCell cell = row.createCell(j);
                        if (i == 0) {
                            sheet.setColumnWidth(j, 6000);
                            cell.setCellValue(new HSSFRichTextString(ziDuan.get(j + ziDuanKeys[j])));
                        } else {
                            String ziDuanName = (String) ziDuanKeys[j];
                            System.out.println(ziDuanName);
                            ziDuanName = ziDuanName.replaceFirst(ziDuanName.substring(0, 1),
                                    ziDuanName.substring(0, 1).toUpperCase());
                            ziDuanName = "get" + ziDuanName;
                            Class clazz = Class.forName(obj.getClass().getName());
                            Method[] methods = clazz.getMethods();
                            Pattern pattern = Pattern.compile(ziDuanName);
                            Matcher mat = null;
                            for (Method m : methods) {
                                mat = pattern.matcher(m.getName());
                                if (mat.find()) {
                                    Object shuXing = m.invoke(obj, null);
                                    if (shuXing != null) {
                                        cell.setCellValue(shuXing.toString());// 這里可以做數據格式處理
                                    } else {
                                        cell.setCellValue("");
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                OutputStream out = new FileOutputStream(path);
                wb.write(out);// 寫入File
                out.flush();
                out.close();
            }

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

               測試代碼為:

try {
                            List listData = new ArrayList();   //---------此處應遍歷真正的list,但是現在沒有辦法測試webservice,所以先寫測試代碼-----------------------------
                            for (int i = 0; i < 6; i++) {
                                CheckPerson person = new CheckPerson();
                                person.setDHHM("145647583486");
                                person.setGMSFHM("13546876874786643");
                                person.setMZ("MZ");
                                person.setXM("XM");
                                person.setXZ("XZ");
                                
                                listData.add(person);
                            }
                            
                            Map<String, String> ziDuan = new HashMap<String, String>();
                            ziDuan.put("0DHHM", "電話號碼");//屬性前邊的數字代表字段的先后順序。
                            ziDuan.put("1MZ", "暫定");//最好將源碼中判別順序的格式改為"序號-字段"。
                            ziDuan.put("2XM", "姓名");
                            ziDuan.put("3XZ", "住址");
                            Map data = new HashMap();
                            data.put("listData", listData);
                            data.put("ziDuan", ziDuan);
                            ListToExcel.objListToExcel("爆破作業人員查詢結果名單", data,path);
                     }catch(Exception e3){
                         e3.printStackTrace();
                     }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM