jfinal framework to achieve export database

maven

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

 Front-end code

<p Alignment="left"><a href="/admin/customer/export">导出数据</a>

  Tools

package com.jfinal.club._admin.customer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

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 com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class CustomerExcelExportUtil {

    private static final String FILEPATH = PathKit.getWebRootPath() + File.separator + "upload" + File.separator ;

    public static String getTitle(){
        Date date = new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String title=FILEPATH+dateFormat.format(date)+"_统计用户表.xls";
        return title;
    }


    public  static File the saveFile (the Map <String, String> headData, String SQL, File File) {
         // create a workbook 
        HSSFWorkbook HSSFWorkbook = new new HSSFWorkbook ();
         // Sheet: an abbreviated table
         // Row: table row
         // create a worksheet in the workbook 
        HSSFSheet HSSFSheet = hssfWorkbook.createSheet ();
         // create a row 
        HSSFRow hssfSheet.createRow row = (0 );
         // create a cell, to create a column header disposed 
        HSSFCell cell = null ;
         // initialization index 
        int rowIndex = 0 ;
         int= 0 the cellIndex ; 

        // Create a header row 
        Row = hssfSheet.createRow (rowIndex); 
        rowIndex ++ ;
         // traverse the title 
        for (String H: headData.keySet ()) {
             // Create a row 
            the Cell = row.createCell (the cellIndex) ;
             // index increments 
            the cellIndex ++ ;
             // by column insert titles 
            cell.setCellValue (headData.get (H)); 
        } 

        // get all the rows: column 
        List <record> list = Db.find ( "select * from customer" ); 
        the Record Record = null; 

        IF (! List = null ) {
             // Get all the records on the number of records created how many rows 
            for ( int i = 0; i <list.size (); i ++ ) { 
                Row = hssfSheet.createRow (rowIndex);
                 // get all the lines a record on behalf of his party 
                record = list.get (i);
                 // the next line index 
                rowIndex ++ ;
                 // refresh new line index 
                the cellIndex 0 = ;
                 // in there all the records on the basis of convenient transfer come into the header, and then creates N lines 
                for (String H: headData.keySet ()) { 
                    Cell= row.createCell(cellIndex);
                    cellIndex++;
                    //按照每条记录匹配数据
                    cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
                }
            }
        }
        try {
            FileOutputStream fileOutputStreane = new FileOutputStream(file);
            hssfWorkbook.write(fileOutputStreane);
            fileOutputStreane.flush();
            fileOutputStreane.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}

controller

public  void Export () { 
        String SQL = "SELECT * from` customer` " ; 
        the Map <String, String> = titleData new new the HashMap <String, String> (); // header followed used 
        titleData.put (" uid " "customer ID" ); 
        titleData.put ( "pid", "parent ID" ); 
        titleData.put ( "loginid", "login" ); 
        titleData.put ( "tel", "phone number" ); 
        titleData .put ( "level", "customer level" ); 
        titleData.put ( "status", "account status" );
        titleData.put ( "realname", "real name" );
        titleData.put ( "Merchant", "whether the business" ); 
        titleData.put ( "USDT", "USDT" ); 
        titleData.put ( "Generation", "Algebra" ); 
        titleData.put ( "path", " path " ); 
        titleData.put ( " Balance "," margin " ); 
        titleData.put ( " NUM "," The number of direct push " ); 
        titleData.put ( " nums "," number of teams " ); 
        titleData.put ( "resumption", "whether the re-vote" ); 
        titleData.put ( "cdeal", "may reward OTC transactions.");
        titleData.put("ntasks", "总任务数");
        titleData.put("extasks", "task status" ); 
        titleData.put ( "cassets", "the chain assets" ); 
        titleData.put ( "Reson", "Lock amount" ); 
        titleData.put ( "djprice", "freeze amount " ); 
        titleData.put ( " zgkc "," stock up " ); 
        titleData.put ( " XZC "," new user registration " ); 
        titleData.put ( " mrtz "," daily total investment " ); 
        titleData .put ( "CREATED_TIME", "creation time" ); 
        File File = new new File (CustomerExcelExportUtil.getTitle ());
        file = CustomerExcelExportUtil.saveFile(titleData, sql, file);
        this.renderFile(file);
    }

Modify tools like sql file into the database table you want to export it

Guess you like

Origin www.cnblogs.com/fanxu3/p/11484299.html