使用了两个开源控件读取Excel文件的内容,不需要安装Excel或Office,开发环境可能需要vs2008(2005没测试过)

NPOI,       读取xls文件(Excel2003及之前的版本)   (NPOI.dll+Ionic.Zip.dll)     http://npoi.codeplex.com/

EPPlus,    读取xlsx文件(Excel2007版本)                (EPPlus.dll)                       http://epplus.codeplex.com/

本文中只实现了Excel文件的读取,实际上,这两个控件均支持对其内容,格式,公式等进行修改,这些复杂功能尚无需求,所以没有实现

读取接口IExcel:

public interface IExcel{/// <summary> 打开文件 </summary>bool Open();/// <summary> 文件版本 </summary>ExcelVersion Version { get; }/// <summary> 文件路径 </summary>string FilePath { get; set; }/// <summary> 文件是否已经打开 </summary>bool IfOpen { get;  }/// <summary> 文件包含工作表的数量 </summary>int SheetCount { get; }/// <summary> 当前工作表序号 </summary>int CurrentSheetIndex { get; set; }/// <summary> 获取当前工作表中行数 </summary>int GetRowCount();/// <summary> 获取当前工作表中列数 </summary>int GetColumnCount();/// <summary> 获取当前工作表中某一行中单元格的数量 </summary>/// <param name="Row">行序号</param>int GetCellCountInRow(int Row);/// <summary> 获取当前工作表中某一单元格的值(按字符串返回) </summary>/// <param name="Row">行序号</param>/// <param name="Col">列序号</param>string GetCellValue(int Row, int Col);/// <summary> 关闭文件 </summary>void Close();}public enum ExcelVersion{/// <summary> Excel2003之前版本 ,xls </summary>Excel03,/// <summary> Excel2007版本 ,xlsx  </summary>Excel07}

xls文件实现:

using NPOI.HSSF.UserModel;public class Excel03:IExcel{public Excel03(){ }public Excel03(string path){ filePath = path; }private FileStream file = null;private string filePath = "";private HSSFWorkbook book = null;private int sheetCount=0;private bool ifOpen = false;private int currentSheetIndex = 0;private HSSFSheet currentSheet = null;public string FilePath{get  { return filePath; }set { filePath = value; }}public bool Open(){try{file = new FileStream(filePath, FileMode.Open, FileAccess.Read);book= new HSSFWorkbook(file);if (book == null) return false;sheetCount = book.NumberOfSheets;currentSheetIndex = 0;currentSheet = (HSSFSheet)book.GetSheetAt(0);ifOpen = true;}catch (Exception ex){throw new Exception("打开文件失败,详细信息:" + ex.Message);}return true;}public void  Close(){if (!ifOpen) return;file.Close();}public ExcelVersion Version{ get { return ExcelVersion.Excel03; } }public bool IfOpen{ get { return ifOpen; } }public int SheetCount{ get { return sheetCount; } }public int CurrentSheetIndex{get { return currentSheetIndex; }set {if (value != currentSheetIndex){if (value >= sheetCount)throw new Exception("工作表序号超出范围");currentSheetIndex = value;currentSheet = (HSSFSheet)book.GetSheetAt(currentSheetIndex);}}}public int GetRowCount(){if (currentSheet == null) return 0;return currentSheet.LastRowNum + 1;         }public int GetColumnCount(){if (currentSheet == null) return 0;int colCount = 0;for (int i = 0; i <= currentSheet.LastRowNum; i++){if (currentSheet.GetRow(i) != null && currentSheet.GetRow(i).LastCellNum+1 > colCount)colCount = currentSheet.GetRow(i).LastCellNum + 1;}return colCount;}public int GetCellCountInRow(int Row){if (currentSheet == null) return 0;if (Row > currentSheet.LastRowNum) return 0;if (currentSheet.GetRow(Row) == null) return 0;return currentSheet.GetRow(Row).LastCellNum+1;}public string GetCellValue(int Row, int Col){if (Row > currentSheet.LastRowNum) return "";if (currentSheet.GetRow(Row) == null) return "";HSSFRow r = (HSSFRow)currentSheet.GetRow(Row);if (Col > r.LastCellNum) return "";if (r.GetCell(Col) == null) return "";return r.GetCell(Col).StringCellValue;            }}

xlsx文件实现:

using OfficeOpenXml;public class Excel07:IExcel{ public Excel07(){ }public Excel07(string path){ filePath = path; }private string filePath = "";private ExcelWorkbook book = null;private int sheetCount = 0;private bool ifOpen = false;private int currentSheetIndex = 0;private ExcelWorksheet currentSheet = null;private ExcelPackage ep = null;public bool Open(){try{ep = new ExcelPackage(new FileInfo(filePath));if (ep == null) return false;book =ep.Workbook;sheetCount = book.Worksheets.Count;currentSheetIndex = 0;currentSheet = book.Worksheets[1];ifOpen = true;}catch (Exception ex){throw new Exception("打开文件失败,详细信息:" + ex.Message);}return true;}public void Close(){if (!ifOpen || ep == null) return;ep.Dispose();}public ExcelVersion Version{ get { return ExcelVersion.Excel07; } }public string FilePath{get { return filePath; }set { filePath = value; }}public bool IfOpen{ get { return ifOpen; } }public int SheetCount{ get { return sheetCount; } }public int CurrentSheetIndex{get  { return currentSheetIndex; }set{if (value != currentSheetIndex){if (value >= sheetCount)throw new Exception("工作表序号超出范围");currentSheetIndex = value;currentSheet =book.Worksheets[currentSheetIndex+1];}}}public int GetRowCount(){if (currentSheet == null) return 0;return currentSheet.Dimension.End.Row;}public int GetColumnCount(){if (currentSheet == null) return 0;return currentSheet.Dimension.End.Column;}public int GetCellCountInRow(int Row){if (currentSheet == null) return 0;if (Row >= currentSheet.Dimension.End.Row) return 0;return currentSheet.Dimension.End.Column;}public string GetCellValue(int Row, int Col){if (currentSheet == null) return "";if (Row >= currentSheet.Dimension.End.Row || Col >= currentSheet.Dimension.End.Column) return "";object tmpO =currentSheet.GetValue(Row + 1, Col + 1);if (tmpO == null) return "";return tmpO.ToString();}        }

调用类:

