日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jfinal框架批量导出数据到Excel

發布時間:2024/10/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jfinal框架批量导出数据到Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.配置controller

public void export(){
?? ??? ?Map<String, String[]> searchMap = new HashMap<String,String[]>(getParaMap());
?? ??? ?String sql = "select b.name,b.sex,b.card_number,b.address,b.lxdh,b.check_flag,b.sjly,d.value jflx,c.name jfdd,o.je,o.jfnf,o.lrr,o.create_time,e.value yhlx from zklt_sjjl o,zklt_id_card_info b,zklt_area c,sys_dct d,sys_dct e,sys_user f where o.sfzh=b.card_number and o.jfdd=c.id and o.jflx=d.key and b.yhlx=e.key and o.creater=f.id and d.group_id='82088c59e3584c27830c8a831d355150' and e.group_id='93bfd0284e0e47ddaf4f08f429b691d1' and f.status=1 and o.yxbs='1' and b.yxbs='1' and c.yxbs='1' ";
?? ??? ?String[] colCode =new String[] {"name","sex","card_number","address","jflx","jfdd","je","lrr","create_time","yhlx","lxdh","sjly"};
?? ??? ?String[] colName = new String[] {"姓名","性別", "身份證號", "地址", "繳費類型", "繳費地點", "金額", "填報人", "填報日期", "備注","聯系電話","數據來源","說明"};
? ? ? ? File file = new File(ExcelExportUtil.getTitle());
?? ? ? ?file = ExcelExportUtil.saveFile(colCode,colName, sql, file);
?? ? ? ?renderFile(file);
?? ?}

2.controller中使用的工具類:

