java实现把数据写入到Excel并下载
生活随笔
收集整理的這篇文章主要介紹了
java实现把数据写入到Excel并下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
引入依賴:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.0</version></dependency> @RequestMapping("/download")public void download(String year, String month, HttpServletResponse response,HttpServletRequest request) throws IOException {List<ProductList> productLists = loginService.findProdutSales(year, month);String filename = year + "年" + month + "月銷售榜單";String sheetName = month + "月銷售榜單";String titlename = year + "年" + month + "月銷售榜單";String[] columnName = {"商品名稱", "商品銷量"};String[][] dataList = new String[productLists.size()][2]; //二維數字必須指定長度for (int i = 0; i < productLists.size(); i++) {dataList[i][0] = productLists.get(i).getName();dataList[i][1] = productLists.get(i).getSales();}//創建一個excel文件HSSFWorkbook excel =new HSSFWorkbook();//創建一個表HSSFSheet sheet = excel.createSheet(sheetName);//創建表的第一行HSSFRow row1=sheet.createRow(0);//創建第一行的第一個單元格HSSFCell cell=row1.createCell(0);//合并第一行的兩個單元格 起始行 終止行 起始列 終止列sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));//給第一行的的一個合并后的單元格賦值cell.setCellValue(titlename);//創建第二行HSSFRow row=sheet.createRow(1);for(int i=0;i<2;i++){row.createCell(i).setCellValue(columnName[i]);}//創建第三行 循環,都是數據for(int i=0;i<dataList.length;i++){ //二維數組的長度是行的長度,與列無關row=sheet.createRow(i+2);for(int j=0;j<2;j++){row.createCell(j).setCellValue(dataList[i][j]);}}總結
以上是生活随笔為你收集整理的java实现把数据写入到Excel并下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow测试gpu_如何检验
- 下一篇: mvn install:install-