Client For Microsoft Excel in C#
2010-10-12 11:01
513 查看
转自http://www.codeproject.com/KB/office/microsoftexcelclient.aspx
MicrosoftExcelClient is a small assembly coded in C#.NET which is used to interface with, i.e., read from and write into an Excel sheet in the .NET Framework.
Download source files - 1.42 Kb
Download demo project - 21 Kb
Collapse | Copy Code
Shown below are code snippets from some of the basic functions used in the MicrosoftExcelClient assembly...
Collapse | Copy Code
Collapse | Copy Code
Collapse | Copy Code
Collapse | Copy Code
A list of licenses authors might use can be found here
MicrosoftExcelClient is a small assembly coded in C#.NET which is used to interface with, i.e., read from and write into an Excel sheet in the .NET Framework.
Download source files - 1.42 Kb
Download demo project - 21 Kb
Introduction
In the following article, a brief description has been provided for a client which is used to interface with Microsoft Excel documents. Interfacing to Excel documents (reading, inserting, updating etc.) is a basic task under ADO.NET, and every developer must have a sense of interfacing to Excel documents. TheMicrosoftExcelCientis a simple class any developer can use in their code to interface with Excel documents. It has been created in C# and uses OLEDB.
Inside MicrosoftExcelClient
TheMicrosoftExcelClienthas been created in C#, and in essence is a class which provides an easy to use simple interface to access Excel workbooks in the .NET Framework. OLEDB components are used within C# to access Excel sheets like databases.
Collapse | Copy Code
/// <summary> /// Connects to the source excel workbook /// </summary> OleDbConnection m_ConnectionToExcelBook; /// <summary> /// Reads the data from the document to a System.Data object /// </summary> OleDbDataAdapter m_AdapterForExcelBook;
Shown below are code snippets from some of the basic functions used in the MicrosoftExcelClient assembly...
Open Connection To An Excel Workbook
Collapse | Copy Code
this.m_ConnectionToExcelBook = new OleDbConnection("Provider=Microsoft.Jet" + ".OLEDB.4.0;Data Source=" + this.m_SourceFileName + ";Extended Properties=Excel 8.0;"); this.m_ConnectionToExcelBook.Open();
Read Data From An Excel Sheet
Collapse | Copy Code
DataTable returnDataObject = new DataTable(); OleDbCommand selectCommand = new OleDbCommand("select * from [" + iSheetName + "$]"); selectCommand.Connection = this.m_ConnectionToExcelBook; this.m_AdapterForExcelBook = new OleDbDataAdapter(); this.m_AdapterForExcelBook.SelectCommand = selectCommand; this.m_AdapterForExcelBook.Fill(returnDataObject);
Insert Or Update Format
Collapse | Copy Code
OleDbCommand nonQueryCommand = new OleDbCommand(iQuery); nonQueryCommand.Connection = this.m_ConnectionToExcelBook; nonQueryCommand.CommandText = iQuery; nonQueryCommand.ExecuteNonQuery();
How To Use The MicrosoftExcelClient
As mentioned previously, the provided client is quite simple to use for budding developers, and should be a mere cake walk for experienced pros. Shown below is a brief snippet of how simple it is to use this assembly to extract and feed data from / into an Excel sheet�Collapse | Copy Code
//Declare the sample object & set source data path MicrosoftExcelClient sampleObject = new MicrosoftExcelClient("SampleData.xls"); //Open connection to the excel work book sampleObject.openConnection(); //Load the entire excel sheet into Datagrid1 //( Sheet name is passed as a parameter ) this.dataGrid1.DataSource = sampleObject.readEntireSheet("sheet1"); //Load the result of a specific query on the excel sheet into Datagrid2 //Query pased as a parameter this.dataGrid2.DataSource = sampleObject.readForSpecificQuery("select data1 , " + "data2 ,data3 from [sheet1$]"); //******NON RESULT ORIENTED QUERIES******* //Inserts a new record into the excel sheet sampleObject.runNonQuery("insert into [sheet1$]" + " values('1','2','3','4','5','6','7') "); //Update the given excel sheet sampleObject.runNonQuery("update [sheet1$] set data1 = '9'");
Conclusion
To sum it all up, theMicrosoftExcelClientis very simple to use, and is open source and not copyrighted. You may use it in parts or as a whole without any restrictions. The demo project is bug free, and any and all suggestions are welcome. You may face problems deleteing records from an Excel database when using the
NonQueryfunction, however this is a drawback of OLEDB, and is not a mistake in the code.
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.A list of licenses authors might use can be found here
相关文章推荐
- Link ASP.NET Data Grid to Microsoft Excel in the client side
- Deep Copy in C# (Cloning for a user defined class)
- Microsoft Audio Compression Manager (ACM) in C#.NET
- Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
- MSDN - New Recommendations for Using Strings in Microsoft .NET 2.0
- Client-Side Reporting with Visual Studio in C#
- Parallel for loops in .NET C# z
- Essential Silverlight: Getting started now with Microsofts new cross-platform plug-in for rich inte
- Debut of Hyper-V, departure of Gates usher[引导,展示,招待员] in new era for Microsoft
- Intellisense in Visual Studio for Microsoft Dynamics CRM 2016
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
- fuzzy search add-in for excel
- Excel Programming in VSTO (book:Visual Studio Tools for Office 2007 VSTO) Note
- Excel 2 Json For Python In Mac
- Hands-on Labs for Windows® Workflow Foundation in C# and VB.NET
- FTP Client library in C#
- 关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel) 收藏
- Using native GDI for text rendering in C#
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件