Question1 :
how to use c++ to read/write excel file without changing it into CSV file?

Question2 :
How to use CSV parser in c++? Are there any examples?

Recommended Answers

All 5 Replies

http://support.microsoft.com/kb/216686

SUMMARY:There are several advantages to writing your Automation code in straight C++. First and foremost, you can do exactly what you want. Next, your code will be smaller, faster, and easier to debug. And finally, you won't be dependent on any libraries.

I have a some class which allows to write in a *.xls (not a *.xlsx) files.

//XLSWriter.h
//---------------------------------------------------------------------------
#ifndef XLS_WRITER_IMPL_H
#define XLS_WRITER_IMPL_H

#include <stdio.h>
//---------------------------------------------------------------------------

#define  CBOF			0x0009
#define  BIT_BIFF5		0x0800
#define  BOF_BIFF5		(CBOF | BIT_BIFF5)

#define  BIFF_EOF		0x000A
#define  DOCTYPE_XLS	0x0010

#define  DIMENSIONS		0x0000

#define BLANK			0x0000
#define DIM_LEN			0x0008
#define LABEL			0x0004


class XLSWriter
{
private:
	// pointer to a file
	FILE* out;
	unsigned short blank;
	unsigned short bof;
	unsigned short tmp;
	int iWordSize;
	// cell attributes
	unsigned char cell_attr[3];
	int iMaxRow;
private:
	void Blank();
	// write into begin of file
	void BOF();
	void DocTypeXls();
	void DIM();
	void WriteEOF();
	void WriteLabel();
	void WriteCellAttr();
public:
	// Creating xls file
	bool CreateXls(const char* FileName);
	// Closing xls file
	void CloseXls();
	// Writing
	void WriteData(const char* Data, int row, int col);
public:
	XLSWriter(int MaxRow = 2000); // max number of rows
	~XLSWriter();
};
//---------------------------------------------------------------------------
#endif
//XLSWriter.cpp
//---------------------------------------------------------------------------
#include "stdafx.h"
#include "XLSWriter.h"
#include <string.h>

XLSWriter::XLSWriter(int MaxRow)
: out(NULL) ,blank(BLANK) ,bof(BOF_BIFF5) ,iMaxRow(MaxRow) ,tmp(BLANK)
{
	iWordSize = sizeof(unsigned short);

	cell_attr[0] = 0xFF; // default cell attributes
	cell_attr[1] = 0xFF;
	cell_attr[2] = 0x00;
}

XLSWriter::~XLSWriter()
{
	if(out) 
		fclose(out);
}

void XLSWriter::Blank()
{
	fwrite(&blank,iWordSize,1,out);
}

void XLSWriter::BOF()
{
	fwrite(&bof,iWordSize,1,out);
	tmp = 0x0006;
	fwrite(&tmp,iWordSize,1,out);
	Blank();
}
//---------------------------------------------------------------------------
void XLSWriter::DocTypeXls()
{
	tmp = DOCTYPE_XLS;
	fwrite(&tmp,iWordSize,1,out);
	Blank();
	Blank();
}

void XLSWriter::DIM()
{
	tmp = DIM_LEN;
	fwrite(&tmp,iWordSize,1,out);
	Blank();
	tmp = (unsigned short) iMaxRow;
	fwrite(&tmp,iWordSize,1,out);
	Blank();
	tmp=10;
	fwrite(&tmp,iWordSize,1,out);
}

void XLSWriter::WriteEOF()
{
	tmp = BIFF_EOF;
	fwrite(&tmp,iWordSize,1,out);
	Blank();
}

void XLSWriter::WriteCellAttr()
{
	fwrite(&cell_attr,sizeof(cell_attr),1,out);
}

void XLSWriter::WriteLabel()
{
	tmp = LABEL;
	fwrite(&tmp,iWordSize,1,out);
}

bool XLSWriter::CreateXls(const char* FileName)
{
	if(!FileName || FileName[0] == 0) 
		return false;
	if(out)  
		CloseXls();

	out = fopen(FileName,"wb");
	if(!out) 
		return false;

	BOF();
	DocTypeXls();
	DIM();

	return true;
}

void XLSWriter::CloseXls()
{
	WriteEOF();
	fclose(out);
	tmp = BLANK;
}

void XLSWriter::WriteData(const char* Data,int row, int col)
{   
	WriteLabel();
	int len = strlen(Data);
	tmp = (unsigned short) 8 + len;
	fwrite(&tmp,iWordSize,1,out);

	tmp = (unsigned short) row;
	fwrite(&tmp,iWordSize,1,out);
	tmp = (unsigned short)col;
	fwrite(&tmp,iWordSize,1,out);
	WriteCellAttr();
	unsigned char btmp = (unsigned char) len;
	fwrite(&btmp,1,1,out);
	fwrite(Data,btmp,1,out);
}

I am not the author of this class. This class from a free library. I've found it in the google.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.