最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中用POI實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel

 更新時(shí)間:2021年04月22日 11:39:38   作者:憶晨丶  
這篇文章主要介紹了Java中用POI實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下

一、前言

數(shù)據(jù)導(dǎo)出為Excel在我們寫(xiě)項(xiàng)目的過(guò)程中經(jīng)常用到

需要用到的jar包 poi-3.17.jar

二、具體實(shí)現(xiàn)步驟

//第一步創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件
	HSSFWorkbook wb=new HSSFWorkbook();
	//第二步,在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
	HSSFSheet sheet=wb.createSheet("食物信息數(shù)據(jù)");
	//第三步,在sheet中添加表頭第0行
	HSSFRow row = sheet.createRow(0);
	//第四步,創(chuàng)建單元格,并設(shè)置表頭居中
	HSSFCellStyle style = wb.createCellStyle();
	style.setAlignment(HorizontalAlignment.CENTER);//居中格式
	HSSFCell cell = row.createCell(0);
	cell.setCellValue("編號(hào)");
	cell.setCellStyle(style);
	
	cell=row.createCell((short)1);
	cell.setCellValue("名稱");
	cell.setCellStyle(style);
	
	cell=row.createCell((short)2);
	cell.setCellValue("類型");
	cell.setCellStyle(style);
	
	cell=row.createCell((short)3);
	cell.setCellValue("單價(jià)");
	cell.setCellStyle(style);
	
	cell=row.createCell((short)4);
	cell.setCellValue("庫(kù)存");
	cell.setCellStyle(style);
	
	//第五步,寫(xiě)入實(shí)體數(shù)據(jù),從數(shù)據(jù)庫(kù)拿數(shù)據(jù)
	FoodController controller=new FoodController();
	List<Foods> foodsList = controller.foodsList(null, null);
	for (int i = 0; i < foodsList.size(); i++) {
		//創(chuàng)建單元格,并賦值
		row=sheet.createRow(i+1);
		Foods foods = foodsList.get(i);
		row.createCell((short)0).setCellValue(foods.getId());
		row.createCell((short)1).setCellValue(foods.getName());
		row.createCell((short)2).setCellValue(foods.getType());
		row.createCell((short)3).setCellValue(foods.getPrice());
		row.createCell((short)4).setCellValue(foods.getNum());
	}
	//第六步,下載Excel
	OutputStream out=null;
	out=response.getOutputStream();
	String fileName="食物信息.xls";
	response.setContentType("application/x-=msdownload");
	response.setHeader("Content-Disposition", "attachment; filename="
			+URLEncoder.encode(fileName, "UTF-8"));
	wb.write(out);

三、實(shí)現(xiàn)效果圖

導(dǎo)出成功后數(shù)據(jù)成功顯示

在這里插入圖片描述

到此這篇關(guān)于Java中用POI實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel的文章就介紹到這了,更多相關(guān)java數(shù)據(jù)導(dǎo)出到Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

德保县| 涟源市| 武清区| 南溪县| 和平区| 江陵县| 承德市| 乌苏市| 肥东县| 循化| 河北省| 民丰县| 肥东县| 枝江市| 枣庄市| 南昌县| 黄骅市| 崇文区| 通辽市| 玛多县| 宜昌市| 大竹县| 屯门区| 英超| 阿荣旗| 湘潭市| 茌平县| 赞皇县| 理塘县| 合作市| 和硕县| 惠安县| 永德县| 马龙县| 台州市| 乌什县| 木兰县| 什邡市| 宜川县| 平陆县| 贵德县|