bonjour,

sur l'une de mes applications en c#, j'exporte un datagridview au format excel à l'aide d'un bouton ( à l'effigie d'une disque ) et je remarque les limites de la dll InteroExcel, je m'explique.
Je suis amené à executer des requetes sql, qui s'affiche dans mon datagrid, certaines requetes me donnent un nombre de résultat potable ( entre 100 et 200lignes) et j'arrive à enregistrer facilement mon fichier excel.
Or nous avons découvert que lorsque la requête renvoie 4000 voir 5000 lignes, lorsque nous cliquons sur la petite disquette pour enregistrer au format excel, nous avons le message suivant (voir pièce jointe ) :
system.runetime.interopService.ComException etc.... Hresult 0x800A03EC.

Nous n'arrivons donc pas à enregistrer le datagridview au format excel.

Existe il un autre moyen qui nous permettrais d'éviter ce genre d'erreur?

Voici le code du bouton c# utilisé pour enregistrer au format excel :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
private void BtnSave_Click(object sender, EventArgs e)
{
if (datagrid.RowCount > 0)
{
	String path = ".....";
	DialogResult res = fbdExportExcel.ShowDialog();
	if (res == DialogResult.OK)
	{
		path = fbdExportExcel.SelectedPath;
		String title = "";
		if (panParam.Visible == false)
		{
			title = lblTitle.Text;
		}
		else
		{
			title = lblTitle.Text;
		}
		classe.fonctionExporter(madatagrid, path + @"\" + title + ".xlsx", "Export ", 0);
	}
}
}
puis ma fonction exporter

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public static void fonctionexporter(DataGridView dgView, String unFichier, string strEnteteDeFichier, int startColumn)
{
int i = 0;
int j = 0;
try
{
 
 
ExcelApplication excel = new ExcelApplication();
excel.DefaultWebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
 
Workbook exbook = (Workbook)excel.Workbooks.Add(Missing.Value);
Worksheet exsheet = (Worksheet)excel.ActiveSheet;
 
Range r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
//formatage de lentete
foreach (DataGridViewColumn ch in dgView.Columns)
{
	if (ch.Visible && ch.CellType.Name != "DataGridViewImageCell")
	{
		r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
		exsheet.Cells[1, i + 1] = ch.HeaderText.Trim();
		r.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
		r.Font.Bold = true;
		r.Font.Color = XlRgbColor.rgbBlue;
		r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
		r.EntireColumn.AutoFit();
		i++;
	}
}
 
j = 2;
 
foreach (DataGridViewRow uneLigne in dgView.Rows)
{
	i = 1;
	foreach (DataGridViewColumn uneColonne in dgView.Columns)
	{
		if (uneColonne.Visible && uneColonne.CellType.Name != "DataGridViewImageCell")
		{
			r = exsheet.get_Range(Convert.ToChar(65 + i - 1).ToString() + j.ToString(), Missing.Value);
			exsheet.Cells[j, i] = "'" + uneLigne.Cells[uneColonne.Name].Value.ToString().Trim();
			r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
			r.EntireColumn.AutoFit();
			i++;
		}
	}
	exsheet.Columns.AutoFit();
	j++;
}
 
exsheet.SaveAs(unFichier, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excel.Quit();
 
 
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());//ex.Message
}
 
}
en gros, lorsque l'on tente de sauvegarder dans un lourd fichier, on tombe dans le msgbox :s ...
Y a t-il une alternative?
Merci pour votre aide