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

Android:生成Excel表格并保存到本地

提醒
本文实例是使用Kotlin进行开发演示的。

一、技术方案

  • org.apache.poi:poi
  • org.apache.poi:poi-ooxml

二、添加依赖

[versions]poi = "5.2.3"
log4j = "2.24.2"[libraries]#https://mvnrepository.com/artifact/org.apache.poi/poi
apache-poi = { module = "org.apache.poi:poi", version.ref = "poi" }
apache-poi-ooxml = { module = "org.apache.poi:poi-ooxml", version.ref = "poi" }
# https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core 上面的 apache-poi 需要添加log4j-core
log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j" }
implementation(libs.apache.poi)
implementation(libs.apache.poi.ooxml)
implementation(libs.log4j.core)

三、效果图

在这里插入图片描述

四、示例代码

TestPoi.kt

package com.example.test.testimport org.apache.poi.ss.usermodel.HorizontalAlignment
import org.apache.poi.ss.usermodel.VerticalAlignment
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.File
import java.io.FileOutputStreamdata class Fruit(val id: Long,var name: String,var price: String,var desc: String?,var count: Int = 0,var tip: String? = null
)fun getProductList(): List<Fruit> {val productList: MutableList<Fruit> = mutableListOf()productList.add(Fruit(1001, "蓝莓", "40.00", "新鲜水果", 10))productList.add(Fruit(1002, "葡萄", "15.00", "新鲜水果", 20))productList.add(Fruit(1003, "苹果", "12.00", "新鲜水果", 30))productList.add(Fruit(1004, "香蕉", "8.00", "新鲜水果", 15))productList.add(Fruit(1005, "西瓜", "4.00", "新鲜水果", 6))productList.add(Fruit(1006, "橙子", "5.00", "新鲜水果", 9))productList.add(Fruit(1007, "柚子", "5.00", "新鲜水果", 5))productList.add(Fruit(1008, "火龙果", "9.00", "新鲜水果", 11))productList.add(Fruit(1009, "猕猴桃", "10.00", "新鲜水果", 12))productList.add(Fruit(1010, "哈密瓜", "7.00", "新鲜水果", 6))productList.add(Fruit(1011, "皇冠梨", "5.00", "新鲜水果", 8))return productList
}fun createExcelSheet() {// 创建一个新的工作簿val workbook = XSSFWorkbook()// 创建一个工作表(sheet)val sheet = workbook.createSheet("水果清单")//创建XSSFFont对象val headXSSFFont = workbook.createFont()//设置字体样式,如字体名称、字体大小、加粗等, 下面设置了字体名称为Arial、字体大小为12、加粗headXSSFFont.fontName = "Arial"headXSSFFont.fontHeightInPoints = 14headXSSFFont.bold = true// 创建一个XSSFCellStyle对象来表示要设置的样式val headXSSFCellStyle = workbook.createCellStyle()// 将字体样式设置到XSSFCellStyle对象中headXSSFCellStyle.setFont(headXSSFFont)// 居中对齐headXSSFCellStyle.alignment = HorizontalAlignment.CENTERheadXSSFCellStyle.verticalAlignment = VerticalAlignment.CENTER// 创建行(0基索引)var xssFRow = sheet.createRow(0)//设置行高xssFRow.heightInPoints = 40F
//    xssFRow.height = 600//设置样式
//    xssFRow.rowStyle = headXSSFCellStyleval excleHead = arrayOf("编号", "水果名", "价格(斤)", "库存(箱)", "描述", "备注")for ((index, item) in excleHead.withIndex()) {var width = 20 * 256if (index == 2 || index == 3) {width = 15 * 256} else if (index == 4 || index == 5) {width = width shl 1}// 设置列宽sheet.setColumnWidth(index, width)val xSSFCell = xssFRow.createCell(index)//设置样式xSSFCell.cellStyle = headXSSFCellStylexSSFCell.setCellValue(item)}val deviceInfoList = getProductList()val xSSFFont = workbook.createFont()xSSFFont.fontHeightInPoints = 12val xSSFCellStyle = workbook.createCellStyle()xSSFCellStyle.setFont(xSSFFont)xSSFCellStyle.alignment = HorizontalAlignment.CENTERxSSFCellStyle.verticalAlignment = VerticalAlignment.CENTERfor ((index, item) in deviceInfoList.withIndex()) {xssFRow = sheet.createRow(index + 1)xssFRow.heightInPoints = 40F
//        xssFRow.height = 600val cXSSFCell0 = xssFRow.createCell(0)cXSSFCell0.setCellValue(item.id.toString())cXSSFCell0.cellStyle = xSSFCellStyleval cXSSFCell1 = xssFRow.createCell(1)cXSSFCell1.setCellValue(item.name)cXSSFCell1.cellStyle = xSSFCellStyleval cXSSFCell2 = xssFRow.createCell(2)cXSSFCell2.setCellValue(item.price)cXSSFCell2.cellStyle = xSSFCellStyleval cXSSFCell3 = xssFRow.createCell(3)cXSSFCell3.setCellValue(item.count.toString())cXSSFCell3.cellStyle = xSSFCellStyleval cXSSFCell4 = xssFRow.createCell(4)cXSSFCell4.setCellValue(item.desc.toString())cXSSFCell4.cellStyle = xSSFCellStyle}try {val fileOutputStream = FileOutputStream("水果清单.xlsx")
//        val fileOutputStream = FileOutputStream(File("D:\\水果清单.xlsx"))// Windows磁盘: D盘
//        val fileOutputStream = FileOutputStream(File("/Users/chinadragon/Desktop/水果清单.xlsx"))// mac 文件地址workbook.write(fileOutputStream)fileOutputStream.close()workbook.close()println("成功创建水果清单表格")} catch (e: Exception) {println("生成水果清单表格发生异常 $e")}
}fun main() {createExcelSheet()
}
http://www.lryc.cn/news/498347.html

相关文章:

  • JVM-程序计数器与栈
  • 【kotlin 】内联类(value class / inline class)
  • 【SpringBoot】使用IDEA创建SpringBoot项目
  • C++设计模式(原型、代理、适配器、组合)
  • 如何在CentOS 7上使用FreeIPA设置集中式Linux身份验证
  • vue2播放视频和预览文件的组件以及使用方法
  • 性能之巅:Go语言优化深度探索
  • react + antd desgin 使用form功能时upload,radio,checkbox不能回显的问题
  • 【08】MySQL复杂查询:子查询语句详解与示例
  • Unity 相机旋转及角度限制
  • error=‘null‘], commandType=io.lettuce.core.RedisPublisher$SubscriptionCommand]
  • Golang 字符串字面量表示方法
  • 03_Webpack模块打包工具
  • 【目标跟踪】AntiUAV600数据集详细介绍
  • 十、JavaScript的应用的习题
  • 【Spring】AOP
  • 三维地图,智慧城市,商业智能BI,数据可视化大屏(Cesiumjs/UE)
  • 鸿蒙Next通过oss上传照片到阿里云
  • 小白爬虫——selenium入门超详细教程
  • nlp培训重点
  • 什么是多模态和模态
  • apache中的Worker 和 Prefork 之间的区别是什么?
  • 系统监控——分布式链路追踪系统
  • 【Python]深入Python日志管理:从logging到分布式日志追踪的完整指南
  • DHCP Client的工作方式
  • docker-常用应用部署dockerfile模板
  • Unity3D学习FPS游戏(13)玩家血量控制
  • TDesign:Switch开关
  • AI在SEO中的应用与关键词优化探讨
  • [docker中首次配置git环境与时间同步问题]