99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

App下載

Java項目中如何用POI導出數據到Excel文件中

獨留清風醉 2021-08-10 14:15:28 瀏覽數 (2643)
反饋

在項目開發(fā)的過程中,少不了要把數據導入導出,把數據導出到Excel文檔中又是經常使用的。本篇文章將介紹一個Java提供的POI來實現(xiàn)把數據導出到Excel文檔中。

一、前言

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

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

//第一步創(chuàng)建一個webbook,對應一個Excel文件
	HSSFWorkbook wb=new HSSFWorkbook();
	//第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
	HSSFSheet sheet=wb.createSheet("食物信息數據");
	//第三步,在sheet中添加表頭第0行
	HSSFRow row = sheet.createRow(0);
	//第四步,創(chuàng)建單元格,并設置表頭居中
	HSSFCellStyle style = wb.createCellStyle();
	style.setAlignment(HorizontalAlignment.CENTER);//居中格式
	HSSFCell cell = row.createCell(0);
	cell.setCellValue("編號");
	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("單價");
	cell.setCellStyle(style);
	
	cell=row.createCell((short)4);
	cell.setCellValue("庫存");
	cell.setCellStyle(style);
	
	//第五步,寫入實體數據,從數據庫拿數據
	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);

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

導出成功后數據成功顯示

2021042211335960

以上就是關于Java中POI的使用,以及使用POI導出數據到Excel的步驟,想要了解更多Java POI導出數據到其他文檔文件的內容,可以瀏覽W3Cschool相關技術文章,希望本篇文章能夠對大家的學習有所幫助!


0 人點贊