java实现xls、csv文件导出或下载
2016-06-01 11:58
656 查看
一般在java开发中需要下载xls或者是csv文件,需要在做报表或者是数据采集的时候需要,现将代码片段摘出:
还有一种情况就是将后台获取的数据以xls或者是csv格式为文件导出,方便打印或者是传阅等。
public String exportMouldCsv() { OutputStream os = null; WritableWorkbook wbook = null; try { HttpServletResponse response = ServletActionContext.getResponse(); // 取得输出流 os = response.getOutputStream(); // 清空输出流 response.reset(); // 设定输出文件头 String fileName = "文件名设置.xls"; fileName = new String(fileName.getBytes("GBK"), "iso-8859-1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); // 定义输出类型 response.setContentType("application/msexcel"); // 建立excel文件 wbook = Workbook.createWorkbook(os); WritableCellFormat cellFormat = new WritableCellFormat(); cellFormat.setAlignment(jxl.format.Alignment.CENTRE); // WritableSheet.setColumnView(int i,int width) // 作用是指定第i+1列的宽度,比如: // 将第一列的宽度设为30 // sheet.setColumnView(0,30) // wsheet.setRowView(0,10) WritableFont font1 = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat cellFormat_top = new WritableCellFormat(); cellFormat_top.setAlignment(jxl.format.Alignment.CENTRE); // 设置单元格背景颜色 // cellFormat_top.setBackground(Colour.LIGHT_BLUE); // 设置字体格式 cellFormat_top.setFont(font1); cellFormat_top.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); WritableCellFormat cellFormat_bottom = new WritableCellFormat(); cellFormat_bottom.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); WritableCellFormat cellFormat_bottom_1 = new WritableCellFormat(); // 设置居中 cellFormat_bottom_1.setAlignment(jxl.format.Alignment.CENTRE); // 设置表格边框 cellFormat_bottom_1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); WritableSheet wsheet = wbook.createSheet(" 文件名设置", 0); wsheet.setRowView(0, 300); wsheet.setRowView(1, 300); wsheet.setColumnView(0, 10); wsheet.setColumnView(1, 20); wsheet.setColumnView(2, 15); wsheet.setColumnView(3, 15); wsheet.setColumnView(4, 15); wsheet.setColumnView(5, 15); wsheet.setColumnView(6, 20); wsheet.setColumnView(7, 30); //表头设置 Label label_0 = new Label(0, 0, "组织"); label_0.setCellFormat(cellFormat_top); wsheet.addCell(label_0); Label label_1 = new Label(1, 0, "代码"); label_1.setCellFormat(cellFormat_top); wsheet.addCell(label_1); Label label_2 = new Label(2, 0, "名称"); label_2.setCellFormat(cellFormat_top); wsheet.addCell(label_2); Label label_3 = new Label(3, 0, "年"); label_3.setCellFormat(cellFormat_top); wsheet.addCell(label_3); Label label_4 = new Label(4, 0, "月"); label_4.setCellFormat(cellFormat_top); wsheet.addCell(label_4); Label label_5 = new Label(5, 0, "项"); label_5.setCellFormat(cellFormat_top); wsheet.addCell(label_5); Label label_6 = new Label(6, 0, "项名称"); label_6.setCellFormat(cellFormat_top); wsheet.addCell(label_6); Label label_7 = new Label(7, 0, "数量"); label_7.setCellFormat(cellFormat_top); wsheet.addCell(label_7); wbook.write(); } catch (Exception e) { logger.error(e); e.printStackTrace(); } finally { if (wbook != null) { try { wbook.close(); } catch (Exception e) { logger.error(e); } wbook = null; } if (os != null) { try { os.close(); } catch (Exception e) { logger.error(e); } os = null; } } }
还有一种情况就是将后台获取的数据以xls或者是csv格式为文件导出,方便打印或者是传阅等。
public String exportMouldCsv() { OutputStream os = null; WritableWorkbook wbook = null; try { HttpServletResponse response = ServletActionContext.getResponse(); // 取得输出流 os = response.getOutputStream(); // 清空输出流 response.reset(); // 设定输出文件头 String fileName = "文件名设置.xls"; fileName = new String(fileName.getBytes("GBK"), "iso-8859-1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); // 定义输出类型 response.setContentType("application/msexcel"); // 建立excel文件 wbook = Workbook.createWorkbook(os); WritableCellFormat cellFormat = new WritableCellFormat(); cellFormat.setAlignment(jxl.format.Alignment.CENTRE); // WritableSheet.setColumnView(int i,int width) // 作用是指定第i+1列的宽度,比如: // 将第一列的宽度设为30 // sheet.setColumnView(0,30) // wsheet.setRowView(0,10) WritableFont font1 = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat cellFormat_top = new WritableCellFormat(); cellFormat_top.setAlignment(jxl.format.Alignment.CENTRE); // 设置单元格背景颜色 // cellFormat_top.setBackground(Colour.LIGHT_BLUE); // 设置字体格式 cellFormat_top.setFont(font1); cellFormat_top.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); WritableCellFormat cellFormat_bottom = new WritableCellFormat(); cellFormat_bottom.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); WritableCellFormat cellFormat_bottom_1 = new WritableCellFormat(); // 设置居中 cellFormat_bottom_1.setAlignment(jxl.format.Alignment.CENTRE); // 设置表格边框 cellFormat_bottom_1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); WritableSheet wsheet = wbook.createSheet(" 文件名设置", 0); wsheet.setRowView(0, 300); wsheet.setRowView(1, 300); wsheet.setColumnView(0, 10); wsheet.setColumnView(1, 20); wsheet.setColumnView(2, 15); wsheet.setColumnView(3, 15); wsheet.setColumnView(4, 15); wsheet.setColumnView(5, 15); wsheet.setColumnView(6, 20); wsheet.setColumnView(7, 30); //表头设置 Label label_0 = new Label(0, 0, "组织"); label_0.setCellFormat(cellFormat_top); wsheet.addCell(label_0); Label label_1 = new Label(1, 0, "代码"); label_1.setCellFormat(cellFormat_top); wsheet.addCell(label_1); Label label_2 = new Label(2, 0, "名称"); label_2.setCellFormat(cellFormat_top); wsheet.addCell(label_2); Label label_3 = new Label(3, 0, "年"); label_3.setCellFormat(cellFormat_top); wsheet.addCell(label_3); Label label_4 = new Label(4, 0, "月"); label_4.setCellFormat(cellFormat_top); wsheet.addCell(label_4); Label label_5 = new Label(5, 0, "项"); label_5.setCellFormat(cellFormat_top); wsheet.addCell(label_5); Label label_6 = new Label(6, 0, "项名称"); label_6.setCellFormat(cellFormat_top); wsheet.addCell(label_6); Label label_7 = new Label(7, 0, "数量"); label_7.setCellFormat(cellFormat_top); wsheet.addCell(label_7); //获取从后台获取数据,并赋值给对应的列值 MList mList = goalService.getMaterielViewList(mat); if (null!=mList ) { for (int i = 0; i < matList.size(); i++) { Materiel materiel=new Materiel(); materiel=matList.get(i); Label label_00 = new Label(0, (i+1), materiel.getMvgr1()==null?"":materiel.getMvgr1()); label_00.setCellFormat(cellFormat_bottom_1); wsheet.addCell(label_00); Label label_01 = new Label(1, (i+1), materiel.getBezei()==null?"":materiel.getBezei()); label_01.setCellFormat(cellFormat_bottom_left); wsheet.addCell(label_01); ..... //后面是自己来咯!! } } wbook.write(); } catch (Exception e) { logger.error(e); e.printStackTrace(); } finally { if (wbook != null) { try { wbook.close(); } catch (Exception e) { logger.error(e); } wbook = null; } if (os != null) { try { os.close(); } catch (Exception e) { logger.error(e); } os = null; } } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树