 public class ExcelLib{/// <summary> 获取Excel对象 </summary>/// <param name="filePath">Excel文件路径</param>/// <returns></returns>public static IExcel GetExcel(string filePath){if (filePath.Trim() == "") throw new Exception("文件名不能为空");if(!filePath.Trim().EndsWith("xls") && !filePath.Trim().EndsWith("xlsx"))throw new Exception("不支持该文件类型");if (filePath.Trim().EndsWith("xls")){IExcel res = new Excel03(filePath.Trim());return res;}else if (filePath.Trim().EndsWith("xlsx")){IExcel res = new Excel07(filePath.Trim());return res;}else return null;}}

程序中调用:

ExcelLib.IExcel tmp = ExcelLib.ExcelLib.GetExcel(Application.StartupPath + "\\TestUnicodeChars.xls");
//ExcelLib.IExcel tmp = ExcelLib.ExcelLib.GetExcel(Application.StartupPath + "\\TestUnicodeChars.xlsx");if (tmp == null) MessageBox.Show("打开文件错误");
try
{ if (!tmp.Open()) MessageBox.Show("打开文件错误");tmp.CurrentSheetIndex = 1; int asdf = tmp.GetColumnCount(); string sdf = tmp.GetCellValue(0,1); tmp.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); return ; }

C#下使用第三方开源控件读取Excel文件的内容相关推荐

  1. java 从excel中读取数据_在Java中读取Excel文件的内容和导出数据到Excel文件中

    转自www.chianjavaworld.net 原作者:SonyMusic 读:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 在Java ...

  2. C# 第三方开源控件库,非常强大实用,好比devexpress

