Excel文件读取
应用技术:
1、文件io流
2、Apache提供的jar包–POI(处理Excel、Word、PPT等文件)
Maven导入:
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
<exclusions>
<exclusion>
<artifactId>commons-codec</artifactId>
<groupId>commons-codec</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<!-- 文件上传的jar包 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
实现代码–结合项目
//controller类
@Controller
@RequestMapping("excel")
@slf4j
@Api
public class ExcelController{
@Resource
excelService
public ResponseEntity importExcel(MultipartFile file){
//调用service方法即可
}
}
//对应的service
public class ExcelService{
public void readExcel(MultipartFile file){
Workbook wb = WorkbookFactory.create(file.getInputStream());
//一个excel文件中只有一个工作簿时,可以直接获取
sheet s = wb.getSheetAt(0);//第一个工作簿
int maxs = s.getPhysicalNumberOfRows();//总行数
Row r = s.getRow(0);//表的第0列行
int maxr = r.getPhysicalNumberOfCells();//总列数
//遍历表格内容,其实就是遍历一个二维数组
for(int i=0;i<maxs;i++){
Row row = s.getRow(i);//行
for(int j=0;j<maxr;j++){
//注意根据行来获取列,将获取到的单元格内容设置到封装好的对象属性中即可
//也可以直接打印单元格内容
Cell cell = row.getCell(j);//列,到这里表示已经拿到了行对应列的单元格内容cell,根据需要来对其进行操作
}
}
}
}
代码逻辑测试
这里为了简单直接书写逻辑方法中容易出现的问题:
1、文件io流相关异常问题?
- 项目代码中文件输入采用的是MultipartFile file,是客户端上传excel文件,测试的时候采用了File直接输入。
File file = new File(“你想要测试的表格文件路径”):
InoutStream in = new InputStream();
异常信息:InvalidFormatException:Your stream was neither an OLE2 stream, nor an OOXML stream。
原因:maven编译打包时,将resources下的资源文件转码了。最终web工程打出的jar/war包,里面归档进去的excel模板文件都是乱码,文件头信息被修改,导致poi根本无法识别这样的excel文件。
解决办法:
- 修改maven文件------------本人没试过
<plugins>
<!-- 让maven不编译xls文件,但仍将其打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
- excel的不兼容问题-------本人没成功以至于报了其他错误
- 读取Excel2003(xls):
public void test(File file) throws IOException {
InputStream inp = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(inp);
// workbook...遍历操作
}
- 读取的是Excel2007(xlsx)
public void test(File file) throws IOException {
InputStream inp = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(inp);
// workbook...遍历操作
}
-
采用HSS和XSS方法产生的异常
读取word文档报错 错误信息:
Invalid header signature; read 0x3C0A0D0A0DBFBBEF, expected 0xE11AB1A1E011CFD0private final static String filePath = "D:/Test.doc"; public static void main(String[] args) throws FileNotFoundException, IOException { FileInputStream stream = new FileInputStream(filePath); System.out.println(stream); HWPFDocument doc = new HWPFDocument(new FileInputStream(filePath)); System.out.println(doc); }
解决办法:将读取那个文档,另存为一个文件就好,重新读取
private final static String filePath = "D:/TestNew.doc";
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream stream = new FileInputStream(filePath);
System.out.println(stream);
HWPFDocument doc = new HWPFDocument(new FileInputStream(filePath));
System.out.println(doc);
}
————————————————
版权声明:本文为CSDN博主「HQZYX」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hqbootstrap1/article/details/81283504
对于上述异常其实都没有解决,还有一个异常忘了,以至于我在三个异常之间循环往复,可以说是很无奈了,但是采用 MultipartFile file 上传文件则不会产生上述异常问题。猜想是excel文件的流操作出了问题,那不要把文件变成流流是不是就好了,可以采 用修改maven包的方式试一试-
2、一个excel文件中如果有多个工作簿?
sheet s = wb.getSheetAt(0);//第一个工作簿
sheet s = wb.getSheetAt(1);//第2个工作簿
//以此就可以找到你想要的工作簿
3、一个工作簿中有多个表?
当一个工作簿中有多个工作表的时候,直接读取行跟列就行,如何区分是不是你读到的表格,我们可以添加相应的表格识别代码。
比如:每一个表都会带有自己的表头,一般位于第一行;也可以按照表格之间的不同点来进行表格识别。
小菜鸡变大飞机