必威体育Betway必威体育官网
当前位置:首页 > IT技术

java实现下载excle(jxl、poi 两种方式)

时间:2019-10-04 13:42:16来源:IT技术作者:seo实验室小编阅读:70次「手机版」
 

excle下载

  1. @requestMAPPing(value="/download_index")

  2. public String downloadexcel(HttpServletRequest request,HttpServletresponse response,String name) {

  3. outputstream os = null;

  4. WritableWorkbook wbook = null;

  5. try {

  6. String flieName = name;

  7. // 取得输出流

  8. os = response.getOutputStream();

  9. response.reset();// 清空输出流

  10. // 设定输出文件头

  11. response.setheader("content-Disposition", "attachment; filename=" + new String(flieName.getBytes("UTF-8"), "ISO8859-1") + ".xls");

  12. // 定义输出类型

  13. response.setcontenttype("application/msexcel;charset=utf-8");

  14. // 建立excel文件

  15. wbook = Workbook.createWorkbook(os);

  16. //要下载的list数据

  17. List<UnifiedVO> unifiedVOList = new ArrayList<UnifiedVO>();

  18. // 设置数据

  19. setWorkBookData(unifiedVOList, wbook,flieName);

  20. if (wbook.getNumberOfSheets() > 0) {

  21. wbook.write();

  22. }

  23. wbook.close();

  24. os.close();

  25. } catch (Exception e) {

  26. e.printstacktrace();

  27. }finally {

  28. if(wbook != null) {

  29. try {

  30. wbook.close();

  31. } catch (WriteException | IOException e) {

  32. e.printStackTrace();

  33. }

  34. }

  35. if(os != null) {

  36. try {

  37. os.close();

  38. } catch (IOException e) {

  39. e.printStackTrace();

  40. }

  41. }

  42. }

  43. return null;

  44. }

String [] codes 是页面传过来的要查询的内容,name 是传过来动态表名。for循环查出List数据,然后统一用unifiedVOList.addAll()合在一起。然后将list数据集合传入方法内、work、表名传入方法内。

  1. private void setWorkBookData(List<UnifiedVO> giftRecords, WritableWorkbook wbook,String name)

  2. throws WriteException, RowsExceededException {

  3. final integer EXCEL_MAX_NUMBER = 65535;

  4. WritableCellformat wc = new WritableCellFormat();

  5. // 设置居中

  6. wc.setAlignment(Alignment.CENTRE);

  7. simpledateformat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  8. WritableSheet wsheet = null;

  9. int sheetNumber = 0;

  10. if (giftRecords.size() % EXCEL_MAX_NUMBER != 0) {

  11. sheetNumber = giftRecords.size() / EXCEL_MAX_NUMBER + 1;

  12. } else {

  13. sheetNumber = giftRecords.size() / EXCEL_MAX_NUMBER;

  14. }

  15. for (int j = 0; j < sheetNumber; j++) {

  16. wsheet = wbook.createSheet(name + (j + 1), j); // sheet名称

  17. // 开始生成主体内容

  18. wsheet.addCell(new Label(0, 0, "名称", wc));

  19. wsheet.setColumnView(0, 32);

  20. wsheet.addCell(new Label(1, 0, "时间", wc));

  21. wsheet.setColumnView(1, 20);

  22. wsheet.addCell(new Label(2, 0, "数据", wc));

  23. wsheet.setColumnView(2, 32);

  24. wsheet.addCell(new Label(3, 0, "单位", wc));

  25. wsheet.setColumnView(3, 30);

  26. int start = EXCEL_MAX_NUMBER * j;

  27. int end = EXCEL_MAX_NUMBER + start;

  28. for (int i = start, k = giftRecords.size(); i < k && i < end; i++) {

  29. UnifiedVO unifiedVO = giftRecords.get(i);

  30. int row = i - start + 1;

  31. wsheet.addCell(new Label(0, row, unifiedVO .getKey(), wc));

  32. wsheet.addCell(new Label(1, row, unifiedVO .getDate(), wc));

  33. wsheet.addCell(new Label(2, row, unifiedVO .getData(), wc));

  34. wsheet.addCell(new Label(3, row, unifiedVO .getUnit(), wc));

  35. }

  36. }

  37. }

