调用C#的SpreadsheetLight.dll程序集实现对xlsx文件的操作

By admin at 2021-11-05 • 1人收藏 • 2418人看过

SpreadsheetLight is an open source Open XML spreadsheet library for .NET Framework written in C#, and is released under the MIT License.

image.png

这里简单提供下使用示例

import dotNet;

var appDomain = dotNet.clr().createAppDomain();
var ssl = appDomain.loadFile("\SpreadsheetLight.dll");

var sl = ssl.new("SpreadsheetLight.SLDocument");
// set a boolean at "A1"
sl.SetCellValue("A1", true);

// set at row 2, columns 1 through 20, a value that's equal to the column index
for ( i = 1; 20; 1) sl.SetCellValue(2, i, i);

// set the value of PI
sl.SetCellValue("B3", 3.14159);
sl.SetCellValueNumeric(4, 2, "3.14159");

// normal string data
sl.SetCellValue("C6", "This is at C6!");

// typical XML-invalid characters are taken care of,
// in particular the & and < and >
sl.SetCellValue("I6", "Dinner & Dance costs < $10");
// this sets a cell formula
// Note that if you want to set a string that starts with the equal sign,
// but is not a formula, prepend a single quote.
// For example, "'==" will display 2 equal signs
sl.SetCellValue(7, 3, "=SUM(A2:T2)");
sl.SaveAs(io.fullpath("\HelloWorld.xlsx"));

这个程序集也依赖其他dll, 我把示例打包了, 有这方面需求的可以下载自行研究:

SpreadsheetLight.zip


开源项目地址:https://spreadsheetlight.com

17 个回复 | 最后更新于 2022-02-24
2021-11-05   #1

我在NuGet找不到system.drawing.common 4.0.0.1  你这个DLL哪里下载的?

2021-11-05   #2

回复#1 @朝西 :

在vs包管理里面安装这个包的时候,会自动下载需要的依赖库, 安装完成后,vs的包目录文件夹里就有了

2021-11-05   #3

大佬

spreadsheetlight.com的dll不行,用大佬的dll就行,厉害啊!

2021-11-06   #4

image.png

我下载VS 建立工程:  Microsoft Visual Studio Community 2019

控制台应用 .net framework  / 4.6.1

有这么多依赖.




2021-11-06   #5

回复#4 @朝西 :

系统自带的那些又不需要你带它 , 同时期待你能分享些实际使用这个dll的代码, 帮助一些其他迷茫的人.

image.png

2021-11-06   #6

回复#4 @朝西 :

。。。不会vs。。。飘过

2021-11-06   #7

OK, 同时感谢分享;

2021-11-09   #8

很好,又多了一个操作excel的途径,多谢大佬。

import console; 
import dotNet

// 导入dll
var ssl = dotNet.load("/SpreadsheetLight.dll");

// 新建excel
var s1 = ssl.new("SLDocument");
// 设置单元格
s1.SetCellValue("A1", 1)
// 保存文件
s1.SaveAs(io.fullpath("/a.xlsx"))

// 打开已存在的excel
var SLDocument = ssl.import("SLDocument")
var s2 = SLDocument("a.xlsx")
//设置单元格
s2.SetCellValue("A2", "111");
// 保存文件
s2.SaveAs(io.fullpath("/a.xlsx"));

console.pause(true);


2021-11-09   #9

回复#5 @admin :

最好能直接做成像com.excel那样的扩展库,这样调用起来就方便了,尤其对于没有安装excel的客户端来说。

2021-11-09   #10

回复#9 @jerryxjr1220 :

可以,得加钱

2021-11-10   #11

增加一个读取所有数据的例子:

import console; 
import dotNet

// 导入dll
var ssl = dotNet.load("/SpreadsheetLight.dll");

// 打开已存在的excel
var SLDocument = ssl.import("SLDocument")
var s2 = SLDocument("a.xlsx")
// 读取所有数据
var stats = s2.GetWorksheetStatistics();
for(i=1;stats.EndRowIndex;1){
	for(j=1;stats.EndColumnIndex;1){
		var value = s2.GetCellValueAsString(i, j);
		if(value != ""){
			console.log(value)
		}
	}
}


2021-11-10   #12

回复#11 @rebellioin51 :

谢谢分享

2022-02-12   #13

用Python调用C#的dll,需要用到pythonnet库,改写了一下,完美运行

把Spreadsheetlight.dll放到同一文件夹。

import clr
clr.AddReference('Spreadsheetlight') # import dll, no need to add ".dll"
from SpreadsheetLight import SLDocument # import class
sl = SLDocument() # create object from class

# set a boolean at "A1"
sl.SetCellValue("A1", True)

# set at row 2, columns1 through 20, a value that's equal to the column index
for i in range(20):
    sl.SetCellValue(2, i, i)

# set the value of PI
sl.SetCellValue("B3", 3.14159)
sl.SetCellValueNumeric(4, 2, "3.14159")

# normal string data
sl.SetCellValue("C6", "This is at C6!")

# typical XML - invalid characters are taken care of, in particular the & and < and >
sl.SetCellValue("I6", "Dinner & Dance costs < $10")

# this sets a cell formula
# Note that if you want to set a string that starts with the equal sign,
# but is not a formula, prepend a single quote.
# For example, "'==" will display 2 equal signs
sl.SetCellValue(7, 3, "=SUM(A2:T2)")
sl.SaveAs("HelloWorld.xlsx")


2022-02-12   #14

回复#14 @jacen :

pythonnet关键是版本要装对,python的版本太多了,各种第三方库支持的版本又不一样,比如pythonnet只支持到python 3.8,用python 3.9就不行

2022-02-13   #15

设置字体颜色

style = s1.CreateStyle();

style.Font.FontColor=System.Drawing.Color.Blue

s1.SetCellStyle("A1",style)
s1.SetCellValue("A1","我想改变字体颜色")


2022-02-19   #16

感谢大佬们的教程

2022-02-24   #17

好哈学习....

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



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

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

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...