    ![在这 开源第三方.net 控件库,全部为源代码,工具栏可显示控件列表,可直接拖动使用,下载链接地址如下:https://download.csdn.net/download/wanghuannih ...

  3. java excel文件读取的内容_java读取Excel文件指定内容

    --边学习边记录~ 最近需要用到从外部文件导入测试数据,因而上网查了一些读取excel文件这方面的代码,然后修改后适用于现有场景中(得到excel中指定单元格的内容). 导入的jar:poi-3.16 ...

  4. active控件读取服务器文件,Delphi下利用ActiveX控件读取PDF文件

    首先,要找到这个控件,安装Acrobat Reader程序也就同时得到了这个控件. 这个控件位于它的 ActiveX 子目录下,名字可能因版本不同而稍有不同.如 3.0 版的名字叫 pdf42.ocx ...

  5. vb.net利用listbox控件与excel文件相结合统计数据

    Dim xlApp As Object = NothingDim xlBooks As Excel.Workbooks = NothingDim xlBook As Excel.Workbook = ...

  6. 使用GemBox.Spreadsheet控件操作Excel文件

    最近在给cc帮忙做一个客户账单处理的小工具,需要打开xls编辑处理后再另存自定义样式的新xls文件,无意间发现了GemBox的这个控件,简直不能好用更多啊,就一个dll导入后不管目标机器上是否安装Ex ...

  7. java操作excel文件之系列一:《读取excel文件的内容保存到数据库》

    excel文件:example.xls 文件内容大概是下面这样的: 现在要读取excel的内容写入数据库中 String filepath = "example.xls";Stri ...

  8. java中读取excel数据类型_在Java中读取Excel文件的内容

    利用JExcelApi来动态生成excel文档 首先,请到http://www.andykhan.com/jexcelapi/index.html下载java excel api,主页上同时有比较详细 ...

  9. 利用python读取Excel文件的内容,报错:Excel xlsx file; not supported

    现象: pycharm中存在如下代码 xlrd = xlrd.open_workbook(r'C:\\Users\\Administrator\\Desktop\\SN.xlsx') 目的是在桌面中打 ...

最新文章

  1. [转载] 大道至简:软件工程实践者的思想——第四章 流于形式的沟通
  2. python logging.getlogger_logging.getLogger与logger的父子关系
  3. SQL UNION 操作符
  4. python 类-Python 类class定义 方法与属性教程
  5. [YTU]_2383 ( 矩形类定义【C++】)
  6. WRF-Chem User Guide3.9.1.1 部分内容翻译
  7. mysql内存数据库性能_Mysql内存表配置及性能测试
  8. html表单php连接mysql数据库_使用HTML表单和PHP更新MySQL
  9. TortoiseGit 添加ssh key
  10. 网络编程在线英英词典之客户端代码框架搭建(一)
  11. jsp怎么做柱状图_jsp圆饼图与柱状图代码
  12. Modern CMake 简介
  13. 复旦大学智能感知与无人系统实验室诚聘海内外超级博士后/博士后
  14. 利用压缩文件修改加密word文档
  15. IT公司软件工程师薪水排名
  16. 跑鸭”微信小程序-一款基于校园跑步的社交小程序
  17. 时延、丢包、抖动是什么?
  18. U盘变为只有2M大小空间的解决方法
  19. Sparrow项目疑问解答
  20. 3D游戏中“刀光剑影”特效的实现算法

热门文章

  1. 建立dblink(database link)
  2. FPGA经验谈系列文章——FPGA资源评估
  3. Android手势检测简介
  4. 用计算机管理员同步一下文件,《计算机应用基础(Windows 7 Office 2010)同步训练》0711.docx...
  5. PB 中获取时间的方法
  6. 什么才是市场急需的前端工程师?【零基础web前端入门视频教程】
  7. 1. PYNQ在ZCU102上的移植【PYNQ】
  8. 前端,值得收藏的那些网站
  9. 八皇后问题。。。。。。
  10. 拿了北京户口!却是跌落的开始....