调用C# GemBox读写Excel表格

By jerryxjr1220 at 2022-06-22 • 1人收藏 • 1263人看过

image.png

GemBox.Spreadsheet 是一个 .NET 组件,它使您能够使用一个简单的 API 从 .NET 应用程序中读取、写入、编辑、转换和打印电子表格文件。

使用 GemBox.Spreadsheet,您可以获得一个快速可靠且易于使用的组件。它只需要 .NET,因此您可以轻松部署您的应用程序,而无需考虑其他许可证。它比 Microsoft Excel 自动化快 250 倍!


官方网址: https://www.gemboxsoftware.com/spreadsheet


import dotNet;
dotNet.reference({
	"GemBox.Spreadsheet" = $"\res\GemBox.Spreadsheet.dll";
	"BouncyCastle.Crypto" = $"\res\BouncyCastle.Crypto.dll";
});

Spreadsheet = dotNet.import("GemBox.Spreadsheet");

Spreadsheet.SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

//新建Excel
wb = Spreadsheet.ExcelFile();

//新增worksheet
ws = wb.Worksheets.Add("Sheet 1");

//读取Excel
//wb = Spreadsheet.ExcelFile.Load(file);

//按Cell名称读写值
ws.Cells.Item["A1"].Value = 123;

//按Cell行列读写公式,行列从0开始计数
ws.Rows.Item[1].Cells.Item[2].Formula = "= A1";

//保存
wb.Save(io.fullpath("\123.xlsx"));

image.png

调用还是挺方便的,暂时用免费版也没法发现什么限制。

要是买了License可以更改上面的值变成正式版。


测试文件下载:

GemBox.7z

GemBox也可以直接在Nuget上下载。

6 个回复 | 最后更新于 2022-08-21
2022-06-22   #1

谢谢分享, 我帮你增加了简介和dll打包工程. , 方面其他人测试.

2022-06-23   #2

回复#1 @admin :

2022-06-24   #3

看起来确实简...试下看看

谢谢楼主

2022-08-20   #4

@admin 如何把如何把 sheet 嵌入到 form里面

import dotNet;
import win.ui;
import console;
/*DSG{{*/
mainForm = win.form(text="测试2";right=1089;bottom=554)
mainForm.add(
custom={cls="custom";text="自定义控件";left=364;top=8;right=1074;
bottom=538;bgcolor=12639424;db=1;dl=1;dr=1;dt=1;z=1};
)
/*}}*/
dotNet.reference({
    "GemBox.Spreadsheet" = $"\res\GemBox.Spreadsheet.dll";
    "BouncyCastle.Crypto" = $"\res\BouncyCastle.Crypto.dll";
});
 
Spreadsheet = dotNet.import("GemBox.Spreadsheet");
 
Spreadsheet.SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
// 如何把 sheet 嵌入到 form里面??
//win.setParent(Spreadsheet,mainForm.custom.hwnd);
mainForm.custom.onEraseBkgnd  = lambda() 0;
mainForm.custom.adjust = function( cx,cy,wParam ) {    
    Spreadsheet.Width = cx;
    Spreadsheet.Height = cy; 
};
mainForm.custom.adjust();
 
//新建Excel
wb = Spreadsheet.ExcelFile();
 
//新增worksheet
ws = wb.Worksheets.Add("Sheet 1");
 
//读取Excel
//wb = Spreadsheet.ExcelFile.Load("");
 
//按Cell名称读写值
ws.Cells.Item["A1"].Value = 123444;
 
//按Cell行列读写公式,行列从0开始计数
ws.Rows.Item[1].Cells.Item[2].Formula = "= A1";
 
//保存
wb.Save(io.fullpath("\123.xlsx"));

mainForm.show();
return win.loopMessage();


2022-08-20   #5

回复#4 @andrew7baker :

这个是后台处理数据的, 不是显示数据的, 所以没有显示在界面上的功能.

如果你想把处理好的数据显示在前面界面上, 要和DataGridView配合使用, 用DataGridView来显示数据, 具体看

https://www.gemboxsoftware.com/spreadsheet/examples/c-sharp-vb-net-import-export-excel-datagridview/5301


2022-08-21   #6

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



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

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

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...