c#之如何操作excel
可使用EPPlus类库,下载地址如下:
也可以在这里下载:
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的更多相关文章
- 免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)
很多的软件项目几乎都包含着对文档的操作,前面已经介绍过两款操作文档的组件,现在介绍一款文档操作的组件NPOI. NPOI可以生成没有安装在您的服务器上的Microsoft Office套件的Excel ...
- C#通过NPOI操作Excel
参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-post.html http://www.yuanjiaocheng.net/w ...
- POI操作Excel
POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...
- NPOI操作EXCEL(六)——矩阵类表头EXCEL模板的解析
哈哈~~~很高兴还活着.总算加班加点的把最后一类EXCEL模板的解析做完了... 前面几篇文章介绍了博主最近项目中对于复杂excel表头的解析,写得不好,感谢园友们的支持~~~ 今天再简单讲诉一下另一 ...
- VB操作EXCEL文件
用VB操作Excel(VB6.0)(整理) 首先创建Excel对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel. ...
- VB.NET操作Excel
VB.NET操作Excel的基本方法与例子:
- C# 操作excel单元格居中
C# 操作excel //导出Excel private void ExportExcel(string fileName, System.Data.DataTable myDGV, s ...
- NPOI操作Excel辅助类
/// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...
- JAVA的POI操作Excel
1.1Excel简介 一个excel文件就是一个工作簿workbook,一个工作簿中可以创建多张工作表sheet,而一个工作表中包含多个单元格Cell,这些单元格都是由列(Column)行(Row)组 ...
- NPOI操作EXCEL(一)——npoi基础
去年项目有一个子模块需要解析上百张不一样的excel表格入库,当时用的NPOI,做了很久...也尝试想把代码分享到oschina,结果没坚持两篇就放弃了. 赶巧的是,昨天运营那边提出要录入一些基础数据 ...
随机推荐
- halcon 特征测量
Features 1. line_orientation 功能:计算线的方位. 2. line_position 功能:计算一条线的重心.长度和方位. 3. partition_lines ...
- 【ORACLE】解锁scott帐号
sqlplus / as sysdba;SQL> alter user scott account unlock;SQL> conn scott/grace
- 10.Oracle Golden Date(ogg)的搭建和管理
一. GoldenGate 概述 GoldenGate现在是业内成熟的数据容灾与复制产品:GoldenGate是一种基于日志的结构化数据复制方式,它通过解析源数据库在线日志或归档日志获得数据的增删改变 ...
- Eclipse 左侧树形展示字体调节
eclipse中项目导航字体大小由配置文件中的设置决定 1.配置文件:找到eclipse安装位置(或解压路径): eclipse\plugins\org.eclipse.ui.themes_1.2.0 ...
- Spark学习之常用算子介绍
1. reduceByKey reduceByKey的作用对像是(key, value)形式的rdd,而reduce有减少.压缩之意,reduceByKey的作用就是对相同key的数据进行处理,最终每 ...
- react使用mobx
mobx api 使用装饰器语法 mobx数据转化为js数据 安装 yarn add mobx mobx-react yarn add babel-preset-mobx --dev "pr ...
- Ubuntu下安装LNMP
1.安装mysql sudo apt-get install mysql-server mysql-client 2.安装nginx sudo apt-get install nginx 安装完后重启 ...
- Integer 的 valueOf 方法 与 常量池(对 String Pool 的部分理解)
举例: public class Test { @org.junit.Test public void intTest() { Integer t1 = 128; Integer t2 = 127; ...
- java.sql.SQLException: Field 'id' doesn't have a default value(用eclipse操作数据库时报了这种奇怪的错误)的原因与解决方法
1.错误原因 由于id在数据库表中是作为主键,但是在插入的过程中,没有给予数值,并且没有让其自增 2.解决办法 修改数据库表中的id,让其自增(在插入的过程中,不插入id数据时)
- mui 卡片视图 遮罩蒙版
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...