Je commence avec le gros morceau: la création du fichier et l'écriture de la première page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
internal static void WriteXlsFile(BackgroundWorker pWorker, string pFileName, DataTable pValueDT, DataTable pInfoDT)
{
Workbook Book = new CarlosAg.ExcelXmlWriter.Workbook();
WorksheetStyle NormalStyle = Book.Styles.Add("Normal");
WorksheetStyle BoldStyle = Book.Styles.Add("Bold");
BoldStyle.Font.Bold = true;
Worksheet InfoSheets = Book.Worksheets.Add("Information");
InfoSheets.Table.Rows.Add();
foreach (DataRow myDR in pInfoDT.Rows)
{
WorksheetRow Row = new WorksheetRow();
for (int i = 0; i < pInfoDT.Columns.Count; i++)
{
Row.Cells.Add(myDR[i].ToString(), DataType.String, "Normal");
}
InfoSheets.Table.Rows.Add(Row);
}
Worksheet ValueSheets = Book.Worksheets.Add("Nom");
WorksheetRow WRow = new WorksheetRow();
pWorker.ReportProgress(40);
foreach (DataColumn myDC in pValueDT.Columns)
{
WRow.Cells.Add(myDC.ColumnName, DataType.String, "Bold");
}
ValueSheets.Table.Rows.Add(WRow);
pWorker.ReportProgress(60);
foreach (DataRow myDR in pValueDT.Rows)
{
WorksheetRow Row = new WorksheetRow();
for (int i = 0; i < pValueDT.Columns.Count; i++)
{
Row.Cells.Add(myDR[i].ToString());
}
ValueSheets.Table.Rows.Add(Row);
}
try
{
Book.Save(pFileName + @".Xls");
File.SetAttributes(pFileName + @".Xls", FileAttributes.ReadOnly);
pWorker.ReportProgress(80);
return ReadXlsFile(pFileName);
}
catch (Exception Ex)
{
throw Ex;
}
} |
La fonction prend plusieurs paramètre en entrée:
- Un background worker: la fonction pouvant prendre au mieux 5 secondes de traitement, il permet d'éviter de figer votre UI (plus d'information à ce sujet: ici )
- Un string contenant le nom du fichier.
- Un datatable contenant les valeurs de la feuille à ajouter.
- Un datatable contenant les informations (page de garde etc)
Alors décorticage du code ^^:
Workbook Book = new CarlosAg.ExcelXmlWriter.Workbook();
Création du nouveau classeur
1 2 3
| WorksheetStyle NormalStyle = Book.Styles.Add("Normal");
WorksheetStyle BoldStyle = Book.Styles.Add("Bold");
BoldStyle.Font.Bold = true; |
Définition des styles d'écriture.
Worksheet InfoSheets = Book.Worksheets.Add("Information");
Création de la feuille (nomée ici "information" puisqu'elle va contenir la page... d'information, merci ceux qui suive
)
1 2 3 4 5 6 7 8 9 10
| InfoSheets.Table.Rows.Add();
foreach (DataRow myDR in pInfoDT.Rows)
{
WorksheetRow Row = new WorksheetRow();
for (int i = 0; i < pInfoDT.Columns.Count; i++)
{
Row.Cells.Add(myDR[i].ToString(), DataType.String, "Normal");
}
InfoSheets.Table.Rows.Add(Row);
} |
Je saute une ligne qui, dans les autres feuille, est la ligne des titres.
Ensuite viens deux boucle imbriquée, la boucle intérieur ajoute toutes les cellules dans la ligne (Row). La boucle extérieur quand à elle, crée puis ajoute la Row remplie par la boucle intérieur.
1 2 3 4 5 6 7 8 9
| WorksheetRow WRow = new WorksheetRow();
pWorker.ReportProgress(40);
foreach (DataColumn myDC in pValueDT.Columns)
{
WRow.Cells.Add(myDC.ColumnName, DataType.String, "Bold");
}
ValueSheets.Table.Rows.Add(WRow); |
Cette boucle crée la ligne des titres de la page en cours.
1 2 3 4 5 6 7 8 9 10 11
| try
{
Book.Save(pFileName + @".Xls");
File.SetAttributes(pFileName + @".Xls", FileAttributes.ReadOnly);
pWorker.ReportProgress(80);
}
catch (Exception Ex)
{
throw Ex;
} |
Cela sauve le fichier et le passe en "lecture seule" afin de limité les modifications extérieur.
Partager