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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java 自带导出excel_4.java项目页面导出excel功能

發布時間:2025/3/8 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 自带导出excel_4.java项目页面导出excel功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用的是SSM框架,字段根據自己的業務需求改

1.前臺頁面

導出

/*導出按鈕提交*/

function downloadExcel(){

$("#dynamicDownload").submit();

}

2.后臺相關代碼

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;

/**

* 導出考勤記錄

* @param request

* @param response

* @param model

*/

@RequestMapping(value="downloadExcel")

public void downLoadExcel(HttpServletRequest request,HttpServletResponse response,Model model){

try{

//1-查出要導出的數據

String user_id = request.getSession().getAttribute("user_id").toString();//獲取保存登陸信息的員工id

HashMap paramMap = new HashMap();

String date_start1 = request.getParameter("inpstart");

String date_end1 = request.getParameter("inpend");

String userORname1 = request.getParameter("userORname");

String PART_NAME1 = request.getParameter("dId");

paramMap.put("date_start", date_start1);

paramMap.put("date_end", date_end1);

paramMap.put("userORname", userORname1);

paramMap.put("PART_NAME", PART_NAME1);

List> sourceRecordList=sourceRecordService.selectSourceRecordNotpage(paramMap);

//第一步,創建一個webbook,對應一個Excel文件

HSSFWorkbook wk = new HSSFWorkbook();

//第二步,創建一個sheet表對象,創建row對象,getExcelStyle1是一個創建模板的方法,最后面有

HSSFSheet sheet = getExcelStyle1(wk,"考勤信息表");

HSSFRow row;

HSSFCellStyle style = wk.createCellStyle();

HSSFFont font = wk.createFont();

font.setFontName("微軟雅黑");

font.setFontHeightInPoints((short)15);

style.setFont(font);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

//第三步,查詢出表內容放到map中

SimpleDateFormat sdf_out = new SimpleDateFormat("yyyyMMdd");

String time_out = sdf_out.format(new Date());

String srcPath=request.getSession().getServletContext().getRealPath("")+"/考勤信息"+time_out+".xls";//設置將excel數據上傳至服務器的路徑

for(int i=0;i

row=sheet.createRow(i+1);//從第二行還是導入數據

String USER_ID = sourceRecordList.get(i).get("USER_ID").toString();//查詢列表中獲取所需字段數據

String USER_NAME = sourceRecordList.get(i).get("USER_NAME").toString();

String PART_NAME = sourceRecordList.get(i).get("PART_NAME").toString();

String ATTENDANCETEAM_NAME = sourceRecordList.get(i).get("ATTENDANCETEAM_NAME").toString();

String POST_NAME = sourceRecordList.get(i).get("POST_NAME").toString();

String ATTENDANCE_DATE = sourceRecordList.get(i).get("ATTENDANCE_DATE").toString();

String ATTENDANCE_TIME = sourceRecordList.get(i).get("ATTENDANCE_TIME").toString();

String EQUIPMENT_ADDRESS = sourceRecordList.get(i).get("EQUIPMENT_ADDRESS").toString();

if(USER_ID !=null){

row.createCell((short)0).setCellValue(USER_ID);

}else{

row.createCell((short)0).setCellValue("");

}

if(USER_NAME !=null){

row.createCell((short)1).setCellValue(USER_NAME);

}else{

row.createCell((short)1).setCellValue("");

}

if(PART_NAME !=null){

row.createCell((short)2).setCellValue(PART_NAME);

}else{

row.createCell((short)2).setCellValue("");

}

if(ATTENDANCETEAM_NAME !=null){

row.createCell((short)3).setCellValue(ATTENDANCETEAM_NAME);

}else{

row.createCell((short)3).setCellValue("");

}

if(POST_NAME !=null){

row.createCell((short)4).setCellValue(POST_NAME);

}else{

row.createCell((short)4).setCellValue("");

}

if(ATTENDANCE_DATE !=null){

row.createCell((short)5).setCellValue(ATTENDANCE_DATE);

}else{

row.createCell((short)5).setCellValue("");

}

if(ATTENDANCE_TIME !=null){

row.createCell((short)6).setCellValue(ATTENDANCE_TIME);

}else{

row.createCell((short)6).setCellValue("");

}

if(EQUIPMENT_ADDRESS !=null){

row.createCell((short)7).setCellValue(EQUIPMENT_ADDRESS);

}else{

row.createCell((short)7).setCellValue("");

}

}

try {

FileOutputStream fout = new FileOutputStream(srcPath);//創建一個服務器管道地址

wk.write(fout);//把excel數據寫到服務器中

//讀服務器中的數據

File exportFile = new File(srcPath);

FileInputStream fs=null;

//告訴瀏覽器這次請求是一個下載的數據流

response.setContentType("APPLICATION/OCTET-STREAM");

fs=new FileInputStream(exportFile);

//ExportUtil是一個下載的工具類,后面會有

ExportUtil.download(response, fs, "考勤信息-"+time_out+".xls",null);

} catch (Exception e) {

e.printStackTrace();

}

}catch(Exception e){

e.printStackTrace();

}

}

}

/**

* 創建一個excel模板:里面有表頭信息

*/

public HSSFSheet getExcelStyle1(HSSFWorkbook wk,String name){

HSSFSheet sheet = wk.createSheet(name);

//設置表的樣式 新加一行

HSSFRow row1 = sheet.createRow((int) 0);

HSSFCellStyle style1 = wk.createCellStyle();

style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);

HSSFFont font1 = wk.createFont();

font1.setFontName("微軟雅黑");

font1.setFontHeightInPoints((short)12);

style1.setFont(font1);

row1.createCell((short)0).setCellValue("工號");

row1.createCell((short)1).setCellValue("姓名");

row1.createCell((short)2).setCellValue("部門");

row1.createCell((short)3).setCellValue("所屬考勤組");

row1.createCell((short)4).setCellValue("崗位名稱");

row1.createCell((short)5).setCellValue("日期");

row1.createCell((short)6).setCellValue("打卡時間");

row1.createCell((short)7).setCellValue("設備地址");

return sheet;

}

ExportUtil 下載的工具類

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

import org.apache.commons.lang.StringUtils;

public class ExportUtil {

/**

* 導出文件

*

* @param response

* @param file

* 導出文件

* @param name

* @param contentType

* @throws IOException

*/

public static void download(HttpServletResponse response, File file,

String name, String contentType) throws IOException {

String fileName = StringUtils.isBlank(name) ? file.getName() : name;

download(response, new FileInputStream(file), fileName, contentType);

}

/**

* 下載數據/文件

*

* @param response

* HTTP輸出

* @param inputStream

* 文件流

* @param fileName

* 文件名

* @param contentType

* ContentType in HTTP Header

* @throws IOException

* IO異常

*/

public static void download(HttpServletResponse response,

InputStream inputStream, String fileName, String contentType)

throws IOException {

response.setContentType(StringUtils.isEmpty(contentType) ? "application/octet-stream"

: contentType);

response.setHeader("Content-Disposition", "attachment;filename="

+ new String(fileName.getBytes("gbk"), "ISO-8859-1"));

response.setStatus(HttpServletResponse.SC_OK);

BufferedInputStream reader = null;

try {

reader = new BufferedInputStream(inputStream);

IOUtils.copy(reader, response.getOutputStream());

} finally {

if (reader != null) {

reader.close();

}

if (inputStream != null) {

inputStream.close();

}

}

}

}

總結

以上是生活随笔為你收集整理的java 自带导出excel_4.java项目页面导出excel功能的全部內容,希望文章能夠幫你解決所遇到的問題。

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