当前位置:   article > 正文

POI 3.17使用笔记_poi-3.17 maven

poi-3.17 maven

1.新建springboot项目

2.导入POI相关maven依赖

  1. <!--poi-->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>3.17</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.poi</groupId>
  9. <artifactId>poi-ooxml</artifactId>
  10. <version>3.17</version>
  11. </dependency>
  12. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
  13. <dependency>
  14. <groupId>org.apache.poi</groupId>
  15. <artifactId>poi-ooxml-schemas</artifactId>
  16. <version>3.17</version>
  17. </dependency>

 

3.导出excel(excel的创建,单元格字体样式,单元格宽高)

  1. package lucene;
  2. import org.apache.poi.hssf.usermodel.*;
  3. import org.apache.poi.ss.usermodel.*;
  4. import org.apache.poi.ss.util.CellRangeAddress;
  5. import java.io.FileOutputStream;
  6. /**
  7. * lucene全文检索测试类
  8. * Created by Administrator on 2018/7/10.
  9. */
  10. public class LuceneTestController {
  11. //poi导出excel
  12. public void exportExcel() throws Exception{
  13. HSSFWorkbook wb = new HSSFWorkbook();
  14. HSSFSheet sheet = wb.createSheet("sheet name");
  15. //合并单元格
  16. sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 5));
  17. //设置第一列单元格宽度
  18. sheet.setColumnWidth(0,100*100);
  19. //设置第二列单元格宽度
  20. sheet.setColumnWidth(1,100*100);
  21. //创建第一行
  22. HSSFRow row0 = sheet.createRow(0);
  23. //创建第二行
  24. HSSFRow row1 = sheet.createRow(1);
  25. //设置第一行单元格高度
  26. row0.setHeight((short) 400);
  27. //创建第一行第一列单元格
  28. HSSFCell cell0_1 = row0.createCell(0);
  29. //创建第二行第一列单元格
  30. HSSFCell cell0_2 = row1.createCell(0);
  31. //设置单元格的值
  32. cell0_1.setCellValue("项目施工进度管理系统");
  33. //改变字体样式,步骤
  34. HSSFFont hssfFont = wb.createFont();
  35. //设置字体,红色
  36. hssfFont.setColor(HSSFFont.COLOR_RED);
  37. //字体粗体显示
  38. hssfFont.setBold(true);
  39. hssfFont.setFontName("宋体");
  40. // 字体大小
  41. hssfFont.setFontHeightInPoints((short) 22);
  42. //设置样式
  43. HSSFCellStyle cellStyle = wb.createCellStyle();
  44. cellStyle.setFont(hssfFont);
  45. //设置单元格背景色
  46. cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
  47. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  48. //设置居中
  49. cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
  50. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
  51. //设置边框
  52. cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
  53. cellStyle.setBorderLeft(BorderStyle.DASH_DOT_DOT);//左边框
  54. cellStyle.setBorderTop(BorderStyle.THIN);//上边框
  55. cellStyle.setBorderRight(BorderStyle.THIN);//右边框
  56. //3.单元格使用样式,设置第一行第一列单元格样式
  57. cell0_1.setCellStyle(cellStyle);
  58. cell0_2.setCellStyle(cellStyle);
  59. //生成excel文件
  60. FileOutputStream fileOut = new FileOutputStream("e:\\"+System.currentTimeMillis()+".xls");
  61. wb.write(fileOut);
  62. fileOut.close();
  63. /* struts导出excel,前端需要是提交form形式,否则,点击导出不会弹出框
  64. HSSFWorkbook wb = exportExcel(projectId, blockId, buildingId,buildingCode);
  65. // 生成excel文件
  66. String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis()).concat(".xls");
  67. // 清空response
  68. this.getResponse().reset();
  69. this.getResponse().addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes()));
  70. this.getResponse().setContentType("application/vnd.ms-excel;charset=utf-8");
  71. OutputStream os = this.getResponse().getOutputStream();
  72. wb.write(os);
  73. if (os != null) {
  74. os.close();
  75. os = null;
  76. }
  77. wb.close();
  78. */
  79. }
  80. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/170159
推荐阅读
相关标签
  

闽ICP备14008679号