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,结果没坚持两篇就放弃了. 赶巧的是,昨天运营那边提出要录入一些基础数据 ...
随机推荐
- 记一次wordpress安装过程中遇到的问题及解决办法
Q:无法建立目录wp-content/uploads/2017/03.有没有上级目录的写权限?A:执行chmod 777 wp-content/ 提升目录权限 Q:安装主题或安装插件的时候,用到FTP ...
- sql 时间转换格式 convert(varchar(10),字段名,转换格式)
convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121) CONVERT为日期转换函数,一般就是在时间类型(datetime ...
- Houdini技术体系 过程化地形系统(一):Far Cry5的植被系统分析
背景 在大世界游戏里,植被(biome)是自然环境非常重要的组成部分,虽然UE4里的也有比较不错的地形+植被系统,但相比国外AAA级游戏的效果,还是有不少的差距,简介如下: UE4的植被分为( ...
- github 搜索
1.明确搜索仓库标题.仓库描述.README GitHub 提供了便捷的搜索方式,可以限定只搜索仓库的标题.或者描述.README等. 以Spring Cloud 为例,一般一个仓库,大概是这样的 其 ...
- badgeview
https://github.com/AlexLiuSheng/BadgeView include: compile 'com.allenliu.badgeview:library:1.1.1'(ne ...
- Jquery EasyUI Combotree只能选择叶子节点且叶子节点有多选框
Jquery EasyUI Combotree只能选择叶子节点且叶子节点有多选框 Jquery EasyUI Combotree单选框,Jquery EasyUI Combotree只能选择叶子节点 ...
- ambari安装调研
http://blog.csdn.net/daiyutage/article/details/52210830 ssh-keygen ssh-copy-id -i ~/.ssh/id_rsa.pub ...
- [Golang] 开源一个帧同步服务器
具体看README吧 https://github.com/bailu1901/lockstepserver
- IE8下动态生成option无法设置文本内容
问题: 1.在IE8下,JS动态添加 option 的时候,value 有值,但是文本内容添加不上 例:<option value="北京"></option&g ...
- F - Rescue 优先队列bfs
来源poj Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a ...