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
| using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
//using System.Xml.Linq;
using System.Configuration;
using System.Data;
namespace xlboucleajout
{
public partial class _Default : System.Web.UI.Page
{
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// open workbook and add data to cells (woooooooooorksssss)
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
string workbookPath = @"c:/EXCEL.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
//-----jusqu ici il marche, il a ouvert excel------
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
// ici c'est l'etablissement de la conx avc la base
SqlConnection con = new SqlConnection(GetConnectionString());
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT id FROM MyTable", con);
DataSet ds = new DataSet();
sda.Fill(ds);
//recuperer resultat requete
int i;
String resultatreq = ds.Tables[0].Rows[i]["MyTable"].ToString().Trim();
//ecrire le resultat dans excel
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A"+i, "A"+i);
excelCell.Value2 = resultatreq;
}
}
}
} |
Partager