1. 程式人生 > >Java SpringMVC專案匯出excel多種類對應工具類整理(util)

Java SpringMVC專案匯出excel多種類對應工具類整理(util)

上一章介紹如何匯入excel進系統處理成MAP集合(詳情請看上提供的連結),這次我又整理了一下如何快速匯出excel的工具。

1.匯出無表頭excel 檔案單個工作表(sheet),【fileName是標題名,titleList是列名,list就是列的內容了】

/**
	 * 匯出無表頭excel 檔案單個工作表(sheet)
	 * 
	 * @param fileName
	 * @param titleList
	 * @param list
	 * @param response
	 */
	public static void exportNoHeadExcel(String fileName, String[] titleList,
			List list, HttpServletResponse response) {
		SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
		String todayStr = df.format(new Date());
		OutputStream os = null;
		try {
			os = response.getOutputStream();
			String localFileName = fileName;
			fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 處理中文檔名的問題
			fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 處理中文檔名的問題
			response.setContentType("application/vnd.ms-excel;");
			response.setHeader("Content-disposition", "attachment; filename=\""
					+ fileName + "_" + todayStr + ".xls\"");
			// 開始寫入excel
			// 欄位字型
			jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
					WritableFont.COURIER, 10, WritableFont.NO_BOLD, true);
			jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
					wfc1);
			wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			// 結果字型
			jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
			wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			WritableWorkbook wbook = Workbook.createWorkbook(os);
			// 寫sheet名稱
			WritableSheet wsheet = wbook.createSheet(localFileName, 0);
			for (int m = 0; m < titleList.length; m++) {
				wsheet.setColumnView(m, 30);
			}
			// 加入欄位名
			for (int n = 0; n < titleList.length; n++) {
				wsheet.addCell(new jxl.write.Label(n, 0, titleList[n], wcfFC1));
			}
			// 寫入流中
			int row = 0;
			for (int r = 0; r < list.size(); r++) {
				Object[] obj = (Object[]) list.get(r);
				for (int x = 0; x < titleList.length; x++) {
					wsheet.addCell(new jxl.write.Label(x, row + 1,
							obj[x] == null ? " " : obj[x].toString(), wcfFC1));
				}
				row++;
				if (row % 60000 == 0) {
					row = 0;
					// 寫sheet名稱
					wsheet = wbook.createSheet(localFileName, 0);
					for (int m = 0; m < titleList.length; m++) {
						wsheet.setColumnView(m, 30);
					}
					// 加入欄位名
					for (int n = 0; n < titleList.length; n++) {
						wsheet.addCell(new jxl.write.Label(n, 0, titleList[n],
								wcfFC1));
					}
				}
			}
			wbook.write();
			wbook.close();
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os == null) {
				Log.info("os is null");
			} else {
				try {
					os.close();
					os = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

2.首先匯出無表頭excel 檔案帶多個工作表(sheet),【sheetList存放的就是Map包含fileName是標題名,titleList是列名,list就是列的內容】

/**
	 * 匯出無表頭excel檔案
	 * 
	 * @param fileName
	 * @param titleList
	 * @param list
	 * @param response
	 */
	public static void exportNoHeadExcel(List<Map<String,Object>> sheetList, HttpServletResponse response) {
		SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
		String todayStr = df.format(new Date());
		OutputStream os = null;
		try {
			os = response.getOutputStream();
			WritableWorkbook wbook = Workbook.createWorkbook(os);
			for(int i=0;i<sheetList.size();i++){
				Map<String,Object> map=sheetList.get(i);
				String fileName=(String) map.get("fileName");
				String[] titleList=(String[]) map.get("titleList");
				List list=(List) map.get("list");
				String localFileName = fileName;
				fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 處理中文檔名的問題
				fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 處理中文檔名的問題
				response.setContentType("application/vnd.ms-excel;");
				response.setHeader("Content-disposition", "attachment; filename=\""
						+ fileName + "_" + todayStr + ".xls\"");
				// 開始寫入excel
				// 欄位字型
				jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
						WritableFont.COURIER, 10, WritableFont.NO_BOLD, true);
				jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
						wfc1);
				wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
				wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
				// 結果字型
				jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
				wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
				wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
				// 寫sheet名稱
				WritableSheet wsheet = wbook.createSheet(localFileName,i);
				for (int m = 0; m < titleList.length; m++) {
					wsheet.setColumnView(m, 30);
				}
				// 加入欄位名
				for (int n = 0; n < titleList.length; n++) {
					wsheet.addCell(new jxl.write.Label(n, 0, titleList[n], wcfFC1));
				}
				// 寫入流中
				int row = 0;
				for (int r = 0; r < list.size(); r++) {
					Object[] obj = (Object[]) list.get(r);
					for (int x = 0; x < titleList.length; x++) {
						wsheet.addCell(new jxl.write.Label(x, row + 1,
								obj[x] == null ? " " : obj[x].toString(), wcfFC1));
					}
					row++;
					if (row % 60000 == 0) {
						row = 0;
						// 寫sheet名稱
						wsheet = wbook.createSheet(localFileName, 0);
						for (int m = 0; m < titleList.length; m++) {
							wsheet.setColumnView(m, 30);
						}
						// 加入欄位名
						for (int n = 0; n < titleList.length; n++) {
							wsheet.addCell(new jxl.write.Label(n, 0, titleList[n],
									wcfFC1));
						}
					}
				}
			}	
			wbook.write();
			wbook.close();
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os == null) {
				Log.info("os is null");
			} else {
				try {
					os.close();
					os = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

3.匯出excel檔案帶標題【fileName是標題名,titleList是列名,list就是列的內容了】
/**
	 * 匯出excel 檔案  帶標題
	 * 
	 * @param fileName 
	 * @param titleList
	 * @param list
	 * @param response
	 */
	public static void exportWithHeadExcel(String fileName, String[] titleList,
			List list, HttpServletResponse response) {
		Date now = new Date();
		SimpleDateFormat dateformat = new java.text.SimpleDateFormat(
				"yyyy年MM月dd日HH時mm分");
		SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
		String todayStr = df.format(new Date());
		String today = dateformat.format(now);
		OutputStream os = null;
		try {
			os = response.getOutputStream();
			String localFileName = fileName;
			fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 處理中文檔名的問題
			fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 處理中文檔名的問題
			response.setContentType("application/vnd.ms-excel;");
			response.setHeader("Content-disposition", "attachment; filename=\""
					+ fileName + "_" + todayStr + ".xls\"");
			// 開始寫入excel
			// 加標題
			// 標題字型
			jxl.write.WritableFont wfc = new jxl.write.WritableFont(
					WritableFont.COURIER, 18, WritableFont.NO_BOLD, false);
			jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
					wfc);
			wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			// 欄位字型
			jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
					WritableFont.COURIER, 10, WritableFont.NO_BOLD, false);
			jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
					wfc1);
			wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			// 結果字型
			jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
			wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			WritableWorkbook wbook = Workbook.createWorkbook(os);
			// 寫sheet名稱
			WritableSheet wsheet = wbook.createSheet(localFileName, 0);
			int i = 3;
			for (int m = 0; m < titleList.length; m++) {
				wsheet.setColumnView(m, 30);
			}
			// 加入欄位名
			for (int n = 0; n < titleList.length; n++) {
				wsheet.addCell(new jxl.write.Label(n, 3, titleList[n], wcfFC1));
			}
			// 加入標題
			wsheet.mergeCells(0, 0, i - 1, 0);
			wsheet.addCell(new Label(0, 0, localFileName, wcfFC));
			// 加入列印時間
			wsheet.addCell(new Label(i - 2, 1, "列印日期:"));
			wsheet.addCell(new Label(i - 1, 1, today));
			// 寫入流中
			int row = 0;
			for (int r = 0; r < list.size(); r++) {
				Object[] obj = (Object[]) list.get(r);
				for (int x = 0; x < titleList.length; x++) {
					wsheet.addCell(new jxl.write.Label(x, row + 4,
							obj[x] == null ? " " : obj[x].toString(), wcfFC1));
				}
				row++;
				if (row % 60000 == 0) {
					row = 0;
					// 寫sheet名稱
					wsheet = wbook.createSheet(localFileName, 0);
					i = 3;
					for (int m = 0; m < titleList.length; m++) {
						wsheet.setColumnView(m, 30);
					}
					// 加入欄位名
					for (int n = 0; n < titleList.length; n++) {
						wsheet.addCell(new jxl.write.Label(n, 3, titleList[n],
								wcfFC1));
					}
					// 加入標題
					wsheet.mergeCells(0, 0, i - 1, 0);
					wsheet.addCell(new Label(0, 0, localFileName, wcfFC));
					// 加入列印時間
					wsheet.addCell(new Label(i - 2, 1, "列印日期:"));
					wsheet.addCell(new Label(i - 1, 1, today));
				}
			}
			wbook.write();
			wbook.close();
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os == null) {
				Log.info("os is null");
			} else {
				try {
					os.close();
					os = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

目前大概整理了這3個,以後用到還會整理更新,今天就先到在這裡吧..待續...

....謝謝閱讀!