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,结果没坚持两篇就放弃了. 赶巧的是,昨天运营那边提出要录入一些基础数据 ...
随机推荐
- 阿里巴巴Java开发手册及Java代码规约扫描eclipse和IDEA插件
<阿里巴巴Java开发手册>: https://github.com/alibaba/p3c/blob/master/%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B ...
- VS的ASP.NET项目中cshtml关键词出错 红线,当前上下文中不存在名称
[参考]VS的ASP.NET项目中cshtml突然出错,当前上下文中不存在名称“ViewBag” 原因:web.config 配置错误 这种情况是因为两个web.config文件版本不匹配,需要进行修 ...
- Python学习笔记——发邮件
参考:Python3实现163邮箱SMTP发送邮件 1.首先需要注册一个网易的邮箱,开启smtp服务,并使用其授权码 2.发送邮件的Python脚本 #!/usr/bin/python # -*- c ...
- 在windows下编写shell脚本
注意两点: 1.第一行:#!/bin/bash 2.将文档格式转换为unix,因为在windows下编写shell脚本回车符是\n\r,而linux下的回车符是\n,所以在linux下运行脚本的时候, ...
- puppet(4)-类、模版语言、模块
puppet(4)-类.模版语言.模块 代码重用: class, module 类 类的简单说明 类是用于公共目的的一组资源,是命名的代码块,创建后可在puppet全局进行调用,类可以继承类是我们构建 ...
- Java中excel转换为jpg/png图片 采用aspose-cells-18.6.jar
一 Java中excel转换为jpg/png图片 package com.thinkgem.jeesite.modules.task.util; import com.aspose.cells.Im ...
- 如何写Emit代码
写Emit代码也不是想象中的那么复杂,基本过程就是先手工写好C#代码,编译得到Exe或者Dll,然后用ILDASM或反编译工具,得到IL代码,最后就是看着IL代码,用Emit一个个对应发出代码,就行了 ...
- XML文档中的xmlns、xmlns:xsi和xsi:schemaLocation
文章转载自:https://yq.aliyun.com/articles/40353 相信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头 ...
- threding模块的其他用法
import threading import time def func(n): time.sleep(0.5) n += 1 print(n, threading.current_thread() ...
- Educational Codeforces Round 50
1036A - Function Height 20180907 \(ans=\left \lceil \frac{k}{n} \right \rceil\) #include<bits/ ...