从没有office的c#创建Excel文件[重复]

时间:2023-01-15 11:09:09

This question already has an answer here:

这个问题已经有了答案:

I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference. My dev computer has Excel on it, but the end user may or may not have Office installed. Will this tool fail if Office isn't installed on the end users computer, or is this interop service separate from the actual application?

我正在编写一个生成excel报告的程序,目前使用的是Microsoft.Interop。Excel参考。我的开发计算机上有Excel,但是最终用户可能没有安装Office。如果最终用户计算机上没有安装Office,这个工具会失败吗?或者这个互操作服务与实际应用程序是分开的吗?

7 个解决方案

#1


25  

Unless you have Excel installed on the Server/PC or use an external tool (which is possible without using Excel Interop, see Create Excel (.XLS and .XLSX) file from C#), it will fail. Using the interop requires Excel to be installed.

除非您已经在服务器/PC上安装了Excel,或者使用了外部工具(不使用Excel互操作也可以),请参阅Create Excel(。XLS和. xlsx)文件来自c#,它将失败。使用interop需要安装Excel。

#2


37  

If you're interested in making .xlsx (Office 2007 and beyond) files, you're in luck. Office 2007+ uses OpenXML which for lack of a more apt description is XML files inside of a zip named .xlsx

如果你对制作.xlsx (Office 2007及其他)文件感兴趣,那你就很幸运了。Office 2007+使用OpenXML,由于缺少更恰当的描述,它是一个名为.xlsx的zip文件中的XML文件

Take an excel file (2007+) and rename it to .zip, you can open it up and take a look. If you're using .NET 3.5 you can use the System.IO.Packaging library to manipulate the relationships & zipfile itself, and linq to xml to play with the xml (or just DOM if you're more comfortable).

取一个excel文件(2007+)并将其重命名为.zip,您可以打开它并查看它。如果你使用。net 3.5,你可以使用System.IO。打包库以操作关系& zipfile本身,并将linq到xml用于使用xml(如果您觉得更舒服的话,也可以只使用DOM)。

Otherwise id reccomend DotNetZip, a powerfull library for manipulation of zipfiles.

否则,id reccomend DotNetZip,一个用于处理zipfiles的powerfull库。

OpenXMLDeveloper has lots of resources about OpenXML and you can find more there.

OpenXMLDeveloper有很多关于OpenXML的资源,您可以在那里找到更多。

If you want .xls (2003 and below) you're going to have to look into 3rd party libraries or perhaps learn the file format yourself to achieve this without excel installed.

如果您想要.xls(2003及以下),您将需要查看第三方库,或者自己学习文件格式,以便在不安装excel的情况下实现这一点。

#3


22  

Try EPPlus if you use Excel 2007. Supports ranges, cellstyling, charts, shapes, pictures and a lot of other stuff

如果您使用Excel 2007,请尝试使用EPPlus。支持范围,细胞造型,图表,形状,图片和许多其他东西。

#4


8  

An interop calls something else, it's an interoperability assembly, so you're inter-operating with something...in this case Excel, the actual installed Excel.

互操作调用其他东西,它是一个互操作程序集,所以你是在与某些东西进行互操作…在本例中,实际安装的Excel。

In this case yes, it will fail to run because it depends on excel, you're just calling excel functions. If they don't have it installed...out of luck.

在这种情况下,它不会运行因为它依赖于excel,你只是调用excel函数。如果他们没有安装…运气不好。

There are methods to generate without Excel, provided the 2007 file format is ok, here's a few:

有一些方法可以在没有Excel的情况下生成,如果2007年的文件格式是ok的,这里有一些:

as I said though, this is the 2007 format, normally if anything, that's the deal-breaker.

就像我说的,这是2007年的格式,通常来说,如果有什么不同的话,那就是交易的破坏者。

#5


6  

There are a handful of options:

有几个选择:

  • NPOI - Which is free and open source.
  • NPOI是免费和开源的。
  • Aspose - Is definitely not free but robust.
  • Aspose——绝对不是免费的,但是很有活力。
  • Spreadsheet ML - Basically XML for creating spreadsheets.
  • 电子表格ML -用于创建电子表格的基本XML。

Using the Interop will require that the Excel be installed on the machine from which it is running. In a server side solution, this will be awful. Instead, you should use a tool like the ones above that lets you build an Excel file without Excel being installed.

使用Interop需要将Excel安装在运行它的机器上。在服务器端解决方案中,这将非常糟糕。相反,您应该使用类似上面的工具来构建Excel文件,而无需安装Excel。

If the user does not have Excel but has a tool that will read Excel (like Open Office), then obviously they will be able to open it. Microsoft has a free Excel viewer available for those users that do not have Excel.

如果用户没有Excel,但是有一个工具可以读取Excel(比如Open Office),那么很明显他们可以打开它。微软为那些没有Excel的用户提供免费的Excel查看器。

#6


4  

Use OleDB, you can create, read, and edit excel files pretty easily. Read the MSDN docs for more info:

使用OleDB,你可以很容易地创建、读取和编辑excel文件。更多信息请阅读MSDN文档:

http://msdn.microsoft.com/en-us/library/aa288452(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa288452(VS.71). aspx

I've used OleDB to read from excel files and I know you can create them, but I haven't done it firsthand.

我使用OleDB来读取excel文件,我知道你可以创建它们,但是我还没有直接做。

#7


1  

You could use the ExcelStorage Class of the FileHelpers library, it's very easy and simple... you will need Excel 2000 or later installed on the machine.

