可以读取/写入 .xls 文件的 .NET Excel 库

发布于 2024-11-05 03:17:19 字数 739 浏览 0 评论 0原文

我正在寻找一个读取/写入 .xls(不是 .xlsx)文件的 Excel 库。

我正在使用 excellibrary,但它有很多错误,我似乎无法打开我创建的文件。该问题已经存在近一年了,但尚未得到解决。

我在另一个帖子中看到有人建议 EPPlus,但这仅适用于 2007/2010 .xlsx 格式。

我过去使用过 Koogra,并且对它非常满意,但我相信它只能读……不写。

如果您知道图书馆,请告诉我它的名字。

编辑: 如果需要的话,我很高兴使用内置的 Microsoft.Office.Interop.Excel 创建 Excel 文件,但是我的计算机有 Office 2007,而目标计算机只有 Office 2003。我注意到该库有 2 个文件版本:12 和 14。我检查了目标计算机上的 Excel 版本,它是 11.8169.8172 - 我假设内置 Excel 互操作功能无法在目标上运行?

I'm looking for an Excel library which reads/writes .xls (not .xlsx) files.

I'm using excellibrary but it's very buggy and I can't seem to open the files I create. The issue has been known for almost a year and hasn't been fixed yet.

I've seen in another thread someone suggested EPPlus, but that only works with the 2007/2010 .xlsx format.

I've used Koogra in the past, and been quite happy with it, but I believe it can only read... not write.

If you know of a library, please let me know what it's called.

Edit:
I'm quite happy to create my Excel file with the built-in Microsoft.Office.Interop.Excel if necessary, however my machine has Office 2007 and the target machines only have Office 2003. I noticed I have 2 file versions for that library: 12, and 14. I checked the Excel version on the target machines and it's 11.8169.8172 - I'm assuming that the built-in Excel interop will not work on the target?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

庆幸我还是我 2024-11-12 03:17:19

我推荐 NPOI。 NPOI 免费并且仅适用于 .XLS 文件。它对我有很大帮助。

详细信息:如果您使用 NPOI,则无需在计算机上安装 Microsoft Office 即可处理 .XLS 文件。

查看这些博客文章:

创建 Excel 电子表格 .XLS 和C# 中的 .XLSX

带有 Excel 表格和动态图表的 NPOI

[更新]

NPOI 2.0 添加了对 XLSX 和 DOCX 的支持。

您可以在这里阅读更多相关信息:

NPOI 2.0 系列帖子预定

I'd recommend NPOI. NPOI is FREE and works exclusively with .XLS files. It has helped me a lot.

Detail: you don't need to have Microsoft Office installed on your machine to work with .XLS files if you use NPOI.

Check these blog posts:

Creating Excel spreadsheets .XLS and .XLSX in C#

NPOI with Excel Table and dynamic Chart

[UPDATE]

NPOI 2.0 added support for XLSX and DOCX.

You can read more about it here:

NPOI 2.0 series of posts scheduled

猫卆 2024-11-12 03:17:19

是否有原因导致无法使用 Excel ODBC 连接读取和写入 Excel?例如,我使用以下代码像数据库一样从 Excel 文件中逐行读取:

private DataTable LoadExcelData(string fileName)
{
  string Connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";

  OleDbConnection con = new OleDbConnection(Connection);

  OleDbCommand command = new OleDbCommand();

  DataTable dt = new DataTable(); OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$] WHERE LastName <> '' ORDER BY LastName, FirstName", con);

  myCommand.Fill(dt);

  Console.WriteLine(dt.Rows.Count);

  return dt;
}

您可以以相同的方式写入 Excel“数据库”。可以看到,您可以选择要使用的版本号,这样就可以对装有Excel 2003的机器降级Excel版本。实际上,使用Interop也是如此。您可以使用较低版本,它应该可以与 Excel 2003 一起使用,即使您的开发 PC 上只有较高版本。

Is there a reason why you can't use the Excel ODBC connection to read and write to Excel? For example, I've used the following code to read from an Excel file row by row like a database:

private DataTable LoadExcelData(string fileName)
{
  string Connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";

  OleDbConnection con = new OleDbConnection(Connection);

  OleDbCommand command = new OleDbCommand();

  DataTable dt = new DataTable(); OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$] WHERE LastName <> '' ORDER BY LastName, FirstName", con);

  myCommand.Fill(dt);

  Console.WriteLine(dt.Rows.Count);

  return dt;
}

You can write to the Excel "database" the same way. As you can see, you can select the version number to use so that you can downgrade Excel versions for the machine with Excel 2003. Actually, the same is true for using the Interop. You can use the lower version and it should work with Excel 2003 even though you only have the higher version on your development PC.

柠栀 2024-11-12 03:17:19

您可以考虑使用名为 Excel Jetcell .NET 组件 的第三方工具来读取/写入 excel 文件:

C#示例

// Create New Excel Workbook
ExcelWorkbook Wbook = new ExcelWorkbook();
ExcelCellCollection Cells = Wbook.Worksheets.Add("Sheet1").Cells;

Cells["A1"].Value = "Excel writer example (C#)";
Cells["A1"].Style.Font.Bold = true;
Cells["B1"].Value = "=550 + 5";

// Write Excel XLS file
Wbook.WriteXLS("excel_net.xls");

VB.NET 示例

' Create New Excel Workbook
Dim Wbook As ExcelWorkbook = New ExcelWorkbook()
Dim Cells As ExcelCellCollection = Wbook.Worksheets.Add("Sheet1").Cells

Cells("A1").Value = "Excel writer example (C#)"
Cells("A1").Style.Font.Bold = True
Cells("B1").Value = "=550 + 5"

' Write Excel XLS file
Wbook.WriteXLS("excel_net.xls")

You may consider 3rd party tool that called Excel Jetcell .NET component for read/write excel files:

C# sample

// Create New Excel Workbook
ExcelWorkbook Wbook = new ExcelWorkbook();
ExcelCellCollection Cells = Wbook.Worksheets.Add("Sheet1").Cells;

Cells["A1"].Value = "Excel writer example (C#)";
Cells["A1"].Style.Font.Bold = true;
Cells["B1"].Value = "=550 + 5";

// Write Excel XLS file
Wbook.WriteXLS("excel_net.xls");

VB.NET sample

' Create New Excel Workbook
Dim Wbook As ExcelWorkbook = New ExcelWorkbook()
Dim Cells As ExcelCellCollection = Wbook.Worksheets.Add("Sheet1").Cells

Cells("A1").Value = "Excel writer example (C#)"
Cells("A1").Style.Font.Bold = True
Cells("B1").Value = "=550 + 5"

' Write Excel XLS file
Wbook.WriteXLS("excel_net.xls")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文