wsheet.addCell(new Label(0, 0, "名称", wc))

wsheet.addCell(new Label(1, 0, "时间", wc));

wsheet.addCell(new Label(2, 0, "数据", wc));

wsheet.addCell(new Label(3, 0, "单位", wc));

上面的字段一一对应VO字段

wsheet.addCell(new Label(0, row, unifiedVO .getKey(), wc));

wsheet.addCell(new Label(1, row, unifiedVO .getDate(), wc));

wsheet.addCell(new Label(2, row, unifiedVO .getData(), wc));

wsheet.addCell(new Label(3, row, unifiedVO .getUnit(), wc));

对应的maven API :

  1. <!-- 导出excel jxl -->

  2. <dependency>

  3. <groupId>net.sourceforge.jexcelapi</groupId>

  4. <artifactId>jxl</artifactId>

  5. <version>2.6.12</version>

  6. </dependency>

=====================================================================================

poi 导出:

  1. <!-- 导出excel poi -->

  2. <dependency>

  3. <groupId>org.apache.poi</groupId>

  4. <artifactId>poi</artifactId>

  5. <version>3.17</version>

  6. </dependency>

最简单的下载数据实现:

  1. public test(HttpServletRequest request,HttpServletResponse response) {

  2. List<?> arrayList = new ArrayList<Object>();

  3. OutputStream os = null;

  4. // 取得输出流

  5. try {

  6. os = response.getOutputStream();

  7. } catch (IOException e2) {

  8. e2.printStackTrace();

  9. }

  10. response.reset();// 清空输出流

  11. //1.在内存中创建一个excel文件

  12. HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

  13. //2.创建工作簿

  14. HSSFSheet sheet = hssfWorkbook.createSheet();

  15. //3.创建标题

  16. HSSFRow titlerRow = sheet.createRow(0);

  17. HSSFCellStyle createCellStyle = hssfWorkbook.createCellStyle();//创建单元格样式

  18. createCellStyle.setFillForegroundcolor((short) 255);// 设置背景色

  19. createCellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION); // 居中

  20. createCellStyle.setBorderTop(BorderStyle.SLANTED_DASH_DOT);

  21. titlerRow.createCell(0).setCellValue("营销顾问");

  22. titlerRow.setRowStyle(createCellStyle);

  23. for (Object o : arrayList) {

  24. //获取最后一行的行号

  25. int lastrownum = sheet.getLastRowNum();

  26. HSSFRow dataRow = sheet.createRow(lastRowNum + 1);

  27. dataRow.createCell(0).setCellValue(o.get营销顾问());

  28. }

  29. //5.创建文件名

  30. String fileName = "区域数据统计.xls";

  31. //9.设置信息头

  32. response.setContentType(fileName);

  33. try {

  34. fileName = new String (fileName.getBytes ("utf-8"),"ISO8859-1");

  35. } catch (UnsupportedEncodingException e2) {

  36. e2.printStackTrace();

  37. }

  38. response.setHeader("Content-Disposition","attachment;filename="+fileName);

  39. //10.写出文件,关闭流

  40. try {

  41. hssfWorkbook.write(os);

  42. } catch (IOException e1) {

  43. }

  44. try {

  45. hssfWorkbook.close();

  46. } catch (IOException e) {

  47. e.printStackTrace();

  48. }

  49. }

相关阅读

JDK 1.6.0_41 下载

Java SE Development Kit 6u41 Product / File Description File Size Download password Linux x86 65.43 MB jdk-6u41-

farpoint使用

注:(fpSpread1为farpoint名,fpSpread1_Sheet1为表名) 用farpoint的时,设置视区随滚动条移动(鼠标按着,farpoint的内容更新到滚动条的区域

Java自动化测试常用的工具代码

1:简单的截屏——截全屏 package com.auto.Test; import java.awt.Dimension; import java.awt.Rectangle;

Java集合

关于java集合类Collections.在前端开发中可能用到的比较少,不过java后台用到的应该挺多,比较后台主要是基于数据库的读写的,对于并发

atomic的实现原理

 在多线程的场景中,我们需要保证数据安全,就会考虑同步的方案,通常会使用synchronized或者lock来处理,使用了synchronized意味着内核

分享到:

栏目导航

推荐阅读

热门阅读