.net - Comment créer un fichier Excel (.XLS et .XLSX) en C# sans installer Microsoft Office

Mots clés : c#.netexcelfile-ioc#

meilleur 2 Réponses .net - Comment créer un fichier Excel (.XLS et .XLSX) en C# sans installer Microsoft Office

vote vote

99

//Create the data set and table DataSet ds = new DataSet("New_DataSet"); DataTable dt = new DataTable("New_DataTable");  //Set the locale for each ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture; dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;  //Open a DB connection (in this example with OleDB) OleDbConnection con = new OleDbConnection(dbConnectionString); con.Open();  //Create a query and fill the data table with the data from the DB string sql = "SELECT Whatever FROM MyDBTable;"; OleDbCommand cmd = new OleDbCommand(sql, con); OleDbDataAdapter adptr = new OleDbDataAdapter();  adptr.SelectCommand = cmd; adptr.Fill(dt); con.Close();  //Add the table to the data set ds.Tables.Add(dt);  //Here's the easy part. Create the Excel worksheet from the data set ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds); 
vote vote

81

using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\temp\\test.xls;Extended Properties='Excel 8.0;HDR=Yes'")) {   conn.Open();   OleDbCommand cmd = new OleDbCommand("CREATE TABLE [Sheet1] ([Column1] string, [Column2] string)", conn);   cmd.ExecuteNonQuery(); } 

Questions similaires