export datagridview vers excel
bonjour,
je cherche à exporter un datagridview vers Excel. j'ai trouvé une multitude d'exemples, tous incluant le using suivant :
using Excel = Microsoft.Office.Interop.Excel;
Je suis sous SharpDevelop version 4.1 et quand j'ajoute ce using, le compilateur m'affiche l'erreur suivante :
"The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) (CS0234) "
quelqu'un a t il une idée du module qui me manque ?
bonne soirée
amicalement
voici le principe de mon code
désolé, je n'avais pas vu que quelqu'un me demandais ma solution : la voici :
ma datagridview s'appelle dgv
Code:
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
| void B_Export_EXCELClick(object sender, EventArgs e)
{
int i,j;
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
//Funny
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
//Fixed:(Microsoft.Office.Interop.Excel.Worksheet)
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "nom de la feuille excel";
// storing header part in Excel
for (i = 1; i < dgv_ASL.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dgv.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (i = 0; i < dgv.Rows.Count - 1; i++)
{
for (j = 0; j < dgv.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dgv.Rows[i].Cells[j].Value.ToString();
}
}
workbook = null;
app = null;
} |