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 62 63 64 65 66 67 68
|
public bool ImportDataGridToExcel_WithTemplate(string filepath, string templatepath, int startrow, int startcolumn, DataView dv)
{
bool resultflag = false;
int indexofsheetname = 0;
//Gestion du fichier à écrire
if (File.Exists(filepath))
{
File.Delete(filepath);
}
try
{
msExcel = new Microsoft.Office.Interop.Excel.Application();
if (File.Exists(templatepath))
{
//Ouverture du template
workbook = msExcel.Workbooks.Open(templatepath,0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
}
//Sélection de la feuille
sheets = workbook.Sheets;
for (int i = 1; i <= sheets.Count; i++)
{
//sélection du worksheet courant
worksheet = (Worksheet)sheets.get_Item(i);
//Test si la feuille est la bonne
if (worksheet.Name.ToString().Equals("LISTE"))
{
//Oui, sauvegarde de l'index
indexofsheetname = i;
}
}
worksheet = (Worksheet)sheets.get_Item(indexofsheetname);
Row = startrow;
Col = startcolumn;
//Remplir la feuille
for (int i = 0; i < dv.Table.Rows.Count; i++)
{
for (int j = 0; j < dv.Table.Columns.Count; j++)
{
fillExcelCell(worksheet, Row, Col++, dv[i][j].ToString());
}
Col = 1;
Row++;
}
//Fermeture d'Excel
try
{
workbook.Close(true, filepath, Type.Missing);
msExcel.UserControl = false;
msExcel.Quit();
msExcel = null;
killExcel();
}
catch (COMException)
{
Console.WriteLine("Erreur dans la fermeture automatique d'Excel");
}
}
catch (Exception ex)
{
MessageBox.Show("Erreur :" + ex.Message);
}
return resultflag;;
} |
Partager