Excel-Daten auslesen mit C#

by Flo 14. January 2008 22:20

Gestern hatte ich das Bedürfnis, Daten aus einem Excel-File auszulesen und sie in einer .aspx-Seite darzustellen. Das Ganze ist per OleDbConnection sehr easy und leicht zu managen:

String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("List.xls") + ";"
+
"Extended Properties=Excel 8.0;";
OleDbConnection myConn = new OleDbConnection(connectionString);
// Open connection with the database.
myConn.Open();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new OleDbCommand to return data from worksheet.
OleDbCommand myComm =new OleDbCommand("SELECT * FROM myRange1"
, myConn);
// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter da = new OleDbDataAdapter
();
// Pass the Select command to the adapter.
da.SelectCommand = myComm;
// Create new DataSet to hold information from the worksheet.
DataSet ds = new DataSet
();
// Fill the DataSet with the information from the worksheet.
da.Fill(ds, "XLData"
);
// Bind data to DataGrid control.
resultGridView.DataSource = ds.Tables[0].DefaultView;
resultGridView.DataBind();
// Clean up objects.
myConn.Close();

Vielleicht hilft's ja jemandem. :)

Tags: ,

.NET

Comments

8/16/2009 9:55:42 AM #

cash loans

I think your blog has a very cool wordpress template. I allways snoop arround to find cool wordpress templates, The site has nice and unique wordpress templates.

cash loans United States

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

Über mich

Ich bin freiberuflicher Softwareentwickler (.NET, Cocoa, Webentwicklung) und versuche mich als hobbymäßig an der Fotografie.

Seit 1. Oktober 2007 studiere ich in an der Georg-Simon-Ohm-Hochschule in Nürnberg Wirtschaftsinformatik.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar