日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中让数据生成excle文件并且支持下载

發布時間:2024/4/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中让数据生成excle文件并且支持下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先你要是用的是maven項目,你需要下一個jar包,添加一個配置:

//在maven中配jar包<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.10</version> </dependency>

?

將數據生成excle表格在controller中沒什么好寫的,無非是整理需要生成excle的數據,最主要是生成的過程需要考慮的問題很多,特別是大量數據的存儲問題

可以新建一個工具類:

package com.ai.loc.util;import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.Colour; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.NumberFormats; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException;public class MakeExcel {private static WritableCellFormat titleFormat = null;private static WritableCellFormat bodyFormat = null;private static WritableCellFormat noteFormat = null;private static WritableCellFormat floatFormat = null;private static WritableCellFormat intFormat = null;private static boolean init = false;public MakeExcel() {}public void init() throws WriteException {WritableFont font1, font2, font3, font4;font1 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD,false);titleFormat = new WritableCellFormat(font1);titleFormat.setBackground(Colour.ORANGE);titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);titleFormat.setAlignment(Alignment.CENTRE);font2 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD,false);noteFormat = new WritableCellFormat(font2);noteFormat.setBackground(Colour.ORANGE);noteFormat.setBorder(Border.ALL, BorderLineStyle.THIN);noteFormat.setAlignment(Alignment.CENTRE);noteFormat.setVerticalAlignment(VerticalAlignment.CENTRE);noteFormat.setWrap(true);font3 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,false);bodyFormat = new WritableCellFormat(font3);bodyFormat.setBackground(Colour.LIGHT_GREEN);bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN);font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,false);floatFormat = new WritableCellFormat(font4, NumberFormats.FLOAT);floatFormat.setBackground(Colour.LIGHT_GREEN);floatFormat.setBorder(Border.ALL, BorderLineStyle.THIN);// Arial���壬9�?��?��壬��?����?�����??�font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,false);intFormat = new WritableCellFormat(font4, NumberFormats.INTEGER);intFormat.setBackground(Colour.LIGHT_GREEN);intFormat.setBorder(Border.ALL, BorderLineStyle.THIN);init = true;}
//入參是一個list,一條數據存一個map對象,map對象中存列和值得對應關系,destFile當然就是要存的文件信息。 headList很重要,它是列的展示,當然和數據的列要對應不然找不到對應的地方存儲。
public static void CreateExcelFile(List<Map<String,Object>> list, File destFile,List<String> headList,String message)throws WriteException, IOException {int sizeAll=list.size();int zheng=sizeAll/65534;int yu=sizeAll%65534;int sheetSize=1;int flagList=1;if(sizeAll<=65534){sheetSize=1;}else{if(yu>0){sheetSize=zheng+1;}else{sheetSize=zheng;}}if (init == false)new MakeExcel().init();WritableWorkbook book = null;book = Workbook.createWorkbook(destFile);if(list.size()==0){book.write();}else{for(int j=0;j<sheetSize;j++){int index;System.out.println("*************************sheet"+j+"***************************");WritableSheet sheet = book.createSheet(destFile.getName().replace(".xls", "")+j, j);WritableFont BoldFont=new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊wcf_center.setAlignment(Alignment.CENTRE); // 文字水平對齊wcf_center.setWrap(false); // 文字是否換行for(int i=0;i<headList.size()+1;i++){sheet.setColumnView(i, 60);}sheet.mergeCells(0, 0, headList.size()-1, 0);sheet.addCell(new Label(0, 0, message, wcf_center));index = 0;for(String name :headList){sheet.addCell(new Label(index, 1, name,wcf_center));index++;}int i=0;int t=2;while(flagList<=list.size()){index = 0;if(i<65534){for(String name :headList){sheet.addCell(new Label(index, t, list.get(flagList-1).get(name)+""));index++;}i++;t++;flagList++;}else{break;}}}}book.write(); if (book != null)book.close();}public static void main(String[] args) {List<Map<String, Object>> list=new ArrayList<>();for(int i=0;i<195534;i++){Map<String, Object> map=new HashMap<>();map.put("a","a"+i );map.put("b","b"+i );map.put("c","c"+i );map.put("d","d"+i );list.add(map);}List<String> ll=new ArrayList<>();ll.add("a");ll.add("b");ll.add("c");ll.add("d");try {CreateExcelFile(list, new File("d:/a.xls"),ll,"hahaha");} catch (WriteException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (IOException e) { // // TODO Auto-generated catch block e.printStackTrace();}}}

生成了exle之后就是傳動了

package com.asiainfo.easyconfig2.util;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;/*** @author guohy**/ public class DownLoad {/*** 文件下載* * @param filepath* 文件路徑* @param response*/public void send(String filepath, HttpServletResponse response) {try {File file = new File(filepath);// path是文件地址String filename = file.getName();// 獲取日志文件名稱InputStream fis = new BufferedInputStream(new FileInputStream(filepath));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();// 先去掉文件名稱中的空格,然后轉換編碼格式為utf-8,保證不出現亂碼,這個文件名稱用于瀏覽器的下載框中自動顯示的文件名response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));response.addHeader("Content-Length", "" + file.length());OutputStream os = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");os.write(buffer);// 輸出文件os.flush();os.close();} catch (Exception e) {e.printStackTrace();}} }

  在controller中調一下這個方法即可,前端會識別出這個流,調出瀏覽器的下載彈框。

?

轉載于:https://www.cnblogs.com/mengzhongyunying/p/8721394.html

總結

以上是生活随笔為你收集整理的java中让数据生成excle文件并且支持下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。