可使用EPPlus类库,下载地址如下:

http://epplus.codeplex.com/

也可以在这里下载:

https://files.cnblogs.com/files/jietian331/EPPlus4.1.zip

转载请注明出处: http://www.cnblogs.com/jietian331/p/8033288.html

用法如:

using OfficeOpenXml;
using System.Collections.Generic;
using System.IO; public class CTLExcelDecoder : CTLConfigFile.IDecoder
{
public void DecodeFile(string path, out string[] fieldNames, out string[] types, out string[] annotations, out string[][] valueLines)
{
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (ExcelPackage excel = new ExcelPackage(fileStream))
{
ExcelWorksheet sheet = excel.Workbook.Worksheets[]; List<string> listAnnotations = new List<string>();
for (int c = ; c <= sheet.Dimension.End.Column; c++)
{
ExcelRange excelRange = sheet.Cells[, c];
string value = (excelRange.Value ?? "").ToString().Trim();
if (!string.IsNullOrEmpty(value))
{
ExcelComment comment = excelRange.Comment;
string commentString = comment != null ? string.Format("({0})", comment.Text.Replace("\n", " ").Replace("\r", " ")) : "";
string stringAnnotaion = string.Format("{0}{1}", value, commentString);
listAnnotations.Add(stringAnnotaion);
}
else
break;
}
int maxColum = listAnnotations.Count; annotations = listAnnotations.ToArray();
fieldNames = new string[maxColum];
types = new string[maxColum];
for (int c = ; c <= maxColum; c++)
{
int index = c - ;
fieldNames[index] = (sheet.Cells[, c].Value ?? "").ToString(); // 字段名
types[index] = (sheet.Cells[, c].Value ?? "").ToString(); // 类型
} List<string[]> listValue = new List<string[]>();
for (int r = ; r <= sheet.Dimension.End.Row; r++)
{
object idObj = sheet.Cells[r, ].Value;
if (idObj != null)
{
string[] valueArray = new string[maxColum];
valueArray[] = idObj.ToString();
for (int c = ; c <= maxColum; c++)
valueArray[c - ] = (sheet.Cells[r, c].Value ?? "").ToString(); listValue.Add(valueArray);
}
else
{
break;
}
}
valueLines = listValue.ToArray();
}
}
}
}

c#之如何操作excel的更多相关文章

  1. 免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)

    很多的软件项目几乎都包含着对文档的操作,前面已经介绍过两款操作文档的组件,现在介绍一款文档操作的组件NPOI. NPOI可以生成没有安装在您的服务器上的Microsoft Office套件的Excel ...

  2. C#通过NPOI操作Excel

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-post.html http://www.yuanjiaocheng.net/w ...

  3. POI操作Excel

    POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...

  4. NPOI操作EXCEL(六)——矩阵类表头EXCEL模板的解析

    哈哈~~~很高兴还活着.总算加班加点的把最后一类EXCEL模板的解析做完了... 前面几篇文章介绍了博主最近项目中对于复杂excel表头的解析,写得不好,感谢园友们的支持~~~ 今天再简单讲诉一下另一 ...

  5. VB操作EXCEL文件

    用VB操作Excel(VB6.0)(整理) 首先创建Excel对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel. ...

  6. VB.NET操作Excel

    VB.NET操作Excel的基本方法与例子:

  7. C# 操作excel单元格居中

    C# 操作excel //导出Excel        private void ExportExcel(string fileName, System.Data.DataTable myDGV, s ...

  8. NPOI操作Excel辅助类

    /// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...

  9. JAVA的POI操作Excel

    1.1Excel简介 一个excel文件就是一个工作簿workbook,一个工作簿中可以创建多张工作表sheet,而一个工作表中包含多个单元格Cell,这些单元格都是由列(Column)行(Row)组 ...

  10. NPOI操作EXCEL(一)——npoi基础

    去年项目有一个子模块需要解析上百张不一样的excel表格入库,当时用的NPOI,做了很久...也尝试想把代码分享到oschina,结果没坚持两篇就放弃了. 赶巧的是,昨天运营那边提出要录入一些基础数据 ...

随机推荐

  1. DTO转DOMAIN动态转换类。

    package dtotransfer.util; import dtotransfer.annotation.DomainField; import java.lang.annotation.Ann ...

  2. mybatis中_parameter使用和常用sql

    mybatis中_parameter使用和常用sql   mybatis中_parameter使用和常用sql 在用自动生成工具生成的mybatis代码中,总是能看到这样的情况,如下: <sel ...

  3. Android中的AlarmManager的使用

    AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent.而使用Intent的时候,我们还需要它执行一个动作,如startActivity, ...

  4. Centos7.0下MySQL的安装

    1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2.安装mysql-comm ...

  5. LVS & NGINX

      LVS NGINX

  6. 超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)

    https://blog.csdn.net/u014732824/article/details/84952848 int i = System.Text.Encoding.Default.GetBy ...

  7. Jquery实现轮播公告

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. LeetCode - 766. Toeplitz Matrix

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

  9. Chrome Google浏览器下载

    https://support.google.com/chrome/answer/95346?co=GENIE.Platform%3DDesktop&hl=zh-Hans    下载和安装 G ...

  10. 【转】最新版PyCharm(2018.2)破解

    源链接 1.下载jar包 我突然发现这个包是通用的!IDEA,PyCharm都可以用 链接:http://idea.lanyus.com/ 上篇IDEA破解依旧是这个jar包 2.jar包放置位置将j ...