浏览代码

扩展ExcelReader,增加单独获取列名的方法

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 2 年之前
父节点
当前提交
068b66b615

+ 9 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/ExcelReader.java

@@ -95,4 +95,13 @@ public class ExcelReader {
         parser.parse(ins, new DataMapHandler(columnNameRow, listener));
     }
 
+    /**
+     * 获取列名数组,不包含列名的情况下可能返回空
+     *
+     * @return
+     */
+    public String[] getColumnNames() {
+        return this.parser.getColumnNames();
+    }
+
 }

+ 2 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/handler/RowDataHandler.java

@@ -5,4 +5,6 @@ import com.qmth.boot.tools.excel.model.RowData;
 public interface RowDataHandler {
 
     void handle(RowData rowData);
+
+    String[] getColumnNames();
 }

+ 5 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/handler/impl/DataMapHandler.java

@@ -27,6 +27,11 @@ public class DataMapHandler implements RowDataHandler {
         }
     }
 
+    @Override
+    public String[] getColumnNames() {
+        return columnNames;
+    }
+
     private void buildColumnNames(RowData rowData) {
         columnNames = rowData.getValues();
     }

+ 5 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/handler/impl/ObjectHandler.java

@@ -29,6 +29,11 @@ public class ObjectHandler<E> implements RowDataHandler {
         this.columnNameRow = columnNameRow;
     }
 
+    @Override
+    public String[] getColumnNames() {
+        return columnNames;
+    }
+
     @Override
     public void handle(RowData rowData) {
         if (rowData.getRow() == columnNameRow && columnNames == null) {

+ 1 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/parser/ExcelParser.java

@@ -8,4 +8,5 @@ public interface ExcelParser {
 
     void parse(InputStream ins, RowDataHandler handler) throws Exception;
 
+    String[] getColumnNames();
 }

+ 5 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/parser/impl/XlsParser.java

@@ -34,6 +34,11 @@ public class XlsParser implements HSSFListener, ExcelParser {
         factory.processWorkbookEvents(request, new POIFSFileSystem(inputStream));
     }
 
+    @Override
+    public String[] getColumnNames() {
+        return handler.getColumnNames();
+    }
+
     @Override
     public void processRecord(Record record) {
         switch (record.getSid()) {

+ 8 - 1
tools-poi/src/main/java/com/qmth/boot/tools/excel/parser/impl/XlsxParser.java

@@ -15,11 +15,13 @@ import java.io.InputStream;
 
 public class XlsxParser implements ExcelParser {
 
+    private XlsxSheetContentHandler contentHandler;
+
     public void parse(InputStream inputStream, RowDataHandler handler) throws Exception {
         OPCPackage pkg = OPCPackage.open(inputStream);
         XSSFReader reader = new XSSFReader(pkg);
 
-        XlsxSheetContentHandler contentHandler = new XlsxSheetContentHandler(handler);
+        this.contentHandler = new XlsxSheetContentHandler(handler);
         XMLReader parser = XMLHelper.newXMLReader();
         parser.setContentHandler(
                 new XSSFSheetXMLHandler(reader.getStylesTable(), new ReadOnlySharedStringsTable(pkg), contentHandler,
@@ -33,4 +35,9 @@ public class XlsxParser implements ExcelParser {
         }
     }
 
+    @Override
+    public String[] getColumnNames() {
+        return contentHandler.getColumnNames();
+    }
+
 }

+ 4 - 0
tools-poi/src/main/java/com/qmth/boot/tools/excel/parser/impl/XlsxSheetContentHandler.java

@@ -39,4 +39,8 @@ public class XlsxSheetContentHandler implements XSSFSheetXMLHandler.SheetContent
         } catch (Exception e) {
         }
     }
+
+    public String[] getColumnNames() {
+        return handler.getColumnNames();
+    }
 }