当前位置:   article > 正文

使用easyexcel操作Excel_easy excel 设置单元格禁止输入

easy excel 设置单元格禁止输入

我也是凑巧用上了,就在这里分享一下,性能确实是没得说!

easyexcel的介绍是:Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到KB级别,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式。在上层做了模型转换的封装,让使用者更加简单方便 

1、导入的pom.xml

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>easyexcel</artifactId>
  4. <version>1.0.3</version>
  5. </dependency>

实际上版本(1.0.1)在读取的时候是有BUG的,读取03版的.xls格式的excel正常,但是读取07版的.xlsx版的excel就会出异常,原因是在解析的时候sheet临时文件路径拼装有误,所以我们用1.0.3

2、使用easyexcel读取excel的时候需要设置excel的版本,但是有些时候我们无法预知excel的版本,所以在网上copy了大神模仿poi写了一个用于获取com.alibaba.excel.ExcelReader对象的工具类

  1. package easyExcel.com.easy.excel;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.PushbackInputStream;
  5. import org.apache.poi.EmptyFileException;
  6. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  7. import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
  8. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  9. import org.apache.poi.util.IOUtils;
  10. import com.alibaba.excel.ExcelReader;
  11. import com.alibaba.excel.read.context.AnalysisContext;
  12. import com.alibaba.excel.read.event.AnalysisEventListener;
  13. import com.alibaba.excel.support.ExcelTypeEnum;
  14. public class ExcelReaderFactory {
  15. /**
  16. * @param in
  17. * 文件输入流
  18. * @param customContent
  19. * 自定义模型可以在
  20. * {@link AnalysisEventListener#invoke(Object, AnalysisContext) }
  21. * AnalysisContext中获取用于监听者回调使用
  22. * @param eventListener
  23. * 用户监听
  24. * @throws IOException
  25. * @throws EmptyFileException
  26. * @throws InvalidFormatException
  27. */
  28. public static ExcelReader getExcelReader(InputStream in, Object customContent,
  29. AnalysisEventListener<?> eventListener) throws EmptyFileException, IOException, InvalidFormatException {
  30. // 如果输入流不支持mark/reset,需要对其进行包裹
  31. if (!in.markSupported()) {
  32. in = new PushbackInputStream(in, 8);
  33. }
  34. // 确保至少有一些数据
  35. byte[] header8 = IOUtils.peekFirst8Bytes(in);
  36. ExcelTypeEnum excelTypeEnum = null;
  37. if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
  38. excelTypeEnum = ExcelTypeEnum.XLS;
  39. }
  40. if (DocumentFactoryHelper.hasOOXMLHeader(in)) {
  41. excelTypeEnum = ExcelTypeEnum.XLSX;
  42. }
  43. if (excelTypeEnum != null) {
  44. return new ExcelReader(in, excelTypeEnum, customContent, eventListener);
  45. }
  46. throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
  47. }
  48. /**
  49. * @param in
  50. * 文件输入流
  51. * @param customContent
  52. * 自定义模型可以在
  53. * {@link AnalysisEventListener#invoke(Object, AnalysisContext) }
  54. * AnalysisContext中获取用于监听者回调使用
  55. * @param eventListener
  56. * 用户监听
  57. * @param trim
  58. * 是否对解析的String做trim()默认true,用于防止 excel中空格引起的装换报错。
  59. * @throws IOException
  60. * @throws EmptyFileException
  61. * @throws InvalidFormatException
  62. */
  63. public static ExcelReader getExcelReader(InputStream in, Object customContent,
  64. AnalysisEventListener<?> eventListener, boolean trim)
  65. throws EmptyFileException, IOException, InvalidFormatException {
  66. // 如果输入流不支持mark/reset,需要对其进行包裹
  67. if (!in.markSupported()) {
  68. in = new PushbackInputStream(in, 8);
  69. }
  70. // 确保至少有一些数据
  71. byte[] header8 = IOUtils.peekFirst8Bytes(in);
  72. ExcelTypeEnum excelTypeEnum = null;
  73. if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
  74. excelTypeEnum = ExcelTypeEnum.XLS;
  75. }
  76. if (DocumentFactoryHelper.hasOOXMLHeader(in)) {
  77. excelTypeEnum = ExcelTypeEnum.XLSX;
  78. }
  79. if (excelTypeEnum != null) {
  80. return new ExcelReader(in, excelTypeEnum, customContent, eventListener, trim);
  81. }
  82. throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
  83. }
  84. }

