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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

生成excel表格并下载

發布時間:2025/3/19 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 生成excel表格并下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工作需要生成excel并下載。

jsp:<a href="${ctx}/device/dvbg/outExcelbg" class="data_input_bg">導出</a>

controler:

/*

* 生成excel

*/

@RequestMapping(value = "/outExcelbg", method = RequestMethod.GET)

public ModelAndView outExcelbg(HttpServletRequest request,HttpServletResponse response) throws IOException{

JSONObject ret=new JSONObject();

List<Dvbgdata> list=bgService.getBgdata((Ps) getSession().getAttribute(CURRENT_USER));

ExcelUtil.outExcelbg(request.getRealPath("/upload").toString(),list);

response.setContentType("text/html;charset=utf-8"); ?

? ? ? ?request.setCharacterEncoding("UTF-8"); ?

? ? ? ?java.io.BufferedInputStream bis = null; ?

? ? ? ?java.io.BufferedOutputStream bos = null; ?

?

? ? ? ?String ctxPath = request.getSession().getServletContext().getRealPath("/upload")

? ? ? ? ? ? ? ?+ "\\" + "xls\\"; ?

? ? ? ?String downLoadPath = ctxPath + "bg_temp.xls"; ?

? ? ? ?System.out.println(downLoadPath); ?

? ? ? ?try { ?

? ? ? ? ? ?long fileLength = new File(downLoadPath).length(); ?

? ? ? ? ? ?response.setContentType("application/x-msdownload;"); ?

? ? ? ? ? ?response.setHeader("Content-disposition", "attachment; filename=" ?

? ? ? ? ? ? ? ? ? ?+ new String("bg_temp.xls".getBytes("utf-8"), "ISO8859-1")); ?

? ? ? ? ? ?response.setHeader("Content-Length", String.valueOf(fileLength)); ?

? ? ? ? ? ?bis = new BufferedInputStream(new FileInputStream(downLoadPath)); ?

? ? ? ? ? ?bos = new BufferedOutputStream(response.getOutputStream()); ?

? ? ? ? ? ?byte[] buff = new byte[2048]; ?

? ? ? ? ? ?int bytesRead; ?

? ? ? ? ? ?while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { ?

? ? ? ? ? ? ? ?bos.write(buff, 0, bytesRead); ?

? ? ? ? ? ?} ?

? ? ? ?} catch (Exception e) { ?

? ? ? ? ? ?e.printStackTrace(); ?

? ? ? ?} finally { ?

? ? ? ? ? ?if (bis != null) ?

? ? ? ? ? ? ? ?bis.close(); ?

? ? ? ? ? ?if (bos != null) ?

? ? ? ? ? ? ? ?bos.close(); ?

? ? ? ?} ?

return null;

}

util:

public static void outExcelbg(String pathString,List<Dvbgdata> bgdatas){

try{

? ? ? ? WritableWorkbook book ?= ?Workbook.createWorkbook(new ?File(pathString+"/xls/bg_temp.xls"));

? ? ? ? // ?生成名為“第一頁”的工作表,參數0表示這是第一頁?

? ? ? ? WritableSheet sheet ?= ?book.createSheet( "血糖表" , ?0 );

? ? ? ? // ?在Label對象的構造子中指名單元格位置是第一列第一行(0,0)

? ? ? ? WritableCell cell=null;

? ? ? ? // ?將定義好的單元格添加到工作表中?

? ? ? ? Label label1=new Label(0,0,"時間");

? ? ? ? Label label2=new Label(1,0,"血糖(mmol/l)");

? ? ? ? Label label3=new Label(2,0,"設備號");

? ? ? ? Label label4=new Label(3,0,"詳情");

? ? ? ? sheet.addCell(label1);

? ? ? ? sheet.addCell(label2);

? ? ? ? sheet.addCell(label3);

? ? ? ? sheet.addCell(label4);

? ? ? ? int length=bgdatas.size();

? ? ? ? int i=1;

? ? ? ? for(Dvbgdata dvbgdata : bgdatas){

? ? ? ??? ?Label label01=new Label(0, i, DateUtil.dateToString(dvbgdata.getMeterdate()));

? ? ? ??Label label02=new Label(1, i, dvbgdata.getBg().toString());

? ? ? ??Label label03=new Label(2, i, dvbgdata.getDevicesn());

? ? ? ??Label label04=new Label(3, i,WarningsUtil.getBg(Double.parseDouble(dvbgdata.getBg().toString())));

? ? ? ??sheet.addCell(label01);

? ? ? ??sheet.addCell(label02);

? ? ? ??sheet.addCell(label03);

? ? ? ??sheet.addCell(label04);

? ? ? ??System.out.println(dvbgdata.getBg());

? ? ? ??i++;

? ? ? ??if(i>length){

? ? ? ??break;

? ? ? ??}

? ? ? ? }

? ? ? ?

? ? ? ? // ?寫入數據并關閉文件?

? ? ? ?book.write();

? ? ? ?book.close();


? ?}catch(Exception e) ?{

? ? ? ?System.out.println(e);

? ?}?

}


總結

以上是生活随笔為你收集整理的生成excel表格并下载的全部內容,希望文章能夠幫你解決所遇到的問題。

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