從數據庫導出數據到excel之List導出

說明:不少時候取出來的數據是封裝爲List<Map<String,Object>>,能夠直接導出excel表格html

項目說明就在 「上一篇java

直接上代碼(數據層和業務層不用說了,查出的代碼格式爲List<Map<String,Object>>便可):linux

控制層sql

List<Map<String,String>> list=expot.GetStudentTest(gender.getGender());
         System.out.println("listDate:"+list);         
        ExportExcelHSSF<List<Map<String,String>>> ee= new ExportExcelHSSF<>();
        ExportExcelOutputStream ee=new ExportExcelOutputStream();
        //String[] headers = {  "姓名", "性別", "體重","班級","網絡協議","javaEE","計算機基礎","Linux操做系統","網絡安全","sql數據庫","數據結構" };
        String[] headers = {  "姓名","體重","網絡協議","javaEE","計算機基礎","Linux操做系統","網絡安全","sql數據庫","數據結構" };
        String fileName = "信息表";
        ee.exportExcel(list, headers,fileName, response);

實體類:數據庫

public class ExportDateTest implements Serializable{
    private String name;
    //private String gender;//性別
    private String weight;
    //private String grades;//班級
    private Double Networkprotocol;
    private Double javaEE;
    private Double Computerbasis;
    private Double Linuxoperatingsystem;
    private Double networksecurity;
    private Double SQLdatabase;
    private Double datastructure;
    public ExportDateTest() {
    
        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    /*
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    */
    public String getWeight() {
        return weight;
    }
    public void setWeight(String weight) {
        this.weight = weight;
    }
    /*
    public String getGrades() {
        return grades;
    }
    public void setGrades(String grades) {
        this.grades = grades;
    }
    */
    public Double getNetworkprotocol() {
        return Networkprotocol;
    }
    public void setNetworkprotocol(Double networkprotocol) {
        Networkprotocol = networkprotocol;
    }
    public Double getJavaEE() {
        return javaEE;
    }
    public void setJavaEE(Double javaEE) {
        this.javaEE = javaEE;
    }
    public Double getComputerbasis() {
        return Computerbasis;
    }
    public void setComputerbasis(Double computerbasis) {
        Computerbasis = computerbasis;
    }
    public Double getLinuxoperatingsystem() {
        return Linuxoperatingsystem;
    }
    public void setLinuxoperatingsystem(Double linuxoperatingsystem) {
        Linuxoperatingsystem = linuxoperatingsystem;
    }
    public Double getNetworksecurity() {
        return networksecurity;
    }
    public void setNetworksecurity(Double networksecurity) {
        this.networksecurity = networksecurity;
    }
    public Double getSQLdatabase() {
        return SQLdatabase;
    }
    public void setSQLdatabase(Double sQLdatabase) {
        SQLdatabase = sQLdatabase;
    }
    public Double getDatastructure() {
        return datastructure;
    }
    public void setDatastructure(Double datastructure) {
        this.datastructure = datastructure;
    }
    public ExportDateTest(String name, String gender, String weight, String grades, Double networkprotocol, Double javaEE,
            Double computerbasis, Double linuxoperatingsystem, Double networksecurity, Double sQLdatabase,
            Double datastructure) {
        super();
        this.name = name;
        //this.gender = gender;
        this.weight = weight;
        //this.grades = grades;
        Networkprotocol = networkprotocol;
        this.javaEE = javaEE;
        Computerbasis = computerbasis;
        Linuxoperatingsystem = linuxoperatingsystem;
        this.networksecurity = networksecurity;
        SQLdatabase = sQLdatabase;
        this.datastructure = datastructure;
    }
    @Override
    public String toString() {
        return "ExportDate [name=" + name + ""
                //+ ", gender=" + gender + ""
                + ", weight=" + weight + ""
            //    + ", grades=" + grades
                + ", Networkprotocol=" + Networkprotocol + ", javaEE=" + javaEE + ", Computerbasis=" + Computerbasis
                + ", Linuxoperatingsystem=" + Linuxoperatingsystem + ", networksecurity=" + networksecurity
                + ", SQLdatabase=" + SQLdatabase + ", datastructure=" + datastructure + "]";
    }
    

    

}

導出代碼:緩存

/**
 * 將List<Map<>>數據格式導出到excel中
 * @author 開發者
 *
 */
public class ExportExcelOutputStreamTest {
     public String exportExcel(List<Map<String, String>> orderlist,String[] headerlist, 
             String name,HttpServletResponse response ) {  
            String result = "系統提示:Excel文件導出成功!";  
            // 如下開始輸出到EXCEL  
            try {  
                 //定義輸出流,以便打開保存對話框______________________begin  
                 OutputStream os = response.getOutputStream();// 取得輸出流  
                 response.reset();// 清空輸出流  
                   
                 String fileName = name + ".xls";  
                 response.setContentType("application/x-msdownload");// 設定輸出文件類型
                 response.setHeader("Content-Disposition",
                         "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" )); //設定文件輸出類型         
                 //定義輸出流,以便打開保存對話框_______________________end  
                /** **********建立工做簿************ */  
                WritableWorkbook workbook = Workbook.createWorkbook(os);        
                /** **********建立工做表************ */       
                WritableSheet sheet = workbook.createSheet("Sheet1", 0);        
                /** **********設置縱橫打印(默認爲縱打)、打印紙***************** */  
                SheetSettings sheetset = sheet.getSettings();  
                sheetset.setProtected(false);  
                sheetset.setDefaultColumnWidth(20);  
      
                /** ************設置單元格字體************** */  
                WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);  
                WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,  
                        WritableFont.BOLD);  
                WritableFont TitleFont = new WritableFont(WritableFont.ARIAL, 20,  
                        WritableFont.BOLD);  
      
