首先你要是用的是maven項目,你須要下一個jar包,添加一個配置:前端
//在maven中配jar包 <dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
將數據生成excle表格在controller中沒什麼好寫的,無非是整理須要生成excle的數據,最主要是生成的過程須要考慮的問題不少,特別是大量數據的存儲問題java
能夠新建一個工具類:api
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中調一下這個方法便可,前端會識別出這個流,調出瀏覽器的下載彈框。app