C# How To Read .xlsx Excel File With 3 Lines Of Code
Introduction
We produce professional business software, and we often have to import data from very simple Excel *.xslx-files: with some relevant rows and cells in the first worksheet of a workbook, and that's it. But we do not want to use large DLL's or third party software. Therefore we produced a small solution for our needs. It could be useful for you, too:
Using the code
Download the "Excel.dll" (8 kByte, .net 4.5!) and add it to your project. Or adapt the source code. Then work with the rows and cells (of the first worksheet) in the Excel file like so:
worksheet ws = worksheet.GetData(@"C:\ExcelFile.xlsx");
foreach (var row in ws.Rows)
foreach (var cell in row.Cells)
if (cell != null)
Console.WriteLine(cell.Text); // Do something with the cells
Here you open the Excel file, and iterate through the rows (and the cells of each row) within three lines of code.
Points of Interest
This article (written by M I developer) describes all the theoretical background, if you are interested in it. We only reduced our solution to the max using the integrated ZIP-library in .net 4.5 and the standard XML-serializer of .net.
If you want to adapt the solution to your needs: edit the simple source code for the Excel.dll. This is how it works:
Maybe you did not know that xlsx-files are ZIP-files. And the text strings of the Excel cells of all worksheets per workbook are always stored in a file named "xl/sharedStrings.xml", while the first worksheet is called "xl/worksheets/sheet1.xml".
So we have to unzip the Excel file and we have to deserialize the two mentioned XML files in it:
using System.IO.Compression; public static worksheet GetData(string ExcelFileName)
{
worksheet ws; using (ZipArchive zipArchive = ZipFile.Open(ExcelFileName, ZipArchiveMode.Read))
{
worksheet.SharedStrings = worksheet.DeserializedZipEntry<sst>(zipArchive, @"xl/sharedStrings.xml");
ws = worksheet.DeserializedZipEntry<worksheet>(zipArchive, @"xl/worksheets/sheet1.xml");
}
For deserialization of an XML formatted ZIP-entry (see also this article written by Md. Rashim uddin) we use this generic method:
private static T DeserializedZipEntry<t>(ZipArchive ZipArchive, string ZipEntryName)
{
using (Stream stream
= ZipArchive.Entries.First<ziparchiveentry>(n => n.FullName.Equals(ZipEntryName)).Open())
return (T)new XmlSerializer(typeof(T), worksheet.RootAttr).Deserialize(XmlReader.Create(stream));
}
Therefore the XML-structures have to be reflected in our classes. Here you see the "sst"-class and the "SharedString"-class for the XML in the "shared strings table":
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="72" uniqueCount="6">
<si>
<t>Text A</t>
</si>
<si>
<t>Text B</t>
</si>
</sst>
public class sst
{
[XmlElement("si")]
public SharedString[] si; public sst()
{
}
} public class SharedString
{
public string t;
}
The same strategy we also use for the "worksheet" -XML-file in the ZIP-file. There we focus on the XML-elements and -attributes "row", "c", "v", "r" and "t". All the work is done again by the XmlSerializer:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<dimension ref="A1:F12"/>
<sheetViews>
<sheetView workbookViewId="0"></sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData>
<row r="1">
<c r="A1" t="s">
<v>0</v>
</c>
<c r="B1" t="s">
<v>1</v>
</c>
<c r="C1" t="s">
<v>2</v>
</c>
</row>
</sheetData>
</worksheet>
public class worksheet
{
[XmlArray("sheetData")]
[XmlArrayItem("row")]
public Row[] Rows; public class worksheet
{
}
}
public class Row
{
[XmlElement("c")]
public Cell[] FilledCells;
}
public class Cell
{
[XmlAttribute("r")]
public string CellReference;
[XmlAttribute("t")]
public string tType = "";
[XmlElement("v")]
public string Value;
}
Of course we have to do a little bit in order to convert the usual Excel cell references like "A1", "B1" and so on to column indices. That is done via a setter of "CellReference" in the "Cell"-class and a small method named "GetColumnIndex()":
[XmlAttribute("r")]
public string CellReference
{
get
{
return ColumnIndex.ToString();
}
set
{
ColumnIndex = worksheet.GetColumnIndex(value);
if (ColumnIndex > worksheet.MaxColumnIndex)
worksheet.MaxColumnIndex = ColumnIndex;
}
}
(Here we also derive the maximum column index for the whole worksheet.)
public static int GetColumnIndex(string CellReference)
{
string colLetter = new Regex("[A-Za-z]+").Match(CellReference).Value.ToUpper();
int colIndex = 0; for (int i = 0; i < colLetter.Length; i++)
{
colIndex *= 26;
colIndex += (colLetter[i] - 'A' + 1);
}
return colIndex - 1;
}
The last challenge has to do with the fact, that the Excel file does not contain empty Excel cells. So the tiny methods "ExpandRows()" and "ExpandCells()" handle that problem:
private void ExpandRows()
{
foreach (var row in Rows)
row.ExpandCells(NumberOfColumns);
} public void ExpandCells(int NumberOfColumns)
{
Cells = new Cell[NumberOfColumns];
foreach (var cell in FilledCells)
Cells[cell.ColumnIndex] = cell;
FilledCells = null;
}
In the end we have an array of all rows and an array of all cells for each row representing all columns of the specific Excel worksheet. Empty cells are null in the array, but the ColumnIndex of each cell in "Row.Cells[]" corresponds with the actual Excel column of each cell.
Besides, when you know that an Excel cell contains a date as its value, you can use this method for conversion:
public static DateTime DateFromExcelFormat(string ExcelDateValue)
{
return DateTime.FromOADate(Convert.ToDouble(ExcelDateValue));
}
Let me know how the total Excel.DLL works in your environment - and have fun with it!
C# How To Read .xlsx Excel File With 3 Lines Of Code的更多相关文章
- Read Excel file from C#
Common way is: var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirec ...
- csharp:using OpenXml SDK 2.0 and ClosedXML read excel file
https://openxmlexporttoexcel.codeplex.com/ http://referencesource.microsoft.com/ 引用: using System; u ...
- Read / Write Excel file in Java using Apache POI
Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year o ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...
- ExcelDataReader read excel file
上篇文章向大家介绍了用DocumentFormat.OpenXml.dll读取excel的方法,这里再向大家介绍一种轻量级简便的方法,用的是Excel.dll,及ICSharpCode.SharpZi ...
- axios upload excel file
axios upload excel file https://github.com/axios/axios/issues/1660 https://stackoverflow.com/questio ...
- NetSuite SuiteScript 2.0 export data to Excel file(xls)
In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collec ...
- Creating Excel File in Oracle Forms
Below is the example to create an excel file in Oracle Forms.Pass the Sql query string to the below ...
- Formatting Excel File Using Ole2 In Oracle Forms
Below is the some useful commands of Ole2 to format excel file in Oracle Forms.-- Change font size a ...
随机推荐
- mysql索引之八:myisam压缩(前缀压缩)索引
myisam使用前缀压缩来减少索引的大小,从而让更多的索引可以放入内存中,默认只压缩字符串,但通过参数配置也可以对整数做压缩,myisam压缩每个索引块的方法是,先完全保存索引块中的第一个值,然后将其 ...
- php对数组中的值进行排序
案例 <?php $a = array('1124','1125','1126'); $s1 = 1124; $s2 = 1125; $ks1 = array_search($s1,$a); $ ...
- U-boot分析与移植(2)----U-boot stage1分析
我们要生成u-boot.bin文件,它首先依赖于很多.o文件和.lds链接脚本文件 我们只要找到对应的.lds链接脚本文件就可以分析u-boot的启动流程. 1.打开u-boot-1.1.6\u-bo ...
- Windows下搭建PHP开发环境-WEB服务器
PHP集成开发环境有很多,如XAMPP.AppServ......只要一键安装就把PHP环境给搭建好了.但这种安装方式不够灵活,软件的自由组合不方便,同时也不利于学习.所以我还是喜欢手工搭建PHP开发 ...
- flask系列二之基础知识
一.调试模式(debug模式) 1.设置debug模式 在app.run()中传入关键字参数debug,app.run(debug=Ture),就设置当前项目为debug模式.如下所示: # 从fla ...
- Hibernate4.3.5入门HelloWorld
本文给出一个简单的Hibernate4.3.5入门实例,配置方式采用XML文件方式(这种方式已经不是主流了,目前越来越多采用Annotation方式映射POJO实体) 代码结构如下图所示:主要用到hi ...
- leetcode762
class Solution { public: bool IsPrime(int n) { ) { return false; } || n == ) { return true; } ; i &l ...
- 语法错误 : 缺少“;”(在“*”的前面) 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
sv2010编译的时候遇到错误: error C2143: 语法错误 : 缺少“;”(在“*”的前面) error C4430: 缺少类型说明符 - 假定为 int.注意: C++ 不支持默认 int ...
- Ceph基本情况介绍
Ceph基本情况的介绍 Ceph是一个分布式存储软件. 它支持用3种方式存储数据,分别是:对象存储.块设备存储.分布式文件系统存储. Ceph最初的目标是做一个分布式文件系统,直到现在这个目标也不能算 ...
- WordPress,discuz 根据不同的入口url更换logo
Discuz!中调用cookie的思路出来了: 设置cookie:dsetcookie('cookie名', 'cookie值', '有效时间'); 读取cookie有两种方法,第一种使用get ...