Ecriture de texte dans excel

Contenu du snippet

ce code est un exemple de base pour l'utilisation d'Excel
il écrit hello dans une cellule
On met en forme le texte.
bon je sais il est très basique mais ça peut toujours servir.

Source / Exemple :


#include <stdio.h>
#import "C:\Program Files\Microsoft Office\Office\MSO9.DLL" no_namespace rename("DocumentProperties","DocumentPropertiesXL")
#import "C:\Program Files\Fichiers communs\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_namespace
#import "C:\Program Files\Microsoft Office\Office\EXCEL9.OLB"  rename("ExitWindows", "ExitWindowsWD") rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB") no_dual_interfaces

using namespace std;

int main()
{	

  // Initialize COM
  CoInitialize(NULL);
  try
  {

    Excel::_ApplicationPtr excel;
    // Initialize Excel and make sure it's initialized
    HRESULT hr = excel.CreateInstance(L"Excel.Application");
    if(FAILED(hr))
    {
      char msg[1024] = {0};
      sprintf(msg, "E: There was an error initializing Excel: %d", hr);
       printf(msg);
    }
	excel->PutVisible (true);//Put Excel visible

    //ajoute un workbook
    Excel::_WorkbookPtr workbook = excel->Workbooks->Add(static_cast<long>(Excel::xlWorksheet)); // Create the workbook
    
	//prendre le nom de la feuille active
	Excel::_WorksheetPtr worksheet = excel->ActiveSheet; // Get the active sheet
	
	//nommer la feuille
	worksheet->PutName ("Nom de la feuille");

	//taille du texte
	worksheet->Range["A1:D1"]->Font->Size = 20; 

	//nom de la police de charactère
	worksheet->Range["A1:D1"]->Font->Name = "MS Sérif";
	
	//gras
	worksheet->Range["A1:D1"]->Font->Bold=true;
	
	//italique
	worksheet->Range["A1:D1"]->Font->Italic=true;
	
	//souligné
	worksheet->Range["A1:D1"]->Font->Underline=true;
 
	//couleur de la police
	worksheet->Range["A1:D1"]->Font->ColorIndex = 9L;

	//couleur du fond
    worksheet->Range["A1:D1"]->Interior->ColorIndex = 10L;
	
	//bordure
	worksheet->Range["A1:D1"]->Borders->LineStyle = 6;
	
	//largeur de la colonne
	worksheet->Range["A1"]->Columns->ColumnWidth = 32;
	
	//hauteur de la ligne
	worksheet->Range["A2"]->Rows->RowHeight = 20;

	// This is how you put the values into the worksheet
    worksheet->Range["A1"]->Value = "Hello"; // Set a value

    worksheet->SaveAs("c:\\test.xls"); // Save it
    workbook->Close(); // Close the workbook
    excel->Quit(); // Quit excel
  }
  catch(_com_error &ce)
  {

    // Handle the error
	 
  }
  CoUninitialize();
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.