下面是一段本人开发的一个真实项目中的使用方法。

package io.patamon.geocoding.utilsimport org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStreamclass ReadWriteExcel {fun readExcel(index:Int,cellnum:Int,path:String):Array<String> {//读取文件路径val wb = XSSFWorkbook(FileInputStream(File(path)))//取工作表val sheet = wb.getSheetAt(index)//遍历每行的第二列var addresses:Array<String> = arrayOf()for (i in 1..sheet.lastRowNum) {val row = sheet.getRow(i)val codeCell = row.getCell(cellnum)if (codeCell != null) {addresses = addresses.plus(codeCell.toString())}}return addresses}fun writeExcel(addressesFormat:Array<Array<Array<String>>>,path:String) {//创建Excelval wb = XSSFWorkbook()//创建工作表val roomSheet = wb.createSheet("户室数据")val unitSheet = wb.createSheet("单元数据")val floorSheet = wb.createSheet("楼层数据")val buildingSheet = wb.createSheet("幢楼数据")val doorplateSheet = wb.createSheet("门牌数据")val otherSheet = wb.createSheet("其他数据")//创建表头var row = roomSheet.createRow(0)row.createCell(0).setCellValue("地址名称")row.createCell(1).setCellValue("行政区划中文")row.createCell(2).setCellValue("街路巷中文")row.createCell(3).setCellValue("门牌号")row.createCell(4).setCellValue("门牌后缀中文")row.createCell(5).setCellValue("幢楼号")row.createCell(6).setCellValue("幢楼后缀中文")row.createCell(7).setCellValue("单元号")row.createCell(8).setCellValue("室号")row.createCell(9).setCellValue("室号后缀中文")row.createCell(10).setCellValue("地址类型")row = unitSheet.createRow(0)row.createCell(0).setCellValue("地址名称")row.createCell(1).setCellValue("行政区划中文")row.createCell(2).setCellValue("街路巷中文")row.createCell(3).setCellValue("门牌号")row.createCell(4).setCellValue("门牌后缀中文")row.createCell(5).setCellValue("幢楼号")row.createCell(6).setCellValue("幢楼后缀中文")row.createCell(7).setCellValue("单元号")row.createCell(8).setCellValue("地址类型")row = floorSheet.createRow(0)row.createCell(0).setCellValue("地址名称")row.createCell(1).setCellValue("行政区划中文")row.createCell(2).setCellValue("街路巷中文")row.createCell(3).setCellValue("门牌号")row.createCell(4).setCellValue("门牌后缀中文")row.createCell(5).setCellValue("幢楼号")row.createCell(6).setCellValue("幢楼后缀中文")row.createCell(7).setCellValue("楼层号")row.createCell(8).setCellValue("地址类型")row = buildingSheet.createRow(0)row.createCell(0).setCellValue("地址名称")row.createCell(1).setCellValue("行政区划中文")row.createCell(2).setCellValue("街路巷中文")row.createCell(3).setCellValue("门牌号")row.createCell(4).setCellValue("门牌后缀中文")row.createCell(5).setCellValue("幢楼号")row.createCell(6).setCellValue("幢楼后缀中文")row.createCell(7).setCellValue("地址类型")row = doorplateSheet.createRow(0)row.createCell(0).setCellValue("地址名称")row.createCell(1).setCellValue("行政区划中文")row.createCell(2).setCellValue("街路巷中文")row.createCell(3).setCellValue("门牌号")row.createCell(4).setCellValue("门牌后缀中文")row.createCell(5).setCellValue("地址类型")row = otherSheet.createRow(0)row.createCell(0).setCellValue("地址名称")row.createCell(1).setCellValue("行政区划中文")row.createCell(2).setCellValue("街路巷中文")row.createCell(3).setCellValue("地址类型")//下面是此项目的业务需要,主要是遍历创建单元格for (i in addressesFormat[0].indices) {val newRow = roomSheet.createRow(i+1)for (j in addressesFormat[0][i].indices){newRow.createCell(j).setCellValue(addressesFormat[0][i][j])}}for (i in addressesFormat[1].indices) {val newRow = unitSheet.createRow(i+1)var n:Int = -1for (j in addressesFormat[1][i].indices){if(j!=8&&j!=9){newRow.createCell(++n).setCellValue(addressesFormat[1][i][j])}}}for (i in addressesFormat[2].indices) {val newRow = floorSheet.createRow(i+1)var n:Int = -1for (j in addressesFormat[2][i].indices){if(j!=8&&j!=9){newRow.createCell(++n).setCellValue(addressesFormat[2][i][j])}}}for (i in addressesFormat[3].indices) {val newRow = buildingSheet.createRow(i+1)var n:Int = -1for (j in addressesFormat[3][i].indices){if(j!=7&&j!=8&&j!=9){newRow.createCell(++n).setCellValue(addressesFormat[3][i][j])}}}for (i in addressesFormat[4].indices) {val newRow = doorplateSheet.createRow(i+1)var n:Int = -1for (j in addressesFormat[4][i].indices){if(j!=5&&j!=6&&j!=7&&j!=8&&j!=9){newRow.createCell(++n).setCellValue(addressesFormat[4][i][j])}}}for (i in addressesFormat[5].indices) {val newRow = otherSheet.createRow(i+1)var n:Int = -1for (j in addressesFormat[5][i].indices){if(j!=5&&j!=6&&j!=7&&j!=8&&j!=9){newRow.createCell(++n).setCellValue(addressesFormat[5][i][j])}}}//写入文件路径val fileOut = FileOutputStream(File(path))// 将workbook写到输出流中wb.write(fileOut)fileOut.flush()fileOut.close()wb.close()}
}

Kotlin读写Excel文件相关推荐

  1. python怎么读excelsheet_python3 excle(python怎么读写excel文件)

    python读取已经打开的3个word和excle文件的路径 用 win32com 操控 word和Excel就可以实现 咋样把python写入excle中 # 需安装 xlrd-0.9.2 和 xl ...

  2. python pandas读取excel-Python使用Pandas读写EXCEL文件教程

    欢迎,来自IP地址为:211.103.135.163 的朋友 教程开始之前,我们需要首先了解一下什么是Excel,这有助于理解之后教程中的内容. 什么是Excel Excel  是微软出品的和款办公软 ...

  3. python处理excel教程实例-python 读写excel文件操作示例【附源码下载】

    本文实例讲述了python 读写excel文件操作.分享给大家供大家参考,具体如下: 对excel文件的操作,python有第三方的工具包支持,xlutils,在这个工具包中包含了xlrd,xlwt等 ...

  4. python导入excel文件-python使用xlrd模块读写Excel文件的方法

    本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...

  5. C++读写EXCEL文件方式比较 .

    C++读取Excel的XLS文件的方法有很多,但是也许就是因为方法太多,大家在选择的时候会很疑惑. 由于前两天要做导表工具,比较了常用的方法,总结一下写个短文, 1.OLE的方式 这个大约是最常用的方 ...

  6. Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 Python使用openpyxl读取excel文件中数据 Python使用openpyxl往excel文件中写入数据 Python使用openpyx ...

  7. 基于POI的读写Excel文件的工具类

    依赖的jar包: import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStrea ...

  8. python xlrd xlwt pandas 模块 区别_python如何读写excel文件|python教程|python入门|python教程...

    https://www.xin3721.com/eschool/pythonxin3721/ python读写excel的方式有很多,不同的模块在读写的方法上稍有区别: 用xlrd和xlwt进行exc ...

  9. python读取python源代码文件_python 读写excel文件操作示例【附源码下载】

    本文实例讲述了python 读写excel文件操作.分享给大家供大家参考,具体如下: 对excel文件的操作,python有第三方的工具包支持,xlutils,在这个工具包中包含了xlrd,xlwt等 ...

最新文章

  1. 【bzoj1179】 Apio2009—Atm
  2. c语言复杂的程序代码,C语言中复杂结构的序列化
  3. 前端学习(2053)vue之电商管理系统电商系统之使用pm2管理
  4. ArcGIS Flex API 中的 Flex 技术(一)--事件
  5. 数据结构与算法之美 02 | 如何抓住重点
  6. python爬虫中文乱码_Python爬虫处理抓取数据中文乱码问题
  7. 【netty】Netty并发工具-Promise
  8. 搜狗输入法精简_韩语输入法软件哪个好?(手机版amp;电脑版)
  9. 读书笔记-泛型有限通配符
  10. Java项目毕业设计:基于springboot+vue的旧物置换网站
  11. Ubuntu 14.04 安装xvid编码器
  12. DS18B20温度传感器学习笔记
  13. react js清除浏览器缓存
  14. 影响职场升迁的小动作
  15. np.meshgrid函数中的indexing参数问题
  16. 申请计算机语言学留学要求,布兰迪斯大学计算机语言学文学硕士研究生申请要求及申请材料要求清单...
  17. excel 如何超链接到另一个工作薄下查找相同字符数据
  18. python另存为快捷键_Python学习之pycharm的快捷键大全
  19. pycharm中出现Windows PowerShell版权所有 (C) Microsoft Corporation。保留所有权利。尝试新的跨平台 PowerShell https://aka.ms/
  20. 2014年,趁着互联网金融P2P网贷未被监管,赚点小钱吧~

热门文章

  1. RocketMQ(四):生产者消息数据写入实现细节
  2. git查看stash里面的具体内容
  3. 设计师:设计师之家装材料知识之家装八项(吊顶材料、门窗材料、五金材料、墙面材料、地面材料、胶粘材料、油漆材料、水电材料等)之详细攻略
  4. OSChina 周六乱弹 —— 给你看个小仙女
  5. 《仙剑奇侠传3》全攻略
  6. 打开word时显示microsoft visual basic运行时错误没有注册类怎么解决?
  7. 【MVO TSP】基于matlab灰狼算法求解旅行商问题【含Matlab源码 1327期】
  8. 用正割对数计算积分的方法
  9. vue 打卡图片_掀起ins打卡潮的《小丑》台阶到底有什么魔力?
  10. kafka配置SASL