国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:142日期:2022-05-23 11:58:17
一、前言

數(shù)據(jù)導出為Excel在我們寫項目的過程中經(jīng)常用到

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

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

//第一步創(chuàng)建一個webbook,對應一個Excel文件HSSFWorkbook wb=new HSSFWorkbook();//第二步,在webbook中添加一個sheet,對應Excel文件中的sheetHSSFSheet sheet=wb.createSheet('食物信息數(shù)據(jù)');//第三步,在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);//第五步,寫入實體數(shù)據(jù),從數(shù)據(jù)庫拿數(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());}//第六步,下載ExcelOutputStream 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)效果圖

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

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

到此這篇關于Java中用POI實現(xiàn)將數(shù)據(jù)導出到Excel的文章就介紹到這了,更多相關java數(shù)據(jù)導出到Excel內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: excel
相關文章:
主站蜘蛛池模板: 峨山| 恭城| 西峡县| 岐山县| 滨州市| 磐石市| 安阳县| 师宗县| 阳江市| 凌源市| 巫溪县| 蓬莱市| 新竹县| 洞口县| 新巴尔虎右旗| 洪泽县| 萨嘎县| 太康县| 高清| 龙泉市| 绩溪县| 调兵山市| 天全县| 临沭县| 苗栗县| 当阳市| 绵阳市| 运城市| 临清市| 麟游县| 汤阴县| 太湖县| 乐昌市| 宣恩县| 克山县| 澄迈县| 油尖旺区| 客服| 隆安县| 军事| 南雄市|