export sqlserver to excel
Bonjour;
je débute en asp.net je cherche à exportet les données d'une table sqlserver to une nouvelle feuille excel et pouvoir travailler normalement avec excel.
ce code n'affiche aucune erreur ni résultat non plus aidez moi s'il vs plait
file: default.aspx.cs
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 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
using System;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
private System.Data.DataTable GetData()
{
SqlConnection conn = new SqlConnection(@"server=(.\SQLEXPRESS);uid=sa;pwd=xxxx;database=examen.mdf;");
SqlDataAdapter adapter = new SqlDataAdapter("select * from ETUDIANT", conn);
DataSet myDataSet = new DataSet();
try
{
adapter.Fill(myDataSet, "ETUDIANT");
}
catch (Exception ex)
{
}
return myDataSet.Tables[1];
}
private void button1_Click(object sender, System.EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
DataTable table = GetData();
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = true;
}
} |
aidezmoiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
mnt cest plutot le prob de conn à la bd qui me dérange s'il vs plait aidez moi:
je peux plus tester mon code car j'ai tjrs les msg Échec d'une tentative d'attachement d'une base de données nommée automatiquement pour le fichier C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test.mdf. Il existe une base de données du même nom ou le fichier spécifié ne peut être ouvert ou il se trouve sur un partage UNC.