当前位置: 首页 > news >正文

Android 使用poi生成Excel ,word并保存在指定路径内

一添加依赖(一定要用新版依赖防止一些bug)

minSdk= 26   //注意最小支持SDK26
dependencies {implementation 'org.apache.poi:poi:5.2.4'implementation 'org.apache.poi:poi-ooxml:5.2.4'implementation 'javax.xml.stream:stax-api:1.0-2'
}

二,创建方法

private void createExcelFile(String Path) {// 创建工作簿Workbook workbook = new XSSFWorkbook();// 创建工作表Sheet sheet = workbook.createSheet("姓名");// 创建行/*   Row row = sheet.createRow(0);// 创建单元格for (int i = 0; i <10 ; i++) {Cell cell = row.createCell(i);// 设置单元格的值cell.setCellValue("Fengfeng");}*/ArrayList<Map<Integer,Object>> arrayList = new ArrayList<>();Map<Integer,Object> m = new HashMap<>();m.put(0,"物料ID");m.put(1,"物料编码");m.put(2,"名称");m.put(3,"编号");m.put(4,"规格");m.put(5,"单位");m.put(6,"单价");m.put(7,"数量");m.put(8,"厂家");m.put(9,"类别");arrayList.add(m);for (int i = 0; i <10 ; i++) {Map<Integer,Object> map = new HashMap<>();map.put(0,"materialID");map.put(1,"materialEncoding");map.put(2,"materialName");map.put(3,"materialModel");map.put(4,"materialSize");map.put(5,"unit");map.put(6,"price");map.put(7,"count");map.put(8,"manufacturers");map.put(9,"type");arrayList.add(map);}Cell cell;int size = arrayList.get(0).size();for (int i = 0;i < arrayList.size();i++){Row row = sheet.createRow(i);Map<Integer, Object> map1 = arrayList.get(i);for (int j = 0;j < size;j++){cell = row.createCell(j);cell.setCellValue((String) map1.get(j));}}// 保存Excel文件try {File file = new File(Path, "example.xlsx");FileOutputStream outputStream = new FileOutputStream(file);workbook.write(outputStream);outputStream.close();Toast.makeText(this, "Excel文件已创建", Toast.LENGTH_SHORT).show();} catch (IOException e) {e.printStackTrace();}
}

三,如果有表追加的做法(这里使用的是room数据库导入的)

public class DbConvertExcel {public static void appendToExcelFile(Context context, String filePath) {// 工作表名称String sheetName = "test";testDao testDao = testDatabase.getDatabaseInstance(context).getLaserMachDao();try {File file = new File(filePath, "test.xlsx");XSSFWorkbook workbook;Sheet sheet;if (file.exists()) {/*          FileInputStream inputStream = new FileInputStream(file);OPCPackage opc = OPCPackage.open(inputStream);workbook = new XSSFWorkbook(opc);sheet = workbook.getSheet(sheetName);opc.close();  // 关闭OPCPackage*/FileInputStream inputStream = new FileInputStream(file);workbook = new XSSFWorkbook(inputStream);sheet = workbook.getSheet(sheetName);} else {workbook = new XSSFWorkbook();sheet = workbook.createSheet(sheetName);// 添加标题行Row titleRow = sheet.createRow(0);titleRow.createCell(0).setCellValue("编号");titleRow.createCell(1).setCellValue("名称");titleRow.createCell(2).setCellValue("类型");}int lastRowNum = sheet.getLastRowNum();List<test> all = testDao.getAll();for (test test: all) {Row row = sheet.createRow(lastRowNum + 1); // 从最后一行的下一行开始写入row.createCell(0).setCellValue(String.valueOf(test.getId()));row.createCell(1).setCellValue(test.getName());row.createCell(2).setCellValue(String.valueOf(test.getHandType()));lastRowNum++;}FileOutputStream outputStream = new FileOutputStream(file);workbook.write(outputStream);outputStream.close();Toast.makeText(context, "数据已追加到Excel文件", Toast.LENGTH_SHORT).show();} catch (IOException e) {e.printStackTrace();Log.e("===========", e.toString());}}
}

四,把Excel 里的数据放回数据库

public static void readFromExcelFile(Context context, String filePath) {try {File file = new File(filePath, "test.xlsx");if (file.exists()) {FileInputStream inputStream = new FileInputStream(file);XSSFWorkbook workbook = new XSSFWorkbook(inputStream);Sheet sheet = workbook.getSheet("test");testDao testDao = testDatabase.getDatabaseInstance(context).getLaserMachDao();Iterator<Row> rowIterator = sheet.iterator();// 跳过标题行if (rowIterator.hasNext()) {rowIterator.next();}while (rowIterator.hasNext()) {Row row = rowIterator.next();test test = new test();test.setId((int) row.getCell(0).getNumericCellValue());test.setName(row.getCell(1).getStringCellValue());test.setHandType((int) row.getCell(2).getNumericCellValue());testDao.insert(test);}Toast.makeText(context, "数据已从Excel文件导入到数据库", Toast.LENGTH_SHORT).show();workbook.close();inputStream.close();} else {// 文件不存在的处理逻辑}} catch (IOException e) {e.printStackTrace();Log.e("===========", e.toString());}
}
http://www.lryc.cn/news/229234.html

相关文章:

  • 嵌入式杂记 -- MCU的大小端模式
  • 对这套BI零售数据分析方案心动,是零售人天性
  • vuekeyclock 集成
  • ARM Linux 基础学习 / 配置交叉编译工具链 / 编译 Linux 应用和驱动 / 编译内核
  • 通讯协议学习之路(实践部分):SPI开发实践
  • 【系统安装】ubuntu20.04启动盘制作,正经教程,小白安装教程,百分百成功安装
  • 2023云计算发展趋势
  • C# .NET Core API Controller以及辅助专案
  • asp.net图书管理系统
  • 概念解析 | LoRA:低秩矩阵分解在神经网络微调中的作用
  • 前端---CSS的盒模型
  • Linux可以投屏到电视吗?用网页浏览器就能投屏到电视!
  • 云汇优想:抖音矩阵系统有哪些类型?
  • XSS 漏洞的理解
  • cocosCreator 之内存管理和释放
  • 飞天使-template模版相关知识
  • 一、Hadoop3.1.3集群搭建
  • QML16、从 C++ 定义 QML 类型
  • 【中间件篇-Redis缓存数据库06】Redis主从复制/哨兵 高并发高可用
  • LeetCode(12)时间插入、删除和获取随机元素【数组/字符串】【中等】
  • 前端面试题 计算机网络
  • windows aseprite编译指南(白嫖)
  • 生活污水处理一体化处理设备有哪些
  • JSON可视化管理工具JSON Hero
  • P6入门:项目初始化7-项目详情之代码/分类码Code
  • 跨国企业如何选择安全靠谱的跨国传输文件软件?
  • Command Injection
  • LeetCode | 20. 有效的括号
  • 英语语法 - 祈使句 | 虚拟语气
  • 记录pytorch实现自定义算子并转onnx文件输出