Bonjour,
Je tente d'utiliser le SDK 2,5 que j'ai installé pour créer des documents Word.
J'ai trouvé un exemple intéressant sur ce site. Malheureusement j'ai une erreur à l’exécution mais pas à la compilation.
Mes using :
using namespace System;
using namespace System::IO;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
using namespace System::Text;
using System::String;
using namespace System::Xml;
using namespace System::IO:ackaging;
using namespace DocumentFormat::OpenXml;
using namespace DocumentFormat::OpenXml::Wordprocessing;
using namespace DocumentFormat::OpenXml:ackaging;
Le code :
const int borderSize = 12;
WordprocessingDocument^ myDoc = WordprocessingDocument::Create("FichierWord",WordprocessingDocumentType:ocument);
MainDocumentPart^ mainDocumentPart = myDoc->AddMainDocumentPart();
mainDocumentPart->Document = gcnew Document();
Body^ body = mainDocumentPart->Document->AppendChild(gcnew Body());
Table^ table = gcnew Table();
TableRow^ trHeader = gcnew TableRow();
TableProperties^ tblProperties = gcnew TableProperties();
TopBorder^ topBorder = gcnew TopBorder();
BottomBorder^ bottomBorder = gcnew BottomBorder();
LeftBorder^ leftBorder = gcnew LeftBorder();
RightBorder^ rightBorder = gcnew RightBorder();
InsideHorizontalBorder^ insideHorizontalBorder = gcnew InsideHorizontalBorder();
InsideVerticalBorder^ insideVerticalBorder = gcnew InsideVerticalBorder();
topBorder->Val = BorderValues::Thick;
topBorder->Size = borderSize;
bottomBorder->Val = BorderValues::Thick;
bottomBorder->Size = borderSize;
leftBorder->Val = BorderValues::Thick;
leftBorder->Size = borderSize;
rightBorder->Val = BorderValues::Thick;
rightBorder->Size = borderSize;
insideHorizontalBorder->Val = BorderValues::Thick;
insideHorizontalBorder->Size = borderSize;
insideVerticalBorder->Val = BorderValues::Thick;
insideVerticalBorder->Size = borderSize;
TableBorders^ tblBorders = gcnew TableBorders();
tblBorders->TopBorder = topBorder;
tblBorders->BottomBorder = bottomBorder;
tblBorders->LeftBorder = leftBorder;
tblBorders->RightBorder = rightBorder;
tblBorders->InsideHorizontalBorder = insideHorizontalBorder;
tblBorders->InsideVerticalBorder = insideVerticalBorder;
tblProperties->TableBorders = tblBorders;
table->AppendChild(tblProperties);
for(int i = 0; i < dataGridView1->Columns->Count; i++)
{
TableCell^ tc = gcnew TableCell();
Paragraph^ pg = gcnew Paragraph();
Run^ run = gcnew Run();
run->AppendChild(gcnew DocumentFormat::OpenXml::Wordprocessing::Text(this->dataGridView1->Columns[i]->Name));
pg->AppendChild(run);
tc->AppendChild(pg);
trHeader->AppendChild(tc);
}
table->AppendChild(trHeader);
int ligne = 0;
while(ligne < dataGridView1->RowCount)
{
TableRow^ tr = gcnew TableRow();
for(int i = 0; i < dataGridView1->Columns->Count; i++)
{
TableCell^ tc = gcnew TableCell();
Paragraph^ pg = gcnew Paragraph();
Run^ run = gcnew Run();
run->AppendChild(gcnew DocumentFormat::OpenXml::Wordprocessing::Text(this->dataGridView1->Rows[ligne]->Cells[i]->Value->ToString()));
pg->AppendChild(run);
tc->AppendChild(pg);
tr->AppendChild(tc);
}
table->AppendChild(tr);
ligne++;
}
body->AppendChild(table);
mainDocumentPart->Document->Save();
delete myDoc;
-----------------------------------------------------------------------------
A la compilation, tout se passe bien mais à l'exécution, j'ai cette erreur :
Une exception non gérée du type 'System.BadImageFormatException' s'est produite dans System.Windows.Forms.dll
Informations supplémentaires*: Impossible de charger le fichier ou l'assembly 'DocumentFormat.OpenXml,
Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' ou une de ses dépendances.
Cet assembly a été construit par un runtime plus récent que le runtime actuellement chargé et ne peut pas être chargé.
Si quelqu'un voit d'où cela peut venir, cela m'aiderai beaucoup.
Au fait, je code avec Visual C++ 2008 Express.
Merci.
Partager