Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenXML to read Excel documents in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to use OpenXML to read Excel documents in C#, the content is very detailed, interested friends can refer to, hope to be helpful to you.

OpenXML is an official Microsoft open source operation Word, Excel and PPT SDK, developed with C #, using this kind of library can easily operate the three main types of office documents, and do not need to install Office programs.

Here is a list of how to use OpenXML to read the contents of an Excel document:

First of all, use nuget to install the class library reference of OpenXML, or you can download the class library directly from the Internet for reference

Add Namespace

Using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Spreadsheet; uses code to open an Excel document using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open ("file path", false)) {WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; / / Select the sheet page of Excel, corresponding to the actual order in Excel WorksheetPart worksheetPart = workbookPart.WorksheetParts.ElementAt (1); / / the content in the sheet page SheetData _ Sheet1data = worksheetPart.Worksheet.Elements (). First () / / some Chinese and English contents such as headings in Excel will be placed in SharedStringTable var _ sharepart = workbookPart.SharedStringTablePart;// circular Excel table row data foreach (Row rin _ Sheet1data.Elements ()) {/ / data foreach (Cell c in r.Elements ()) {string text = c.CellValue.Text in each Cell data / / determine whether to if (c.DataType! = null) {var _ xmlpart = _ sharepart.SharedStringTable.ElementAt (Convert.ToInt32 (c.CellValue.Text)) in SharedStringTable; text = _ xmlpart.FirstChild.InnerText;} / / finally get text data}} on how to use OpenXML to read Excel documents in C# is shared here. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report