在C ++中读取和写入Excel文件 [英] Reading from and writing to Excel files in C++

查看:104
本文介绍了在C ++中读取和写入Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C ++读取和写入Excel文件的最佳和最简单的方法是什么?如果有人可以帮助我,我将不胜感激.

What is the best and the simplest way for Reading from and writing to Excel files in C++?I''ll appreciate if anybody can help me.

推荐答案

您可以使用.csv文件-这是逗号分隔的值.

每个单元格以逗号分隔,每行以CR(endl)结尾.
这是一个简单的例子

You could use a .csv file - which is comma seperated values.

Each cell is seperated with a comma, and each row is ended with a CR ( endl).
Here is a simple example

#include <iostream>
#include <fstream>


using namespace std;


int main(int args, char * argv[])
{
ofstream MyExcelFile;
MyExcelFile.open("C:\\test.csv");

MyExcelFile << "First Name, Last Name, Middle Initial" << endl;
MyExcelFile << "Michael, Jackson, B." << endl;
MyExcelFile.close();
return 0;
} 



或者您可以阅读以下文章,希望对您有所帮助:

CSpreadSheet-读写Excel的类和文本分隔的电子表格 [ ^ ]

http://www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf [ ^ ]

而这个:
http://support.microsoft.com/default.aspx?scid=kb;zh-我们; 178782 [ ^ ]



Or you can read following articles, I hope it will help you:

CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet[^]

http://www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf[^]

and this:
http://support.microsoft.com/default.aspx?scid=kb;en-us;178782[^]


Excel有很多格式可以保存表格.如果您可以选择这些格式之一,那么我可以为您提供帮助. (如果您必须使用旧的excel格式,那就麻烦了!)您应该将excel表保存为Excel 2002 XML或Excel 2003 XML格式.然后,您可以使用简单的xml解析器对其进行读取/写入. ( http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats [ http://msdn.microsoft.com /en-us/library/aa140066%28office.10%29.aspx [
Excel has a lot of formats to save out a table. If you can choose one of these formats then I can give you some help. (If you have to work old excel formats then you are in trouble!) You should save your excel tables to Excel 2002 XML or Excel 2003 XML format. Then you can read/write it with a simple xml parser. (http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats[^])

Here is a refernce to the xml tags: http://msdn.microsoft.com/en-us/library/aa140066%28office.10%29.aspx[^]
If all you want is just a table that has text inside its cells, then dont read the above reference and here is my advice: create a small table (maybe 4x5) and save it as Excel 2002 or 2003 xml. Leave some random cells empty inside your table. Then open the xml file with a text editor/viewer and check out its contents. Most of the tags are just for excel so you can ignore them, you will find out which tags/attributes are important at all. (You can start delete the tags you find unimportant and check if excel can still read the table until you simplified the table to minimal - thats what you will have to save out from your code!) There is only one big mistake you can make, sometimes excel skips some empty columns when it finds some empty cells (this is why you should leave some random cells empty) end then it gives an index to the next nonempty cell in a row without saving the empty cells on the left of it. Thats it!

We use this method to give designers a tool (excel) to tweak the parameters of our program. This solution is so primitive that it can make totally platform independent! Note that you can put in some column/row headers and some coloring for your ppl and your program can ignore those. Later if you have to save space you can trim down the excel file or can convert it to a size optimized custom format.


您将在此网站上找到许多C ++/Excel接口项目.例如保存Excel 2.1工作簿.

至于商业链接: Libxl 似乎做得很好.
You will find many projects of C++ / Excel interfaces on this website. For example Saving Excel 2.1 Workbook.

As for a commercial link: Libxl seems to do well.


这篇关于在C ++中读取和写入Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