easyexcel导入指定excel
时间: 2023-11-17 13:03:55 浏览: 151
以下是使用EasyExcel导入指定Excel的示例代码:
```python
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadCellHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
import com.alibaba.excel.read.metadata.property.ExcelReadSheetProperty;
import com.alibaba.excel.read.metadata.property.ExcelReadTable;
import com.alibaba.excel.read.metadata.property.ExcelReadWorkbookProperty;
import com.alibaba.excel.read.metadata.property.ReadBasicParameter;
import com.alibaba.excel.read.metadata.property.ReadWorkbookParameter;
import com.alibaba.excel.read.metadata.property.XlsReadWorkbookProperties;
import com.alibaba.excel.read.metadata.property.XlsxReadWorkbookProperties;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadRowHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadRowHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadCellHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadCellHolder;
public class ExcelReaderDemo {
public static void main(String[] args) {
String fileName = "path/to/excel/file.xlsx";
ReadListener<Object> listener = new AnalysisEventListener<Object>() {
@Override
public void invoke(Object data, AnalysisContext context) {
// 处理读取到的数据
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 读取完成后的操作
}
};
ExcelReaderBuilder builder = EasyExcel.read(fileName, listener);
ReadWorkbookParameter readWorkbookParameter = new ReadWorkbookParameter();
readWorkbookParameter.setCustomReadTable(new ExcelReadTable());
ReadSheet readSheet = new ReadSheet();
readSheet.setSheetNo(0);
readSheet.setHeadRowNumber(1);
readSheet.setClazz(Object.class);
builder.read(readSheet);
builder.doRead();
}
}
```
在上面的示例代码中,我们使用EasyExcel的`ExcelReaderBuilder`类来构建一个Excel读取器。我们指定了要读取的Excel文件的路径和一个`ReadListener`实例,该实例用于处理读取到的数据。我们还指定了要读取的工作表的编号、标题行号和数据类型。最后,我们调用`doRead`方法来开始读取Excel文件。
阅读全文