java導出excel 瀏覽器直接下載或者或以文件形式導出
更新時間:2021年06月10日 09:16:25 作者:低調(diào)的小白
這篇文章主要介紹了java導出excel 瀏覽器直接下載或者或以文件形式導出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
看代碼吧~
/**
* excel表格直接下載
*/
public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse httpServletResponse,String fileName) throws Exception {
//響應類型為application/octet- stream情況下使用了這個頭信息的話,那就意味著不想直接顯示內(nèi)容
httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//attachment為以附件方式下載
httpServletResponse.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(
fileName + ".xls",
"utf-8"));
/**
* 代碼里面使用Content-Disposition來確保瀏覽器彈出下載對話框的時候。
* response.addHeader("Content-Disposition","attachment");一定要確保沒有做過關(guān)于禁止瀏覽器緩存的操作
*/
httpServletResponse.setHeader("Cache-Control", "No-cache");
httpServletResponse.flushBuffer();
wb.write(httpServletResponse.getOutputStream());
wb.close();
}
/**
* excel以文件的形式導出
* @throws Exception
*/
public static void exportExcelByFile(HSSFWorkbook wb,String fileName,String path) throws Exception{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
wb.write(stream);
FileOutputStream outputStream = new FileOutputStream(path + fileName);
outputStream.write(stream.toByteArray());
stream.close();
outputStream.close();
}
java查詢數(shù)據(jù)導出excel并返回給瀏覽器下載
效果圖:
1.點擊導出表按鈕

2.接著就會出現(xiàn)下圖

3.點擊上圖中的確定按鈕再接著就會出現(xiàn)下圖

4.點擊上圖中的保存按鈕接著就會出現(xiàn)下圖,瀏覽器下載完成后的提示

5.打開下載好的文件如下圖

