C语言 从C中的excel文件中读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3230706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read from excel file in C
提问by parin
I want to read from an excel file in C. The excel 2007 file contains about 6000 rows and 2 columns. I want to store the contents in a 2-D array in C. If there exists a C library or any other method then please let me know.
我想从 C 中的 excel 文件中读取数据。 excel 2007 文件包含大约 6000 行和 2 列。我想将内容存储在 C 中的二维数组中。如果存在 C 库或任何其他方法,请告诉我。
回答by Jerry Coffin
Excel 2007 stores the data in a bunch of files, most of them in XML, all crammed together into a zip file. If you want to look at the contents, you can rename your .xlsxto whatever.zipand then open it and look at the files inside.
Excel 2007 将数据存储在一堆文件中,其中大部分是 XML,所有这些文件都塞进了一个 zip 文件中。如果你想看里面的内容,你可以将你的重命名.xlsx为whatever.zip,然后打开它看看里面的文件。
Assuming your Excel file just contains raw data, and all you care about is reading it (i.e., you do notneed/want to update its contents and get Excel to open it again), reading the data is actually pretty easy. Inside the zip file, you're looking for the subdirectory xl\worksheets\, which will contain a number of .xmlfiles, one for each worksheet from Excel (e.g., a default workbook will have three worksheets named sheet1.xml, sheet2.xmland sheet3.xml).
假设你的Excel文件只包含原始数据,以及所有你关心的是阅读它(即你没有需要/想更新其内容,并得到Excel中再次打开它),读取的数据实际上是很容易的。在 zip 文件中,您要查找子目录xl\worksheets\,该子目录将包含许多.xml文件,每个 Excel 工作表对应一个文件(例如,默认工作簿将包含三个名为sheet1.xml、sheet2.xml和 的工作表sheet3.xml)。
Inside of those, you're looking for the <sheet data>tag. Inside of that, you'll have <row>tags (one for each row of data), and inside of them <c>tags with an attribute r=RCwhere RCis replaced by the normal row/column notation (e.g., "A1"). The <c>tag will have nested <v>tag where you'll find the value for that cell.
在这些里面,你正在寻找<sheet data>标签。在其中,您将拥有<row>标签(每行数据一个),并且在它们内部<c>带有一个属性的标签,r=RC其中RC被普通的行/列符号(例如,“A1”)替换。该<c>标签将具有嵌套<v>标签,您可以在其中找到该单元格的值。
I do feel obliged to add a warning though: while reading really simple data can indeed be just this easy, life can get a lotmore complex in a hurry if you decide to do much more than reading simple rows/columns of numbers. Trying to do anything even slightlymore complex than that can get a lotmore complex in a hurry.
我觉得有责任增加一个警告,虽然:读取非常简单的数据的确可以这样简单,生活也得到了很多匆忙更复杂,如果你决定做的比读数的简单的行/列等等。尝试做任何事情,甚至稍微比这更复杂的可以得到很多匆忙更加复杂。
回答by Romain Hippeau
You have several choices:
1) Save your excel worksheet to a csv file and parse that.
2) Use the COM API (Windows proprietary and tricky)
3) See this linkfor a C++ class that you could modify.
您有多种选择:
1) 将您的 excel 工作表保存到一个 csv 文件并进行解析。
2) 使用 COM API(Windows 专有且棘手)
3)有关可以修改的 C++ 类,请参阅此链接。

