mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
3 years ago
11 changed files with 268 additions and 161 deletions
@ -0,0 +1,112 @@
|
||||
package com.alibaba.excel.analysis.csv; |
||||
|
||||
import java.io.FileReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.util.ArrayList; |
||||
import java.util.Iterator; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.analysis.ExcelReadExecutor; |
||||
import com.alibaba.excel.context.csv.CsvReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.enums.RowTypeEnum; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.metadata.Cell; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; |
||||
import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; |
||||
import com.alibaba.excel.util.SheetUtils; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.collections4.MapUtils; |
||||
import org.apache.commons.csv.CSVFormat; |
||||
import org.apache.commons.csv.CSVRecord; |
||||
|
||||
/** |
||||
* azz |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Slf4j |
||||
public class CsvExcelReadExecutor implements ExcelReadExecutor { |
||||
|
||||
private List<ReadSheet> sheetList; |
||||
private CsvReadContext csvReadContext; |
||||
|
||||
public CsvExcelReadExecutor(CsvReadContext csvReadContext) { |
||||
this.csvReadContext = csvReadContext; |
||||
sheetList = new ArrayList<>(); |
||||
ReadSheet readSheet = new ReadSheet(); |
||||
sheetList.add(readSheet); |
||||
readSheet.setSheetNo(0); |
||||
} |
||||
|
||||
@Override |
||||
public List<ReadSheet> sheetList() { |
||||
return sheetList; |
||||
} |
||||
|
||||
@Override |
||||
public void execute() { |
||||
Iterable<CSVRecord> parseRecords; |
||||
try { |
||||
parseRecords = parseRecords(); |
||||
} catch (IOException e) { |
||||
throw new ExcelAnalysisException(e); |
||||
} |
||||
for (ReadSheet readSheet : sheetList) { |
||||
readSheet = SheetUtils.match(readSheet, csvReadContext); |
||||
if (readSheet == null) { |
||||
continue; |
||||
} |
||||
csvReadContext.currentSheet(readSheet); |
||||
|
||||
int rowIndex = 0; |
||||
for (CSVRecord record : parseRecords) { |
||||
|
||||
dealRecord(record, rowIndex++); |
||||
} |
||||
// The last sheet is read
|
||||
csvReadContext.analysisEventProcessor().endSheet(csvReadContext); |
||||
} |
||||
} |
||||
|
||||
private Iterable<CSVRecord> parseRecords() throws IOException { |
||||
CsvReadWorkbookHolder csvReadWorkbookHolder = csvReadContext.csvReadWorkbookHolder(); |
||||
CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat(); |
||||
|
||||
if (csvReadWorkbookHolder.getMandatoryUseInputStream()) { |
||||
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream())); |
||||
} |
||||
if (csvReadWorkbookHolder.getFile() != null) { |
||||
return csvFormat.parse(new FileReader(csvReadWorkbookHolder.getFile())); |
||||
} |
||||
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream())); |
||||
} |
||||
|
||||
private void dealRecord(CSVRecord record, int rowIndex) { |
||||
Map<Integer, Cell> cellMap = new LinkedHashMap<>(); |
||||
Iterator<String> cellIterator = record.iterator(); |
||||
int cellIndex = 0; |
||||
while (cellIterator.hasNext()) { |
||||
String cellString = cellIterator.next(); |
||||
ReadCellData<String> readCellData = new ReadCellData<>(); |
||||
readCellData.setType(CellDataTypeEnum.STRING); |
||||
readCellData.setStringValue(cellString); |
||||
cellMap.put(cellIndex++, readCellData); |
||||
} |
||||
|
||||
RowTypeEnum rowType = MapUtils.isEmpty(cellMap) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA; |
||||
ReadRowHolder readRowHolder = new ReadRowHolder(rowIndex, rowType, |
||||
csvReadContext.readWorkbookHolder().getGlobalConfiguration(), cellMap); |
||||
csvReadContext.readRowHolder(readRowHolder); |
||||
|
||||
csvReadContext.csvReadSheetHolder().setCellMap(cellMap); |
||||
csvReadContext.csvReadSheetHolder().setRowIndex(rowIndex); |
||||
csvReadContext.analysisEventProcessor().endRow(csvReadContext); |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
package com.alibaba.excel.context.csv; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder; |
||||
import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; |
||||
|
||||
/** |
||||
* A context is the main anchorage point of a ls xls reader. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public interface CsvReadContext extends AnalysisContext { |
||||
/** |
||||
* All information about the workbook you are currently working on. |
||||
* |
||||
* @return Current workbook holder |
||||
*/ |
||||
CsvReadWorkbookHolder csvReadWorkbookHolder(); |
||||
|
||||
/** |
||||
* All information about the sheet you are currently working on. |
||||
* |
||||
* @return Current sheet holder |
||||
*/ |
||||
CsvReadSheetHolder csvReadSheetHolder(); |
||||
} |
@ -0,0 +1,29 @@
|
||||
package com.alibaba.excel.context.csv; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContextImpl; |
||||
import com.alibaba.excel.read.metadata.ReadWorkbook; |
||||
import com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder; |
||||
import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
||||
/** |
||||
* A context is the main anchorage point of a ls xls reader. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class DefaultCsvReadContext extends AnalysisContextImpl implements CsvReadContext { |
||||
|
||||
public DefaultCsvReadContext(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) { |
||||
super(readWorkbook, actualExcelType); |
||||
} |
||||
|
||||
@Override |
||||
public CsvReadWorkbookHolder csvReadWorkbookHolder() { |
||||
return (CsvReadWorkbookHolder)readWorkbookHolder(); |
||||
} |
||||
|
||||
@Override |
||||
public CsvReadSheetHolder csvReadSheetHolder() { |
||||
return (CsvReadSheetHolder)readSheetHolder(); |
||||
} |
||||
} |
@ -0,0 +1,20 @@
|
||||
package com.alibaba.excel.read.metadata.holder.csv; |
||||
|
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; |
||||
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class CsvReadSheetHolder extends ReadSheetHolder { |
||||
|
||||
public CsvReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { |
||||
super(readSheet, readWorkbookHolder); |
||||
} |
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.alibaba.excel.read.metadata.holder.csv; |
||||
|
||||
import com.alibaba.excel.read.metadata.ReadWorkbook; |
||||
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
||||
import lombok.Data; |
||||
import org.apache.commons.csv.CSVFormat; |
||||
|
||||
/** |
||||
* Workbook holder |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class CsvReadWorkbookHolder extends ReadWorkbookHolder { |
||||
|
||||
private CSVFormat csvFormat; |
||||
|
||||
public CsvReadWorkbookHolder(ReadWorkbook readWorkbook) { |
||||
super(readWorkbook); |
||||
setExcelType(ExcelTypeEnum.CSV); |
||||
csvFormat = CSVFormat.DEFAULT; |
||||
} |
||||
} |
Loading…
Reference in new issue