您可以使用filehelper库的ExcelStorage类,它非常简单……您需要在机器上安装Excel 2000或更高版本。

The FileHelpers is a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.

filehelper是一个免费且容易使用。net库从文件、字符串或流中的固定长度或分隔记录中导入/导出数据的工具。

#1


25  

Unless you have Excel installed on the Server/PC or use an external tool (which is possible without using Excel Interop, see Create Excel (.XLS and .XLSX) file from C#), it will fail. Using the interop requires Excel to be installed.

除非您已经在服务器/PC上安装了Excel,或者使用了外部工具(不使用Excel互操作也可以),请参阅Create Excel(。XLS和. xlsx)文件来自c#,它将失败。使用interop需要安装Excel。

#2


37  

If you're interested in making .xlsx (Office 2007 and beyond) files, you're in luck. Office 2007+ uses OpenXML which for lack of a more apt description is XML files inside of a zip named .xlsx

如果你对制作.xlsx (Office 2007及其他)文件感兴趣,那你就很幸运了。Office 2007+使用OpenXML,由于缺少更恰当的描述,它是一个名为.xlsx的zip文件中的XML文件

Take an excel file (2007+) and rename it to .zip, you can open it up and take a look. If you're using .NET 3.5 you can use the System.IO.Packaging library to manipulate the relationships & zipfile itself, and linq to xml to play with the xml (or just DOM if you're more comfortable).

取一个excel文件(2007+)并将其重命名为.zip,您可以打开它并查看它。如果你使用。net 3.5,你可以使用System.IO。打包库以操作关系& zipfile本身,并将linq到xml用于使用xml(如果您觉得更舒服的话,也可以只使用DOM)。

Otherwise id reccomend DotNetZip, a powerfull library for manipulation of zipfiles.

否则,id reccomend DotNetZip,一个用于处理zipfiles的powerfull库。

OpenXMLDeveloper has lots of resources about OpenXML and you can find more there.

OpenXMLDeveloper有很多关于OpenXML的资源,您可以在那里找到更多。

If you want .xls (2003 and below) you're going to have to look into 3rd party libraries or perhaps learn the file format yourself to achieve this without excel installed.

如果您想要.xls(2003及以下),您将需要查看第三方库,或者自己学习文件格式,以便在不安装excel的情况下实现这一点。

#3


22  

Try EPPlus if you use Excel 2007. Supports ranges, cellstyling, charts, shapes, pictures and a lot of other stuff

如果您使用Excel 2007,请尝试使用EPPlus。支持范围,细胞造型,图表,形状,图片和许多其他东西。

#4


8  

An interop calls something else, it's an interoperability assembly, so you're inter-operating with something...in this case Excel, the actual installed Excel.

互操作调用其他东西,它是一个互操作程序集,所以你是在与某些东西进行互操作…在本例中,实际安装的Excel。

In this case yes, it will fail to run because it depends on excel, you're just calling excel functions. If they don't have it installed...out of luck.

在这种情况下,它不会运行因为它依赖于excel,你只是调用excel函数。如果他们没有安装…运气不好。

There are methods to generate without Excel, provided the 2007 file format is ok, here's a few:

有一些方法可以在没有Excel的情况下生成,如果2007年的文件格式是ok的,这里有一些:

as I said though, this is the 2007 format, normally if anything, that's the deal-breaker.

就像我说的,这是2007年的格式,通常来说,如果有什么不同的话,那就是交易的破坏者。

#5


6  

There are a handful of options:

有几个选择:

  • NPOI - Which is free and open source.
  • NPOI是免费和开源的。
  • Aspose - Is definitely not free but robust.
  • Aspose——绝对不是免费的,但是很有活力。
  • Spreadsheet ML - Basically XML for creating spreadsheets.
  • 电子表格ML -用于创建电子表格的基本XML。

Using the Interop will require that the Excel be installed on the machine from which it is running. In a server side solution, this will be awful. Instead, you should use a tool like the ones above that lets you build an Excel file without Excel being installed.

使用Interop需要将Excel安装在运行它的机器上。在服务器端解决方案中,这将非常糟糕。相反,您应该使用类似上面的工具来构建Excel文件,而无需安装Excel。

If the user does not have Excel but has a tool that will read Excel (like Open Office), then obviously they will be able to open it. Microsoft has a free Excel viewer available for those users that do not have Excel.

如果用户没有Excel,但是有一个工具可以读取Excel(比如Open Office),那么很明显他们可以打开它。微软为那些没有Excel的用户提供免费的Excel查看器。

#6


4  

Use OleDB, you can create, read, and edit excel files pretty easily. Read the MSDN docs for more info:

使用OleDB,你可以很容易地创建、读取和编辑excel文件。更多信息请阅读MSDN文档:

http://msdn.microsoft.com/en-us/library/aa288452(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa288452(VS.71). aspx

I've used OleDB to read from excel files and I know you can create them, but I haven't done it firsthand.

我使用OleDB来读取excel文件,我知道你可以创建它们,但是我还没有直接做。

#7


1  

You could use the ExcelStorage Class of the FileHelpers library, it's very easy and simple... you will need Excel 2000 or later installed on the machine.

您可以使用filehelper库的ExcelStorage类,它非常简单……您需要在机器上安装Excel 2000或更高版本。

The FileHelpers is a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.

filehelper是一个免费且容易使用。net库从文件、字符串或流中的固定长度或分隔记录中导入/导出数据的工具。