首先你需要下載 jxl-2.6.12.jar。鏈接: https://pan.baidu.com/s/1qXrorloIjxFb-6hyfdicBQ 提取碼: jgiw
然后把jar包放到你的studio的Project目錄下的libs下面
附上圖片

image.png
記得Add As Library.. 這樣你才可以引用到

image.png
主要代碼:
package com.example.qiandao.Excel;
import android.os.Build;
import androidx.annotation.RequiresApi;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
* @author LiHangZhou
* @description:
* @date :2021/1/6 10:07
* @love :zlx
*/
public class ExcelDataUtil {
@RequiresApi(api = Build.VERSION_CODES.N)
public static void main(String[] args) {
try {
List<Map<String, String>> maps = redExcel("D:\\文檔\\cp方對接參數(shù).xlsx");
System.out.println(maps);
} catch (Exception e) {
System.out.println(e);
}
// 寫入
// File file = new File("D:\\文檔\\a.xlsx");
// WriteInExcel(file,"ad",0);
// //打開文件
// WritableWorkbook book = Workbook.createWorkbook(new File("D:\\文檔\\a.xlsx"));
// //生成名為“第一頁”的工作表,參數(shù)0表示這是第一頁
// WritableSheet sheet = book.createSheet("第一頁", 0);
// //在Label對象的構(gòu)造子中指名單元格位置是第一列第一行(0,0)
// //以及單元格內(nèi)容為test
// Label label = new Label(0, 0, "測試");
// //將定義好的單元格添加到工作表中
// sheet.addCell(label);
// jxl.write.Number number = new jxl.write.Number(1, 0, 789.123);
// sheet.addCell(number);
// jxl.write.Label s = new jxl.write.Label(1, 2, "三十三");
// sheet.addCell(s);
// //寫入數(shù)據(jù)并關(guān)閉文件
// book.write();
// book.close(); //最好在finally中關(guān)閉,此處僅作為示例不太規(guī)范
}
/**
*
* @param file 文件路徑
* @param pagination 第幾頁
* @param i 參數(shù)0表示這是第一頁
* @throws IOException
* @throws WriteException
*/
private static void WriteInExcel(File file, String pagination, int i) throws IOException, WriteException {
//打開文件
WritableWorkbook book = Workbook.createWorkbook(file);
//生成名為“第一頁”的工作表,參數(shù)0表示這是第一頁
WritableSheet sheet = book.createSheet(pagination, i);
//在Label對象的構(gòu)造子中指名單元格位置是第一列第一行(0,0)
//以及單元格內(nèi)容為test
Label label = new Label(0, 0, "測試");
//將定義好的單元格添加到工作表中
sheet.addCell(label);
jxl.write.Number number = new jxl.write.Number(1, 0, 789.123);
sheet.addCell(number);
jxl.write.Label s = new jxl.write.Label(1, 2, "三十三");
sheet.addCell(s);
System.out.println("寫入成功");
//寫入數(shù)據(jù)并關(guān)閉文件
book.write();
book.close(); //最好在finally中關(guān)閉,此處僅作為示例不太規(guī)范
}
/**
* 讀取excel內(nèi)容
* <p>
* 用戶模式下:
* 弊端:對于少量的數(shù)據(jù)可以,單數(shù)對于大量的數(shù)據(jù),會造成內(nèi)存占據(jù)過大,有時候會造成內(nèi)存溢出
* 建議修改成事件模式
*/
public static List<Map<String, String>> redExcel(String filePath) throws Exception {
File file = new File(filePath);
if (!file.exists()) {
throw new Exception("文件不存在!");
}
InputStream in = new FileInputStream(file);
// 讀取整個Excel
XSSFWorkbook sheets = new XSSFWorkbook(in);
// 獲取第一個表單Sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
ArrayList<Map<String, String>> list = new ArrayList<>();
//默認第一行為標題行,i = 0
XSSFRow titleRow = sheetAt.getRow(0);
// 循環(huán)獲取每一行數(shù)據(jù)
for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
XSSFRow row = sheetAt.getRow(i);
LinkedHashMap<String, String> map = new LinkedHashMap<>();
// 讀取每一格內(nèi)容
for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
XSSFCell titleCell = titleRow.getCell(index);
XSSFCell cell = row.getCell(index);
// cell.setCellType(XSSFCell.CELL_TYPE_STRING); 過期,使用下面替換
cell.setCellType(CellType.STRING);
if (cell.getStringCellValue().equals("")) {
continue;
}
map.put(getString(titleCell), getString(cell));
}
if (map.isEmpty()) {
continue;
}
list.add(map);
}
return list;
}
/**
* 把單元格的內(nèi)容轉(zhuǎn)為字符串
*
* @param xssfCell 單元格
* @return String
*/
public static String getString(XSSFCell xssfCell) {
if (xssfCell == null) {
return "";
}
if (xssfCell.getCellTypeEnum() == CellType.NUMERIC) {
return String.valueOf(xssfCell.getNumericCellValue());
} else if (xssfCell.getCellTypeEnum() == CellType.BOOLEAN) {
return String.valueOf(xssfCell.getBooleanCellValue());
} else {
return xssfCell.getStringCellValue();
}
}
}
代碼復(fù)制可以直接使用