当前位置: 首页 > 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.mrgr.cn/news/78895.html

相关文章:

  • 网盘聚合搜索项目Aipan(爱盼)【续】
  • Web安全基础实践
  • Linux网络——传输层
  • Microi 吾码:后端开发的创新引擎与代码艺术
  • 头歌Hadoop 开发环境搭建及HDFS初体验(第2关:配置开发环境 - Hadoop安装与伪分布式集群搭建)
  • AI数据分析工具(二)
  • 书生浦语·第四期作业合集
  • 【小白学机器学习41】如何从正态分布的总体中去抽样?比较不同的取样方差的差别
  • 3分钟快速掌握——c语言【流程控制】if else选择语句,for while循环,goto语句
  • java基础概念46-数据结构1
  • Linux命令进阶·如何切换root以及回退、sudo命令、用户/用户组管理,getent命令以及解决创建用户不显示问题和Ubuntu不显示用户名只显示“$“符号问题
  • 爬虫专栏第二篇:Requests 库实战:从基础 GET 到 POST 登录全攻略
  • 长安汽车嵌入式面试题及参考答案
  • 开源鸿蒙system ability manager关键属性解析
  • 爬虫专栏第一篇:深入探索爬虫世界:基础原理、类型特点与规范要点全解析
  • linux网络抓包工具
  • PyQt6思维导图和实例(登录帝国时代)
  • Linux 35.6 + JetPack v5.1.4@DeepStream安装
  • echarts的双X轴,父级居中的相关配置
  • IDEA 配置鼠标悬浮后显示方法注释 javaDoc
  • EasyMedia播放rtsprtmp视频流(flvhls)
  • 运输层总结
  • Win10+Ubuntu20.04双系统重装Ubuntu22.04单系统
  • Hive学习基本概念
  • 在全志 T113-S3 开发板上运行 AWTK
  • Rust : 生成日历管理markdown文件的小工具