                /** ************如下設置三種單元格樣式,靈活備用************ */  
                // 用於表名,要高端大氣!  
                WritableCellFormat title_center = new WritableCellFormat(TitleFont);  
                title_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條  
                title_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊  
                title_center.setAlignment(Alignment.CENTRE); // 文字水平對齊  
                title_center.setWrap(false); // 文字是否換行  
                  
                // 用於標題居中  
                WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);  
                wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條  
                wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊  
                wcf_center.setAlignment(Alignment.CENTRE); // 文字水平對齊  
                wcf_center.setWrap(false); // 文字是否換行  
      
                // 用於正文居左  
                WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);  
                wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 線條  
                wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊  
                wcf_left.setAlignment(Alignment.LEFT); // 文字水平對齊  
                wcf_left.setWrap(true); // 文字是否換行  
                  
      
       
                /** ***************如下是EXCEL第一行列標題********************* */  
                for (int i = 0; i < headerlist.length; i++) {  
                    sheet.addCell(new Label(i, 3, headerlist[i], wcf_center));  
                }  
                /** ***************如下是EXCEL正文數據********************* */  
                //這裏要分紅兩種狀況,第一種傳的是List<Map>,用Map的方式處理  
                //第二種傳的是List<Object>,用普通類取屬性的方式來處理;  
        
                Iterator iterator = orderlist.iterator();  
                Object o = iterator.next();                 
                    Map m = (Map) o;  
                    System.out.println("m:"+m);  
                    Iterator iter = m.entrySet().iterator();  
                    int first_j=0;  
                    while (iter.hasNext()) {                            
                        Map.Entry entry = (Map.Entry) iter.next();  
                        String[] key_value = entry.toString().split("=");  
                       
                        System.out.println(key_value[1]);  
                        
                        sheet.addCell(new Label(first_j, 4, key_value[1], wcf_left));  
                        first_j++;  
                    }  
                    int i=5;  
                    while(iterator.hasNext()){  
                        Map row_map = (Map) iterator.next();  
                        Iterator row_iterator = row_map.entrySet().iterator();  
                        int second_j=0;  
                        while(row_iterator.hasNext()){  
                            Map.Entry entry_column = (Map.Entry) row_iterator.next();  
                            String[] key_value = entry_column.toString().split("=");  
                            sheet.addCell(new Label(second_j, i, key_value[1], wcf_left));  
                            second_j++;  
                        }  
                        i++;  
                    }
                /** **********將以上緩存中的內容寫到EXCEL文件中******** */  
                workbook.write();  
                /** *********關閉文件************* */  
                workbook.close();  
                  
                System.out.println(result);  
      
            } catch (Exception e) {  
                result = "系統提示:Excel文件導出失敗,緣由:" + e.toString();  
                System.out.println(result);  
                e.printStackTrace();  
            }  
            return result;  
        }