Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)中查詢出數(shù)據(jù)轉(zhuǎn)存成excel表的方法

這篇文章將為大家詳細(xì)講解有關(guān)Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)中查詢出數(shù)據(jù)轉(zhuǎn)存成excel表的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

肇州網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司成立與2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司

注意日期格式如果是以String類型的方式存到數(shù)據(jù)庫(kù)的導(dǎo)出時(shí)要轉(zhuǎn)換一次,直接導(dǎo)出格式不對(duì)

因?yàn)閷?dǎo)出excel表格用的是get方式傳參,所以如果需要對(duì)導(dǎo)出的數(shù)據(jù)用中文模糊查詢,此時(shí) 用get傳參會(huì)出現(xiàn)中文亂碼

解決辦法:

前端對(duì)需要傳的中文參數(shù)進(jìn)行一次編碼 URLEncoder.encode(傳參,“utf-8”);

后臺(tái)需要再次解碼:URLDecoder.decode(接收的參數(shù),“utf-8”);

@RequestMapping(value = "outPutExcel", method = RequestMethod.GET)
@ResponseBody
public void outPutExcel( HttpServletResponse response,String officeid,
String sonid,String nameorphone,String beginTime, String endTime,String option) {
		String nString = "";
		try {
			if (nameorphone != null && nameorphone != "") {
			//對(duì)前端傳的參數(shù)解碼
				 nString = URLDecoder.decode(nameorphone,"UTF-8");
			}
		} catch (UnsupportedEncodingException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		response.reset();
		//設(shè)置瀏覽器下載的格式,并以當(dāng)前時(shí)間的毫秒數(shù)命名
		response.setHeader("Content-Disposition", "attachment;Filename=" + System.currentTimeMillis() + ".xls");
		response.setContentType("application/msexcel");
		List<PurchaseSum> list = purchaseService.selectPCSum(officeid, sonid, nString, beginTime, endTime, option);
		if (list == null && list.isEmpty()) {
			throw new NullPointerException("導(dǎo)出數(shù)據(jù)源為空");
		}
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("sheet0");
		HSSFRow rows;
		HSSFCell cells;
		//設(shè)置表格第一行的列名
		// 獲得表格第一行
		rows = sheet.createRow(0);
		// 根據(jù)需要給第一行每一列設(shè)置標(biāo)題
		cells = rows.createCell(0);
		cells.setCellValue("客戶姓名");

		cells = rows.createCell(1);
		cells.setCellValue("客戶電話");

		cells = rows.createCell(2);
		cells.setCellValue("下單日期");

		cells = rows.createCell(3);
		cells.setCellValue("訂單號(hào)");

		cells = rows.createCell(4);
		cells.setCellValue("所屬分公司");

		cells = rows.createCell(5);
		cells.setCellValue("簽單人");

		cells = rows.createCell(6);
		cells.setCellValue("品名");

		cells = rows.createCell(7);
		cells.setCellValue("型號(hào)");

		cells = rows.createCell(8);
		cells.setCellValue("顏色");

		cells = rows.createCell(9);
		cells.setCellValue("尺寸");

		cells = rows.createCell(10);
		cells.setCellValue("材質(zhì)");

		cells = rows.createCell(11);
		cells.setCellValue("已采購(gòu)數(shù)量(件)");
		
		cells = rows.createCell(12);
		cells.setCellValue("采購(gòu)單價(jià)");
		
		cells = rows.createCell(13);
		cells.setCellValue("采購(gòu)總價(jià)");
		
		cells = rows.createCell(14);
		cells.setCellValue("已出庫(kù)(件)");
		//循環(huán)數(shù)據(jù)庫(kù)查出來的數(shù)據(jù)集,對(duì)應(yīng)每一列賦值
		//此處list.size()本不應(yīng)該-1,因?yàn)橥略趌ist集合里追加了另一條數(shù)據(jù),導(dǎo)致報(bào)錯(cuò)故將其去除
		for (int i = 0; i < list.size()-1; i++) {
			rows = sheet.createRow(i + 1);
			
			cells = rows.createCell(0);
			cells.setCellValue(list.get(i).getCustomerName());

			cells = rows.createCell(1);
			cells.setCellValue(list.get(i).getPhone());
			//對(duì)日期格式進(jìn)行轉(zhuǎn)換
			cells = rows.createCell(2);
			String dateString  = list.get(i).getPlaceOrderTime().toString();
			Date date = null;
			try {
				date = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK).parse(dateString);
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			cells.setCellValue(sdf.format(date));

			cells = rows.createCell(3);
			cells.setCellValue(list.get(i).getOrderNumber());

			cells = rows.createCell(4);
			cells.setCellValue(list.get(i).getOfficeName());

			cells = rows.createCell(5);
			cells.setCellValue(list.get(i).getUsername());

			cells = rows.createCell(6);
			cells.setCellValue(list.get(i).getProductName());

			cells = rows.createCell(7);
			cells.setCellValue(list.get(i).getType());

			cells = rows.createCell(8);
			cells.setCellValue(list.get(i).getColor());

			cells = rows.createCell(9);
			cells.setCellValue(list.get(i).getSize());

			cells = rows.createCell(10);
			cells.setCellValue(list.get(i).getTexture());

			cells = rows.createCell(11);
			cells.setCellValue(list.get(i).getPurchasedNumber());

			cells = rows.createCell(12);
			cells.setCellValue(list.get(i).getPurchaseprice());
			
			cells = rows.createCell(13);
			cells.setCellValue(list.get(i).getPurchasePriceSun());
			
			cells = rows.createCell(14);
			cells.setCellValue(list.get(i).getOutlibraryNumber());
			
		}
		try {
			OutputStream oStream = response.getOutputStream();
			wb.write(oStream);
			oStream.flush();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

關(guān)于Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)中查詢出數(shù)據(jù)轉(zhuǎn)存成excel表的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

新聞標(biāo)題:Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)中查詢出數(shù)據(jù)轉(zhuǎn)存成excel表的方法
本文鏈接:http://bm7419.com/article12/ipdcgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、域名注冊(cè)網(wǎng)站內(nèi)鏈、網(wǎng)站營(yíng)銷定制開發(fā)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)