Galactic Civilizations 3
Generate an Excel file in C with a CSV file

Generate an Excel file in C with a CSV file

Category: Programming.
Posted by on the 16/01/2014.
Last update on the 16/01/2014.


This tutorial explains how to generate an Excel file (.xls) in C with a CSV file (.csv).


This tutorial requires some basic knowledge in C and in Visual Basic. This tutorial doesn't explain how to create a CSV file.

Application required

  • Microsoft Excel.

Excel macro

First of all, we open Microsoft Excel and create an Excel macro. This macro should be launched when Excel is started in order to generate automatically the Excel file (.xls). To do that, the name of the macro must be « auto_open() ».

We open the CSV file with the following command:

Workbooks.OpenText Filename:=pathToCSV,
Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited,
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
Tab:=False, Semicolon:=True,Comma:=False, Space:=False,
Other:=False, FieldInfo:=Array(1, 1),

We specify a path to the CSV with « Filename:= » (for example: C:\\tuto.csv). We specify also that we use the « semicolon » as separator by setting Semicolon to « True ».

Now that we have loaded the CSV file, we change his name to create an Excel file (.xls) and then we save it.

'We change the file extention
myExcelFile = Left(myCSVFile, Len(myCSVFile) - 3) &
'The Excel file is not yet created.
'We save the file

ActiveWorkbook.SaveAs Filename:=pathToMyExcelFile,
'We close the Excel file
Workbooks(myExcelFile).Close SaveChanges:=False
'on ferme le fichier contenant la macro
Workbooks(myMacroXLSMFile).Close SaveChanges:=False

Now that we have finished the macro, we can save it and save the document in file .xlsm.

C code

Now that the Excel file containing the macro is finished, we will launch excel with the system function.

String msDosCommand = "DIR \"C:\\Program Files\\Microsoft Office\\Office12\" & EXCEL.exe \"C:\\MacroFile.xlsm\"";

You like this tutorial ?
Like this website in order to promote it on facebook !

You like this tutorial ? Share it with your friends !
Partager sur Facebook Partager sur Twitter Partager sur Myspace Partager sur Stumbleupon Soumettre sur Reddit Partager sur Digg Ajouter à vos favoris Technorati Ajouter à vos favoris Live Ajouter à vos favoris Google Ajouter sur vos favoris Yahoo