好了,廢話不多少,上代碼
jsp前端代碼
<div style="height:30px;">
<a>時間:</a>
<input id="startDateConsume" type="text" class="easyui-datebox"> <a>-</a>
<input id="endDateConsume" type="text" class="easyui-datebox">
<a>消費類型:</a>
<select id="consumesType" name="">
<option value="0" selected="selected">所有</option>
<option value="1">報名費</option>
<option value="2">酒水零食類</option>
</select>
<a>支付狀態(tài):</a>
<select id="conPaymentStatus" name="">
<option value="0" selected="selected">所有</option>
<option value="1">未支付</option>
<option value="2">已支付</option>
</select>
<a id="btnConsumesSearch" class="easyui-linkbutton"
data-options="iconCls:'icon-search'" style="margin-left:10px">查詢</a><a>(查詢出來的數(shù)據(jù)可統(tǒng)計)</a>
<a id="consumesOutExcel" class="easyui-linkbutton" style="" data-options="iconCls:'icon-redo'">導出表</a>
</div>
js前端代碼
$(function() {
//導出excel表
$('#consumesOutExcel').on('click',function(){
exportExcel();
});
});
function exportExcel() {
$.messager.confirm('確認', '確認把該搜索結(jié)果導出Excel表格 ?', function(r) {
if (r) {
var startTime = $('#startDateConsume').val();
var endTime = $('#endDateConsume').val();
var consumesType = $('#consumesType').val();
var conPaymentStatus = $('#conPaymentStatus').val();
$.messager.progress({
title : '處理中',
msg : '請稍后',
});
$.messager.progress('close');
location.href="web/vip/exportExcel.xlsx?startTime=" rel="external nofollow" +startTime+"&endTime="+endTime+"&consumesType="+consumesType+"&conPaymentStatus="+conPaymentStatus;
}
});
}
java后端代碼
@Controller
@RequestMapping("/vip")
public class VipController {
//文件下載:導出excel表
@RequestMapping(value = "/exportExcel.xlsx",method = RequestMethod.GET)
@ResponseBody
public void exportExcel(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
//一、從后臺拿數(shù)據(jù)
if (null == request || null == response)
{
return;
}
List<VipConsumes> list = null;
String startTime = request.getParameter("startTime");
String endTime = request.getParameter("endTime");
int consumesType = Integer.parseInt(request.getParameter("consumesType"));
int conPaymentStatus =Integer.parseInt(request.getParameter("conPaymentStatus"));
VipConsumesExample example = new VipConsumesExample();
if(consumesType!=0 && conPaymentStatus!=0){
example.createCriteria().andTimeBetween(startTime, endTime).andConsumeTypeEqualTo(consumesType).andStatusEqualTo(conPaymentStatus);
}else if(consumesType ==0 && conPaymentStatus!=0) {
example.createCriteria().andTimeBetween(startTime, endTime).andStatusEqualTo(conPaymentStatus);
}else if(consumesType!=0 && conPaymentStatus==0){
example.createCriteria().andTimeBetween(startTime, endTime).andConsumeTypeEqualTo(consumesType);
}else {
example.createCriteria().andTimeBetween(startTime, endTime);
}
list = this.vipConsumesDao.selectByExample(example);
//二、 數(shù)據(jù)轉(zhuǎn)成excel
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
String fileName = "消費記錄.xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 第一步:定義一個新的工作簿
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步:創(chuàng)建一個Sheet頁
XSSFSheet sheet = wb.createSheet("startTimeendTime");
sheet.setDefaultRowHeight((short) (2 * 256));//設(shè)置行高
sheet.setColumnWidth(0, 4000);//設(shè)置列寬
sheet.setColumnWidth(1,5500);
sheet.setColumnWidth(2,5500);
sheet.setColumnWidth(3,5500);
sheet.setColumnWidth(11,3000);
sheet.setColumnWidth(12,3000);
sheet.setColumnWidth(13,3000);
XSSFFont font = wb.createFont();
font.setFontName("宋體");
font.setFontHeightInPoints((short) 16);
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("流水號 ");
cell = row.createCell(1);
cell.setCellValue("微信名 ");
cell = row.createCell(2);
cell.setCellValue("微信訂單號");
cell = row.createCell(3);
cell.setCellValue("消費時間");
cell = row.createCell(4);
cell.setCellValue("消費類型");
cell = row.createCell(5);
cell.setCellValue("剩余積分 ");
cell = row.createCell(6);
cell.setCellValue("新增積分 ");
cell = row.createCell(7);
cell.setCellValue("扣除積分 ");
cell = row.createCell(8);
cell.setCellValue("消費金額");
cell = row.createCell(9);
cell.setCellValue("支付方式");
cell = row.createCell(10);
cell.setCellValue("支付狀態(tài) ");
cell = row.createCell(11);
cell.setCellValue("錢包原始金額");
cell = row.createCell(12);
cell.setCellValue("錢包扣除金額");
cell = row.createCell(13);
cell.setCellValue("錢包剩余金額");
XSSFRow rows;
XSSFCell cells;
for (int i = 0; i < list.size(); i++) {
// 第三步:在這個sheet頁里創(chuàng)建一行
rows = sheet.createRow(i+1);
// 第四步:在該行創(chuàng)建一個單元格
cells = rows.createCell(0);
// 第五步:在該單元格里設(shè)置值
cells.setCellValue(list.get(i).getConsumeId());
cells = rows.createCell(1);
cells.setCellValue(list.get(i).getName());
cells = rows.createCell(2);
cells.setCellValue(list.get(i).getOrderNumber());
cells = rows.createCell(3);
cells.setCellValue(list.get(i).getTime());
cells = rows.createCell(4);
if (list.get(i).getConsumeType() == 2) {
cells.setCellValue("酒水零食費");
} else {
cells.setCellValue("報名費");
}
cells = rows.createCell(5);
cells.setCellValue(list.get(i).getIntegral());
cells = rows.createCell(6);
cells.setCellValue(list.get(i).getIntegralIn());
cells = rows.createCell(7);
cells.setCellValue(list.get(i).getIntegralOut());
cells = rows.createCell(8);
cells.setCellValue(list.get(i).getMoney());
cells = rows.createCell(9);
if (list.get(i).getPayment() == 2) {
cells.setCellValue("積分抵現(xiàn)");
} else if (list.get(i).getPayment() == 3) {
cells.setCellValue("微信支付");
} else if (list.get(i).getPayment() == 4) {
cells.setCellValue("現(xiàn)金");
} else if (list.get(i).getPayment() == 1) {
cells.setCellValue("錢包");
}
cells = rows.createCell(10);
if (list.get(i).getStatus() == 2) {
cells.setCellValue("已支付");
} else if (list.get(i).getStatus() == 1) {
cells.setCellValue("未支付");
}
cells = rows.createCell(11);
cells.setCellValue(list.get(i).getWalletOriginal());
cells = rows.createCell(12);
cells.setCellValue(list.get(i).getWalletOut());
cells = rows.createCell(13);
cells.setCellValue(list.get(i).getWalletSurplus());
}
try {
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
wb.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security和Shiro的相同點與不同點整理
在本篇文章里小編給大家整理的是關(guān)于Spring Security和Shiro的相同不同點整理,需要的朋友們可以參考下。2020-02-02
SpringBoot如何監(jiān)控Redis中某個Key的變化(自定義監(jiān)聽器)
這篇文章主要介紹了SpringBoot如何監(jiān)控Redis中某個Key的變化(自定義監(jiān)聽器),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
java實現(xiàn)合并單元格的同時并導出excel示例
這篇文章主要給大家介紹了關(guān)于java實現(xiàn)合并單元格的同時并導出excel的相關(guān)資料,文中先進行了簡單的介紹,之后給出了詳細的示例代碼,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03

