用string.xml库来处理excel的xlsx格式数据文件

By admin at 2020-03-17 • 0人收藏 • 2120人看过

感谢aardio培训群:Kylin 分享源码

xlsx格式的excel文件是对xml的封包, 所以可以用string.xml库来处理这种格式文件的数据

image.png

下面是他提供的简单数字数据读取示例库excelx:

//excel_数字数据简单读取
import string.xml;
import zlib.zip
import zlib.unzip
import fsys;
import console;

class excelx{
	ctor (xlsx, n=1) {
		if xlsx and ..io.exist(..io.fullpath(xlsx)) {
			..fsys.copy(..io.fullpath(xlsx),"~/temp.zip");
			..zlib.unzip.extract( "~/temp.zip","~/tmp",
				function(fileName,extractPath,fileInfo,size,unitSize,unitName){
					if(extractPath){ 
						return true;
					}
				}, , function(numEntries){}
			)
			try{
				this = ..string.xml(..string.load("~/tmp/xl/worksheets/sheet"++n++".xml"))
				this.data = this.queryEle({tagName="sheetData"})
				this.rows = this.queryEles({tagName="row"})
				this.cells = this.queryEles({tagName="c"})
				this.totable_ = function(){
					var tab = {}
					for i,row in this.rows {
						var r = {}
						var cells = row.queryEles({tagName="c"})
						for j,cell in cells {
							var v = cell.innerText();
							try {
								v = tonumber(v)
							}
							..table.push(r, v)
						}
						..table.push(tab,r)
					}
					return tab;
				}
				this.tab = this.totable_()
			}
			catch (e) {
				..console.dump(e);
				..console.dump("读取excel文件失败! 程序退出!")
				return null;
			}
		} elseif xlsx and !..io.exist(..io.fullpath(xlsx)) {
			..console.dump(e);
			..console.dump("读取excel文件失败! 程序退出!")
			return null;
		}
	}
	
	totable = function(){
		return this.tab;
	}
	
	getCell = function(row,col){
		if (type(row) == type.string) && (col == null) {
			var v = this.queryEle({tagName="c"; r=row}).innerText();
			try {
				v = tonumber(v)
			}
			return v
		} elseif (type(row) == type.number) && (type(col) == type.number){
			var tab = owner.totable()
			return tab[row][col]
		}	
	}
	
	getRow = function(row){
		return this.tab[tonumber(row)]
	}
	
	getCol = function(col){
		var cols = {}
		if type(col) == type.number {
			for _,row in this.tab {
				..table.push(cols, row[col])
			}
		} elseif (type(col) == type.string) {
			for _,cell in this.cells {
				if ..string.find(cell.r,"^"++col++"\d+") {
					var v = cell.innerText()
					try {
						v = tonumber(v)
					}
					..table.push(cols, v)
				}
			}			
		}
		return cols;			
	}
		
}

注意: 使用的时候该库要放到工程lib库目录下,导入excelx即可

import console
import excelx
xl=excelx("C:\demo.xlsx");
console.dump(xl.getRow(2))
console.dump(xl.getCol("C"))
console.log(xl.getCell("B1"))
console.dump(xl.totable())
console.pause()


4 个回复 | 最后更新于 2021-09-07
2020-03-17   #1

设置setcell?

支持名称引用就好了.

2020-03-18   #2

回复#1 @dason :

你所说的引用,应该是智能提示的功劳

2021-09-05   #3

2021-09-07   #4

这个库其实只是我拿来练手玩玩的,并不能作为实用,因为它是通过tag标签去寻找对应的内容,如果内容中有其他格式或标签就会导致寻找的内容出错。

所以,拿来玩玩可以,平时就不要使用了。我后期也不准备更新了。

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...