?

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.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.IndexedColors;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class ExcelExportUtil {
?? ? 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 saveFile(String[] colCode,String[] codName, String sql, File file) {
?? ? ? ? ? ?// 創建工作薄
?? ? ? ? ? ?HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
?? ? ? ? ? ?// sheet:一張表的簡稱
?? ? ? ? ? ?// row:表里的行
?? ? ? ? ? ?// 創建工作薄中的工作表
?? ? ? ? ? ?HSSFSheet hssfSheet = hssfWorkbook.createSheet();
?? ? ? ? ? ?// 創建行
?? ? ? ? ? ?HSSFRow row = hssfSheet.createRow(0);
?? ? ? ? ? ?// 創建單元格,設置表頭 創建列
?? ? ? ? ? ?HSSFCell cell = null;
?? ? ? ? ? ?// 初始化索引
?? ? ? ? ? ?int rowIndex = 0;
?? ? ? ? ? ?int cellIndex = 0;
?? ? ? ? ? ?// 創建標題行
?? ? ? ? ? ?row = hssfSheet.createRow(rowIndex);
?? ? ? ? ? ?rowIndex++;
?? ? ? ? ? ?HSSFCellStyle cellStyle1 = hssfWorkbook.createCellStyle();
?? ? ? ? ? ?cellStyle1.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
?? ? ? ? ? ?cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
?? ? ? ? ? ?cellStyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
?? ? ? ? ? ?cellStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
?? ? ? ? ? ?cellStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
?? ? ? ? ? ?cellStyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
?? ? ? ? ? ?cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
?? ? ? ? ? ?HSSFFont font = hssfWorkbook.createFont();
?? ? ? ? ? ?font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
?? ? ? ? ? ?cellStyle1.setFont(font);
?? ? ? ? ? ?// 遍歷標題
?? ? ? ? ? ?for (String h : codName) {
?? ? ? ? ? ? ? ?//創建列
?? ? ? ? ? ? ? ?cell = row.createCell(cellIndex);
?? ? ? ? ? ? ? ?//索引遞增
?? ? ? ? ? ? ? ?cellIndex++;
?? ? ? ? ? ? ? ?//逐列插入標題
?? ? ? ? ? ? ? ?cell.setCellValue(h);
?? ? ? ? ? ? ? ?cell.setCellStyle(cellStyle1);
?? ? ? ? ? ?}
?? ? ? ? ? ?// 得到所有記錄 行:列
?? ? ? ? ? ?List<Record> list = Db.find(sql);
?? ? ? ? ? ?Record record = null;
?? ? ? ? ? ?HSSFCellStyle cellStyle2 = hssfWorkbook.createCellStyle();
?? ? ? ? ? ?cellStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
?? ? ? ? ? ?cellStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
?? ? ? ? ? ?cellStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
?? ? ? ? ? ?cellStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
?? ? ? ? ? ?cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
?? ? ? ? ? ?if (list != null) {
?? ? ? ? ? ? ? ?// 獲取所有的記錄 有多少條記錄就創建多少行
?? ? ? ? ? ? ? ?for (int i = 0; i < list.size(); i++) {
?? ? ? ? ? ? ? ? ? ?row = hssfSheet.createRow(rowIndex);
?? ? ? ? ? ? ? ? ? ?// 得到所有的行 一個record就代表 一行
?? ? ? ? ? ? ? ? ? ?record = list.get(i);
?? ? ? ? ? ? ? ? ? ?//下一行索引
?? ? ? ? ? ? ? ? ? ?rowIndex++;
?? ? ? ? ? ? ? ? ? ?//刷新新行索引
?? ? ? ? ? ? ? ? ? ?cellIndex = 0;
?? ? ? ? ? ? ? ? ? ?// 在有所有的記錄基礎之上,便利傳入進來的表頭,再創建N行
?? ? ? ? ? ? ? ? ? ?for (String h : colCode) {
?? ? ? ? ? ? ? ? ? ? ? ?cell = row.createCell(cellIndex);
?? ? ? ? ? ? ? ? ? ? ? ?cellIndex++;
?? ? ? ? ? ? ? ? ? ? ? ?//按照每條記錄匹配數據
?? ? ? ? ? ? ? ? ? ? ? ?cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
?? ? ? ? ? ? ? ? ? ? ? ?cell.setCellStyle(cellStyle2);
?? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?}

//設置第2行第13列單元格的內容
?? ? ? ? ? ? ? ?hssfSheet.getRow(1).createCell(12).setCellValue("數據來源:1-讀卡器采集; 2-手工錄入;3-底冊;4-ocr");
?? ? ? ? ? ?}
?? ? ? ? ? ?//自動調整每一列寬度
?? ? ? ? ? ?for(int i=0;i<colCode.length;i++) {
?? ? ? ? ? ??? ?hssfSheet.autoSizeColumn(i);
?? ? ? ? ? ??? ?hssfSheet.setColumnWidth(i, hssfSheet.getColumnWidth(i) * 17 / 10);
?? ? ? ? ? ?}
?? ? ? ? ? ?hssfSheet.autoSizeColumn(12);
?? ? ? ? ? ?hssfSheet.setColumnWidth(12, hssfSheet.getColumnWidth(12) * 17 / 10);
?? ? ? ? ? ?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;
?? ? ? ?}
}
?

3.頁面HTML按鈕

<form action="#(ctx)/admin/zklt/payInfo/export" method="post">
?? ??? ??? ??? ??? ??? ?<div class="form-group">
?? ??? ??? ??? ??? ??? ??? ?<div class="col-md-3">
?? ??? ??? ??? ??? ??? ??? ??? ?<label class="col-sm-5 control-label">繳費年份</label>
?? ??? ??? ??? ??? ??? ??? ??? ?<div class="col-sm-7">
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<input type="text" id="ssnf" name="ssnf" value="#(ssnf?ssnf:'')" class="form-control" readonly="readonly"
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?onFocus="WdatePicker({isShowClear:false,dateFmt:'yyyy',startDate:'%y'})">?
?? ??? ??? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ??? ?<div class="form-group">
?? ??? ??? ??? ??? ??? ??? ?<div class="col-md-3">
?? ??? ??? ??? ??? ??? ??? ??? ?<label class="col-sm-5 control-label">身份證號</label>
?? ??? ??? ??? ??? ??? ??? ??? ?<div class="col-sm-7">
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<input id="sfzh" name="sfzh" class="form-control" type="text">
?? ??? ??? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ??? ?<div class="form-group">
?? ??? ??? ??? ??? ??? ??? ?<div class="col-md-9 text-right">
?? ??? ??? ??? ??? ??? ??? ??? ?<button class="btn btn-mint" type="button" οnclick="refreshTable();"><i class="glyphicon glyphicon-search"></i> 搜索</button>&nbsp;
?? ??? ??? ??? ??? ??? ??? ??? ?<button class="btn btn-mint" type="submit"><i class="glyphicon glyphicon-arrow-up"></i> 導出</button>
?? ??? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ??? ?</form>

?

總結

以上是生活随笔為你收集整理的jfinal框架批量导出数据到Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。