3、然后编写一个简单的测试类

  1. package easyExcel.com.easy.excel;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import org.junit.Test;
  5. import com.alibaba.excel.ExcelReader;
  6. import com.alibaba.excel.annotation.ExcelProperty;
  7. import com.alibaba.excel.metadata.BaseRowModel;
  8. import com.alibaba.excel.metadata.Sheet;
  9. import com.alibaba.excel.read.context.AnalysisContext;
  10. import com.alibaba.excel.read.event.AnalysisEventListener;
  11. public class ExcelWriteTest {
  12. public static class ExcelPropertyIndexModel extends BaseRowModel {
  13. @ExcelProperty(value = "姓名", index = 0)
  14. private String name;
  15. @ExcelProperty(value = "年龄", index = 1)
  16. private String age;
  17. @ExcelProperty(value = "邮箱", index = 2)
  18. private String email;
  19. @ExcelProperty(value = "地址", index = 3)
  20. private String address;
  21. @ExcelProperty(value = "性别", index = 4)
  22. private String sax;
  23. @ExcelProperty(value = "高度", index = 5)
  24. private String heigh;
  25. @ExcelProperty(value = "备注", index = 6)
  26. private String last;
  27. public String getName() {
  28. return name;
  29. }
  30. public void setName(String name) {
  31. this.name = name;
  32. }
  33. public String getAge() {
  34. return age;
  35. }
  36. public void setAge(String age) {
  37. this.age = age;
  38. }
  39. public String getEmail() {
  40. return email;
  41. }
  42. public void setEmail(String email) {
  43. this.email = email;
  44. }
  45. public String getAddress() {
  46. return address;
  47. }
  48. public void setAddress(String address) {
  49. this.address = address;
  50. }
  51. public String getSax() {
  52. return sax;
  53. }
  54. public void setSax(String sax) {
  55. this.sax = sax;
  56. }
  57. public String getHeigh() {
  58. return heigh;
  59. }
  60. public void setHeigh(String heigh) {
  61. this.heigh = heigh;
  62. }
  63. public String getLast() {
  64. return last;
  65. }
  66. public void setLast(String last) {
  67. this.last = last;
  68. }
  69. @Override
  70. public String toString() {
  71. return "ExcelPropertyIndexModel [name=" + name + ", age=" + age + ", email=" + email + ", address="
  72. + address + ", sax=" + sax + ", heigh=" + heigh + ", last=" + last + "]";
  73. }
  74. }
  75. @Test
  76. public void read() throws Exception {
  77. //text.xlsx
  78. try (InputStream in = new FileInputStream("try.xlsx");) {
  79. AnalysisEventListener<ExcelPropertyIndexModel> listener = new AnalysisEventListener<ExcelPropertyIndexModel>() {
  80. @Override
  81. public void invoke(ExcelPropertyIndexModelobject, AnalysisContext context) {
  82. System.err.println("Row:" + context.getCurrentRowNum() + " Data:"
  83. + object.toString());
  84. }
  85. @Override
  86. public void doAfterAllAnalysed(AnalysisContext context) {
  87. System.err.println("doAfterAllAnalysed...");
  88. }
  89. };
  90. ExcelReader excelReader = ExcelReaderFactory.getExcelReader(in, null, listener);
  91. // 第二个参数为表头行数,按照实际设置
  92. excelReader.read(new Sheet(1, 1, ExcelPropertyIndexModel.class));
  93. }
  94. }
  95. }

解释一下测试类的代码:ExcelPropertyIndexModel是一个用来存放解析完表格的数据类,@ExcelProperty(value = "名称", index = 10)注解中的index标识着这个参数对应的列,index从0开始

 

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

闽ICP备14008679号