java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的 Java 程序设计语言和 Java 平台(即 JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
这篇文章主要介绍了 Javaweb 使用 POI 操作 Excel 文件, 需要的朋友可以参考下
1. 为项目添加 POIPOI 官网链接
点进去之后下载(上边的是编译好的类,下边的是源代码)
解压文件夹,把下面三个文件复制到 WebComtent>WEB-INF>lib 文件夹下
再把这三个文件复制到 Tomcat 的 lib 文件夹下,否则 Tomcat 会因为找不到类而报错(这个地方郁闷了一上午)
读取 ".xls" 格式使用 import org.apache.poi.hssf.usermodel.*; 包的内容,例如:HSSFWorkbook
读取 ".xlsx" 格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook
由于我是读取 xslx 文件所以使用以上几个 jar 文件。
注意:
上图中的两个文件夹中也有我们需要的 jar 文件,具体是哪几个忘记了(当然为了保险也可以把所有的都放进 WebContent>WEN-INF>lib 下再 BuildPath 进项目),没关系,一会运行的过程中会报错,根据错误信息再去找到相关的 jar 文件 BuildPath 进去就好,注意还要再 Tomcat>lib 下放置一份副本。
2. 读取 Excel 文件官方教程: 链接
类库: 链接
直接看代码吧,不难懂。
- //遍历一个Excel文件<br>private void getExcelData(File file) {
- System.out.println("now in getExcelData");
- System.out.println("get file name:" + file.getName().toString());
- XSSFWorkbook workBook = null;
- try {
- workBook = new XSSFWorkbook(file);
- int sheetCount = workBook.getNumberOfSheets(); //Sheet的数量
- System.out.println("num of sheet is : " + sheetCount);
- //遍历每个sheet
- for (int i = 0; i < sheetCount; i++) {
- XSSFSheet sheet = workBook.getSheetAt(i);
- //获取总行数
- int rowCount = sheet.getPhysicalNumberOfRows();
- System.out.println("num of row : " + rowCount);
- System.out.println("i now in sheet : " + i);
- //遍历每一行
- for (int r = 0; r < rowCount; r++) {
- XSSFRow row = sheet.getRow(r);
- //获取总列数
- int cellCount = row.getPhysicalNumberOfCells();
- //遍历每一列
- for (int c = 0; c < cellCount; c++) {
- XSSFCell cell = row.getCell(c);
- String cellValue = null;
- switch (cell.getCellTypeEnum()) {
- case STRING:
- //System.out.println("celltype is string");
- cellValue = cell.getStringCellValue();
- break;
- case NUMERIC:
- //System.out.println("celltype is Number");//整数,小数,日期
- cellValue = String.valueOf(cell.getNumericCellValue());
- break;
- case BOOLEAN:
- //System.out.println("celltype is Boolean");
- cellValue = String.valueOf(cell.getBooleanCellValue());
- break;
- case FORMULA:
- //System.out.println("celltype is Formula");//公式
- cellValue = "错误,不能为公式";
- break;
- case BLANK:
- //System.out.println("celltype is Blank");//空白
- cellValue = cell.getStringCellValue();
- break;
- case ERROR:
- //System.out.println("celltype is Error");
- cellValue = "错误";
- break;
- default:
- //System.out.println("celltype : default");
- cellValue = "错误";
- break;
- }
- System.out.println(cellValue.toString());
- }
- }
- }
- } catch(IOException e) {
- System.out.println("File Error IOException : " + e.getMessage());
- } catch(Exception e) {
- // TODO: handle exception
- } finally {
- try {
- workBook.close();
- } catch(Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- System.out.println("workBook.close()&fileInputStream.close() Error : " + e.getMessage());
- }
- System.out.println("Try Catch : finally");
- }
- System.out.println("hi feipeng8848 getExcelData is done");
- }
以上所述是小编给大家介绍的 JavaWeb 使用 POI 操作 Excel 文件实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 PHPERZ 网站的支持!
来源: http://www.phperz.com/article/17/1231/357278.html