从C ++读取Excel列 [英] read excel column from c++
问题描述
有人可以帮我提供示例代码吗?
我正在编写一个程序,它将根据用户的选择打开word或excel文件.
我需要逐列阅读并将其写入结构
在结构中进行一些解析和计算,然后编写整个
结构恢复到excel.
谢谢
Hi,
Can anyone help me with a sample code?
I''m writing a program which will open word or excel file upon user''s selection.
I need to read column by colum and write it to a structure,
do some parsing and calculation in the structure, and then write the whole
structure back to excel.
Thanks
推荐答案
您可以使用.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;
}
您也可以使用精美的文章详细讨论您的问题:
C ++的简要介绍以及与Excel的接口
http://www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf [ ^ ]
如何使用MFC自动执行Excel和浏览工作表
http://support.microsoft.com/default.aspx?scid=kb;zh-我们; 178782 [ ^ ]
一个非常易于使用的Excel XML导入导出库
一个非常易于使用的Excel XML导入导出库 [ ^ ]
CSpreadSheet-用于读取和写入Excel和文本分隔的电子表格的类
CSpreadSheet-读写Excel的类和文本分隔的电子表格 [ ^ ]
Microsoft Excel自动化类
http://www.codeguru.com/cpp/data/mfc_database/microsoftexcel/article.php/c11745/Microsoft-Excel-Automation-Class.htm [
Also you can use the excellent articles discussing in detail your question :
A brief introduction to C++ and Interfacing with Excel
http://www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf[^]
How To Use MFC to Automate Excel and Navigate Worksheets
http://support.microsoft.com/default.aspx?scid=kb;en-us;178782[^]
A Very Easy to Use Excel XML Import-Export Library
A Very Easy to Use Excel XML Import-Export Library[^]
CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet
CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet[^]
Microsoft Excel Automation Class
http://www.codeguru.com/cpp/data/mfc_database/microsoftexcel/article.php/c11745/Microsoft-Excel-Automation-Class.htm[^]
该代码项目网站是实际上很棒的工具,只需搜索,您会发现一些有趣的东西,例如:
通过C ++访问Excel电子表格 [
The codeproject site is actually amazing tool, just search and you will find something interesting, like this:
Accessing Excel Spreadsheets via C++[^]
嘿,我使用类似的程序,我以.csv(逗号分隔值)格式阅读excel文件,这是一段代码:
Hey I work with similar programs I read excel files, in a .csv (comma seperated values)format and this a piece of code:
Function1()
{
//This function opens the input file,That is the file which has to be read.
CString m_fname;
CFileDialog dlg(true, NULL, _T("*.csv"), NULL, false, NULL);
dlg.m_ofn.lpstrTitle = _T("Select data file");
dlg.m_ofn.lpstrFilter = _T("Npd Files (*.npd)\0*.npd\0CSVFiles(*.csv)\0*.csv\0All Files\0*.*\0\0");
dlg.m_ofn.lpstrInitialDir = m_fname; /
if (dlg.DoModal() == IDOK)
{
m_fname = dlg.GetPathName();
function2(m_fname);
}
}
___________________________________________________________________________
//Open the output file where you want to save your processed values
Function1()
{
CStdioFile csf;
CStdioFile outFile;
CString buffer;
CString outDir;
CEvoFileDlg flDlg(EVODLG_SAVE, outDir, _T("*.txt"));
flDlg.SetTitle(_T("Select Report File"));
flDlg.SetFilter(_T("Text File (*.txt)|*.txt|All Files (*.*)|*.*||"));
if (flDlg.ShowModal() != IDOK)
return;
// Open output file
CStdioFile out;
if (!out.Open(dlg.GetPathName(), CFile::typeText | CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite))
{
Evo::Alarm(23, ALM_NOTIFY, _T("Unable to open file: %s"), m_outDir);
return;
}
double val1, val2;
if (csf.Open(fname, CFile::modeRead))
{
while (csf.ReadString(buffer) )
{
//read the file
out = swscanf_s(buffer,_T("%f,%f" ), val1, val2);
//Your calculations here
//now write into the chosen file.
out.WriteString(your function that returns a string of values)
}
csf.close();
}
}
希望对您有帮助
Hope this helps
这篇关于从C ++读取Excel列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!