java web將數(shù)據(jù)導(dǎo)出為Excel格式文件代碼片段
本文實(shí)例為大家分享了java web將數(shù)據(jù)導(dǎo)出為Excel格式文件的具體代碼,供大家參考,具體內(nèi)容如下
1、jsp代碼
<input type="button" class="btn btn-info" onclick="getVerExcel();" value="導(dǎo)出為Excel文件" />
2、js代碼
function getVerExcel() {
window.location.href = '/pms/jsp/version/getPrdVerListExcel?page='
+ $("#getPage").html() + '&key=' + $("#select").val();
}
3、java代碼
/**
*
* Purpose :將產(chǎn)品版本列表導(dǎo)出為Excel文件
* @param req
* 請(qǐng)求
* @param resp
* 應(yīng)答
* @param page
* 當(dāng)前頁(yè)數(shù)
* @param key
* 查詢條件
* @return
*/
@RequestMapping("getPrdVerListExcel")
public void getExcel(HttpServletRequest req, HttpServletResponse resp, Integer page, String key) {
// 設(shè)置文件的mime類型
resp.setContentType("application/vnd.ms-excel");
// 得到所有的數(shù)據(jù)
List<Version> verList = prdVersionSer.getAllPrdVersion(key);
// 若沒有數(shù)據(jù),則給用戶提示
if (verList.size() == 0) {
req.setAttribute("getFileMsg", "沒有符合條件的信息!");
req.setAttribute("select", key);
try {
req.getRequestDispatcher("/jsp/version/ver_list.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 存儲(chǔ)編碼后的文件名
String name = "name";
// 存儲(chǔ)文件名稱
String n = "";
if (key != "") {
n = verList.get(0).getPrdName() + "的版本列表";
} else {
n = "產(chǎn)品版本列表";
}
try {
name = URLEncoder.encode(n, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
resp.setHeader("content-disposition",
"attachment;filename=" + name + ".xls;filename*=utf-8''" + name + ".xls");
System.out.println("key:" + key);
// 從session中刪除saveExcelMsg屬性
req.getSession().removeAttribute("saveExcelMsg");
// 定義一個(gè)輸出流
ServletOutputStream sos = null;
// 創(chuàng)建一個(gè)工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 創(chuàng)建一個(gè)工作表
HSSFSheet sheet = null;
if (key != "") {
sheet = wb.createSheet(verList.get(0).getPrdName() + "的版本信息");
} else {
sheet = wb.createSheet("產(chǎn)品版本信息");
}
// 返回?cái)?shù)據(jù)格式對(duì)象
// 從格式對(duì)象中獲取對(duì)應(yīng)日期格式的編號(hào),如果格式不存在,該方法會(huì)為它生成新的編號(hào)
HSSFDataFormat format = wb.createDataFormat();
short dfNum = format.getFormat("yyyy-mm-dd");
// 創(chuàng)建樣式對(duì)象
CellStyle style = wb.createCellStyle();
// 設(shè)置數(shù)據(jù)格式
style.setDataFormat(dfNum);
// 創(chuàng)建第一行(表格標(biāo)題)
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
if (key != "") {
cell.setCellValue(verList.get(0).getPrdName() + "的產(chǎn)品版本列表");
} else {
cell.setCellValue("產(chǎn)品版本列表");
}
// 創(chuàng)建第二行(表頭)
row = sheet.createRow(1);
cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("序號(hào)");
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("產(chǎn)品名稱");
cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("版本號(hào)");
cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("發(fā)布日期");
cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("版本類型");
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("版本描述");
int num = 1;
// 遍歷輸出verList中的數(shù)據(jù),將其存入Excel中
for (int i = 0; i < verList.size(); i++) {
row = sheet.createRow(i + 2);
// 寫入序號(hào)
cell = row.createCell(0, HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(num);
num++;
// 寫入產(chǎn)品名稱
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getPrdName());
// 寫入版本號(hào)
cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerNo());
// 寫入發(fā)布日期(日期格式做處理)
cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
// 將樣式應(yīng)用于單元格
cell.setCellStyle(style);
cell.setCellValue(verList.get(i).getVerDate());
// 寫入版本類型
cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerType());
// 寫入版本描述
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerDesc());
}
try {
// 保存到文件中
sos = resp.getOutputStream();
wb.write(sos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sos != null) {
try {
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
教你如何使用Java8實(shí)現(xiàn)菜單樹形數(shù)據(jù)
今天給大家?guī)?lái)的是關(guān)于JAVA的相關(guān)知識(shí),文中圍繞著如何使用Java8實(shí)現(xiàn)菜單樹形數(shù)據(jù)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Spring MVC 4.1.3 + MyBatis零基礎(chǔ)搭建Web開發(fā)框架(注解模式)
本篇文章主要介紹了Spring MVC 4.1.3 + MyBatis零基礎(chǔ)搭建Web開發(fā)框架(注解模式),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法
今天小編就為大家分享一篇java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-07-07
Spring Bean如何實(shí)現(xiàn)自動(dòng)配置代碼實(shí)例
這篇文章主要介紹了Spring Bean如何實(shí)現(xiàn)自動(dòng)配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot配置Email發(fā)送功能實(shí)例
本篇文章主要介紹了SpringBoot配置Email發(fā)送功能實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-04-04

