1. 程式人生 > >根據表名匯出動態表到excel

根據表名匯出動態表到excel

首先新建一個類用於接收引數,由於欄位名和值不確定,所以

public class Pair<T, U> {
    private T first;
    private U second;

    public Pair(T first, U second) {
        this.first = first;
        this.second = second;
    }

    public T getFirst() {
        return first;
    }

    public void setFirst(T first) {
        
this.first = first; } public U getSecond() { return second; } public void setSecond(U second) { this.second = second; } }

由於前端傳入引數只有表名,所以需要先由表名得出各欄位名作為excel的第一行,再在for迴圈中根據欄位名得出欄位值逐行輸出到excel。

mapper.xml及mapper.java內內容

 <select id="getTableFields" parameterType="string" resultType="string">
        select column_name from information_schema.COLUMNS where table_name
=#{tableName} </select>
 <select id="getTableValues" resultType="map">
        select 
        <trim>
            <foreach collection="columnList" item="listItem" separator=",">
                 ${listItem}
            </foreach>
        </trim>
        from ${tableName}
    </select>

 

 
  
  
List<String> getTableFields(@Param("tableName") String tableName);

List<Map<String, ?>> getTableValues(@Param("tableName") String tableName,@Param("columnList") List<String> columnList);

 

 

service及serviceimpl內容

public Pair<List<String>, List<Map<String, ?>>> getTableFields(String tableName);

public List<Map<String, ?>> getTableValues(String tableName,List<String> columnList);
    private DataToExcelMapper dao;

    public Pair<List<String>, List<Map<String, ?>>> getTableFields(String tableName) {
        List<Map<String, ?>> dataList = null;
        List<String> columnList = dao.getTableFields(tableName);
        if (!CollectionUtils.isEmpty(columnList)) {
            dataList = dao.getTableValues(tableName, columnList);
        }
        return new Pair<List<String>, List<Map<String, ?>>>(columnList, dataList);
    }

    public List<Map<String, ?>> getTableValues(String tableName, List<String> columnList) {
        return dao.getTableValues(tableName, columnList);
    }

最後,action內內容

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String str = sdf.format(date);
        String filejg="手工表"; 
        if(tableName==null) {
            filejg="空表";
        }else {
            filejg=tableName; 
        }
        String fileName = filejg+ str + ".xls";
        response.setContentType("application/x.ms-excel");
        response.setHeader("Content-disposition",
                "attachment;filename=".concat(new String(fileName.getBytes("UTF-8"), "ISO-8859-1")));
        Pair<List<String>, List<Map<String, ?>>> pair = DTES.getTableFields(tableName);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("表");
        HSSFRow row = sheet.createRow((int) 0);
        if (!CollectionUtils.isEmpty(pair.getFirst())) {
            int i = 0;
            for (String columnName : pair.getFirst()) {
                HSSFCellStyle style = wb.createCellStyle();
                HSSFCell cell = row.createCell((short) i);
                cell.setCellValue(columnName);
                cell.setCellStyle(style);
                i++;
            }
        }

        if (!CollectionUtils.isEmpty(pair.getSecond())) {
            for (int i = 0, j = pair.getSecond().size(); i < j; i++) {
                Map<String, ?> valueMap = pair.getSecond().get(i);
                row = sheet.createRow(i + 1);

                for (int k = 0; k < pair.getFirst().size(); k++) {
                    Object value = valueMap.get(pair.getFirst().get(k));
                    row.createCell((short) k).setCellValue(value.toString() /** TODO translate values */
                    );
                }
            }
        }
        try {
            wb.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.getOutputStream().flush();

...作為java新手,暫時只會這樣匯出動態表。

待改進複用吧...