【Demo】Android读取excel表格数据

数据

一共17种语言的星期和月份词
在这里插入图片描述

代码

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FileUtil {

    public static void main(String[] args) {
        readExcel(new String[]{"NO", "EN", "BG", "CZ", "DE", "ES", "FI", "FR", "HU", "IT", "LT", "NL", "PL", "PT", "RO", "RU", "SL", "TR"});
    }

    /**
     * 读取excel   (xls和xlsx)
     *
     * @return
     */
    public static List<Map<String, String>> readExcel(String columns[]) {
        File file = new File("/work/02Paccar/date.xlsx");
        String filePath = file.getAbsolutePath();
        Sheet sheet = null;
        Row row = null;
        Row rowHeader = null;
        List<Map<String, String>> list = null;
        String cellData = null;
        Workbook wb = null;
        if (filePath == null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (".xls".equals(extString)) {
                wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                wb = new XSSFWorkbook(is);
            } else {
                wb = null;
            }
            if (wb != null) {
                // 用来存放表中数据
                list = new ArrayList<Map<String, String>>();
                // 获取第一个sheet
                sheet = wb.getSheetAt(0);
                // 获取第一行
                rowHeader = sheet.getRow(0);
                row = sheet.getRow(0);
                // 获取最大行数
                int rownum = sheet.getPhysicalNumberOfRows();
                // 获取最大列数
                int colnum = row.getPhysicalNumberOfCells();

                String data[][] = new String[rownum][colnum];

                for (int i = 0; i < rownum; i++) {
                    row = sheet.getRow(i);
                    for (int j = 0; j < colnum; j++) {
                        data[i][j] = row.getCell(j).getStringCellValue();
                    }
                }

                for (int i = 1; i < colnum; i++) {
                    System.out.println(data[0][i]);
                    for (int j = 1; j < rownum; j++) {
                        System.out.println("<string name=\"" + data[j][0] + "\">" + data[j][i] + "</string>");
                    }
                    System.out.println();
                    System.out.println();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}

结果

在这里插入图片描述
jar包下载:https://github.com/andruhon/android5xlsx
参考文章:https://blog.csdn.net/u014743890/article/details/91045033

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,这是一个使用 Apache POI 库读取 Excel 文件数据的简单示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; public class ExcelReaderDemo { public static void main(String[] args) { String excelFilePath = "path/to/your/excel/file.xlsx"; try (FileInputStream inputStream = new FileInputStream(excelFilePath); Workbook workbook = new XSSFWorkbook(inputStream)) { Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 for (Row row : sheet) { for (Cell cell : row) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { System.out.print(cell.getStringCellValue() + "\t"); } else if (cellType == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } else if (cellType == CellType.BOOLEAN) { System.out.print(cell.getBooleanCellValue() + "\t"); } } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们首先通过 FileInputStream 打开 Excel 文件,然后使用 XSSFWorkbook 类创建 Workbook 对象。接下来,我们使用 getSheetAt() 方法获取第一个工作表。然后,我们遍历每一行和每个单元格,并根据单元格的类型打印出相应的值。 请确保将 `path/to/your/excel/file.xlsx` 替换为你要读取的实际 Excel 文件的路径。 希望这个示例对你有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值