有的客户装了不同的excel程序,所以做了四种excel文件的读写,在一定程度上可以适应各种情况的客户。

npoi :对公式,某些日期兼容的不好,速度很快,不需要装excel程序

ado::对某些日期格式兼容的不好,速度较快

office:支持office2013

wps:支持wps2016

测试:在wps与excel都安装的情况下,调用 wps sdk时,有些打开确是excel进程

源代码下载地址:http://download.csdn.net/detail/liangzhonglin/9546566

wps

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Excel;namespace ExcelLib
{public class CExcel{public string Error = "";[DllImport("User32.dll", CharSet = CharSet.Auto)]public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);/// <summary>/// 强制关闭当前Excel进程/// </summary>public static void Kill(IntPtr intPtr){try{Process[] ps = Process.GetProcesses();int ExcelID = 0;GetWindowThreadProcessId(intPtr, out ExcelID); //得到本进程唯一标志k   foreach (Process p in ps){string procName = p.ProcessName.ToLower();if (procName.Equals("excel")|| procName.Equals("et")){if (p.Id == ExcelID){p.Kill();}}}}catch{//不做任何处理   }}public bool DatatableToExcel(ref System.Data.DataTable dtExcel, string excelPath){Error = "不支持此接口!";return false;}#region 读取excel文件public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, string fieldNameList, out System.Data.DataTable dtExcel){Application objApp = null;Workbook objWorkBook = null;Worksheet objWorkSheet = null;IntPtr wpsPtr = IntPtr.Zero;dtExcel = null;try{//获取工作表表格object type = System.Reflection.Missing.Value;objApp = new Application();objApp.Visible = false;objApp.ScreenUpdating = false;objApp.DisplayAlerts = false;objWorkBook = (Workbook)objApp.Workbooks.Open(excelPath, type, type, type, type, type, type, type, type, type, type, type, type, type, type);if (objApp.Workbooks.Count == 0){throw new Exception("excel文件中没有sheet表");}wpsPtr = new IntPtr(objApp.Hwnd);//获得指定表if (sheetName.Length == 0){objWorkSheet = (Worksheet)objWorkBook.Worksheets.get_Item(1);}else{objWorkSheet = (Worksheet)objWorkBook.Worksheets.get_Item(sheetName);}//取表名赋值到dt TableNamedtExcel = new System.Data.DataTable(tableName);//获取数据区域int row = objWorkSheet.UsedRange.Rows.Count;int col = objWorkSheet.UsedRange.Columns.Count;int recordIndex = 1;if (fieldNameList.Length > 0){string[] fieldList = fieldNameList.Split(',');for (int i = 0; i < fieldList.Length; i++){dtExcel.Columns.Add(fieldList[i]);}}else{for (int c = 1; c <= col; c++){dtExcel.Columns.Add(new DataColumn((String)((Excel.Range)objWorkSheet.Cells[1, c]).Text));}recordIndex = 2;}for (int r = recordIndex; r <= row; r++){DataRow newRow = dtExcel.NewRow();for (int c = 1; c <= col; c++){newRow[c - 1] = ((Excel.Range)objWorkSheet.Cells[r, c]).Text;}dtExcel.Rows.Add(newRow);}return true;}catch (Exception ex){Error = "读取excel失败:" + ex.Message;return false;}finally{if (objWorkBook != null){objWorkBook.Close(true, null, null);objWorkBook = null;}if (objApp != null){objApp.Quit();objApp = null;}// 杀死WPS线程try{if (wpsPtr != IntPtr.Zero){Kill(wpsPtr);}}catch (Exception){ }System.GC.Collect();GC.WaitForPendingFinalizers();}}public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, int getMaxRecord, out System.Data.DataTable dtExcel){dtExcel = null;Error = "不支持此接口!";return false;}#endregion}
}

office

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using System.Diagnostics;namespace ExcelLib
{public class CExcel{public string Error = "";[DllImport("User32.dll", CharSet = CharSet.Auto)]public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);/// <summary>/// 强制关闭当前Excel进程/// </summary>public static void Kill(IntPtr intPtr){try{Process[] ps = Process.GetProcesses();int ExcelID = 0;GetWindowThreadProcessId(intPtr, out ExcelID); //得到本进程唯一标志k   foreach (Process p in ps){string procName = p.ProcessName.ToLower();if (procName.Equals("excel") || procName.Equals("et")){if (p.Id == ExcelID){p.Kill();}}}}catch{//不做任何处理   }}public bool DatatableToExcel(ref DataTable dtExcel, string excelPath){Error = "不支持此接口!";return false;}#region 读取excel文件public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, string fieldNameList, out DataTable dtExcel){Microsoft.Office.Interop.Excel.ApplicationClass excel = null;dtExcel = null;IntPtr excelPtr = IntPtr.Zero;try{excel = new Microsoft.Office.Interop.Excel.ApplicationClass();excelPtr = new IntPtr(excel.Hwnd);excel.Visible = false;excel.ScreenUpdating = false;excel.DisplayAlerts = false;excel.Workbooks.Add(excelPath);if (excel.Workbooks.Count == 0){throw new Exception("excel文件中没有sheet表");}//获得指定表Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Worksheets[1];//取表名赋值到dt TableNamedtExcel = new System.Data.DataTable(worksheet.Name);worksheet.Columns.EntireColumn.AutoFit();int row = worksheet.UsedRange.Rows.Count;int col = worksheet.UsedRange.Columns.Count;for (int c = 1; c <= col; c++){dtExcel.Columns.Add(new DataColumn((String)((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, c]).Text));}for (int r = 2; r <= row; r++){DataRow newRow = dtExcel.NewRow();for (int c = 1; c <= col; c++){newRow[c - 1] = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r, c]).Text;}dtExcel.Rows.Add(newRow);}worksheet = null;return true;}catch (Exception ex){Error = "读取excel失败:" + ex.Message;return false;}finally{if (excel != null){excel.Quit();excel = null;}// 杀死excel线程try{if ( excelPtr!= IntPtr.Zero){Kill(excelPtr);}}catch (Exception){ }System.GC.Collect();}}public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, int getMaxRecord, out DataTable dtExcel){dtExcel = null;Error = "不支持此接口!";return false;}#endregion}
}

ado

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.OleDb;namespace ExcelLib
{public class CExcel{public string Error = "";public bool DatatableToExcel(ref DataTable dtExcel, string excelPath){Error = "不支持此接口!";return false;}#region 读取excel文件public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, string fieldNameList, out DataTable dtExcel){dtExcel = null;OleDbConnection conn = null;try{string defaultSheetName = sheetName;   // "[Sheet1$]";string strConn = string.Format("Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes'", excelPath);// string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'", excelPath);if (sheetName.Length == 0){// 取默认的sheet表名conn = new OleDbConnection(strConn);conn.Open();System.Data.DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });conn.Close();conn = null;defaultSheetName = sheetNames.Rows[0]["TABLE_NAME"].ToString();}DataSet ds = new DataSet();string sqlTabel = string.Format("select * from [{0}]", defaultSheetName);OleDbDataAdapter oada = new OleDbDataAdapter(sqlTabel, strConn);oada.Fill(ds);dtExcel = ds.Tables[0];dtExcel.TableName = tableName;if (fieldNameList.Length > 0){string[] fieldList = fieldNameList.Split(',');for (int i = 0; i < dtExcel.Columns.Count; i++){dtExcel.Columns[i].ColumnName = fieldList[i];}}return true;}catch (Exception ex){Error = "读取excel失败:" + ex.Message;return false;}finally{if (conn != null){conn.Close();conn = null;}}}public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, int getMaxRecord, out DataTable dtExcel){dtExcel = null;Error = "不支持此接口!";return false;}#endregion}
}

npoi

using System;
using System.Collections.Generic;
using System.Text;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using System.IO;
using System.Data;
using NPOI;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;namespace ExcelLib
{public class CExcel{public string Error = "";#region 生成excelprivate CellType GetCellType(string type){CellType cell = CellType.String;if (type == "System.Double"){cell = CellType.Numeric;}return cell;}public bool DatatableToExcel(ref DataTable dtExcel, string excelPath){try{if (excelPath == null){throw new Exception("未指定生成路径");}bool bResult = true;string ext = Path.GetExtension(excelPath);switch (ext.ToLower()){case ".xls":bResult= DatatableToExcel2003(ref dtExcel, excelPath);break;case ".xlsx":bResult= DatatableToExcel2007(ref dtExcel, excelPath);break;default:throw new Exception("保存文件名不正确");}return bResult;}catch (System.Exception ex){Error = "生成excel失败" + ex.Message;return false;}}private bool DatatableToExcel2007(ref DataTable dtExcel, string excelPath){try{XSSFWorkbook workbook = new XSSFWorkbook();create a entry of DocumentSummaryInformation//DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();//dsi.Company = "Partner";//hssfworkbook..DocumentSummaryInformation = dsi;create a entry of SummaryInformation//SummaryInformation si = PropertySetFactory.CreateSummaryInformation();//si.Subject = "FrontLink";//hssfworkbook.SummaryInformation = si;//here, we must insert at least one sheet to the workbook. otherwise, Excel will say 'data lost in file'//So we insert three sheet just like what Excel doesISheet sheet = workbook.CreateSheet(dtExcel.TableName);int cellCount = dtExcel.Columns.Count;IRow column = sheet.CreateRow(0);for (int j = 0; j < cellCount; j++){column.CreateCell(j).SetCellType(GetCellType(dtExcel.Columns[j].DataType.ToString()));column.CreateCell(j).SetCellValue(dtExcel.Columns[j].ColumnName);}for (int i = 0; i < dtExcel.Rows.Count; i++){IRow row = sheet.CreateRow(i + 1);for (int j = 0; j < cellCount; j++){row.CreateCell(j).SetCellType(GetCellType(dtExcel.Columns[j].DataType.ToString()));row.CreateCell(j).SetCellValue(dtExcel.Rows[i][j].ToString());}}for (int i = 0; i < dtExcel.Columns.Count; i++){// sheet.AutoSizeColumn(i, false);// 此方法导致内存占用100%,导出速度慢sheet.SetColumnWidth(i, 9000);}//Write the stream data of workbook to the root directoryFileStream file = new FileStream(excelPath, FileMode.Create);workbook.Write(file);file.Close();return true;}catch (System.Exception ex){Error = "生成Excel失败:" + ex.Message;return false;}}private bool DatatableToExcel2003(ref DataTable dtExcel, string excelPath){try{HSSFWorkbook workbook = new HSSFWorkbook();//create a entry of DocumentSummaryInformationDocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();dsi.Company = "Partner";workbook.DocumentSummaryInformation = dsi;//create a entry of SummaryInformationSummaryInformation si = PropertySetFactory.CreateSummaryInformation();si.Subject = "FrontLink";workbook.SummaryInformation = si;//here, we must insert at least one sheet to the workbook. otherwise, Excel will say 'data lost in file'//So we insert three sheet just like what Excel doesISheet sheet = workbook.CreateSheet(dtExcel.TableName);int cellCount = dtExcel.Columns.Count;IRow column = sheet.CreateRow(0);for (int j = 0; j < cellCount; j++){column.CreateCell(j).SetCellType(GetCellType(dtExcel.Columns[j].DataType.ToString()));column.CreateCell(j).SetCellValue(dtExcel.Columns[j].ColumnName);}for (int i = 0; i < dtExcel.Rows.Count; i++){IRow row = sheet.CreateRow(i + 1);for (int j = 0; j < cellCount; j++){row.CreateCell(j).SetCellType(GetCellType(dtExcel.Columns[j].DataType.ToString()));row.CreateCell(j).SetCellValue(dtExcel.Rows[i][j].ToString());}}for (int i = 0; i < dtExcel.Columns.Count; i++){//sheet.AutoSizeColumn(i, false);   // 此方法导致内存占用100%,导出速度慢sheet.SetColumnWidth(i, 9000);}//Write the stream data of workbook to the root directoryFileStream file = new FileStream(excelPath, FileMode.Create);workbook.Write(file);file.Close();return true;}catch (System.Exception ex){Error = "生成Excel失败:" + ex.Message;return false;}}#endregion#region 读取excel文件/// <summary>/// 读取单元格的数据,并转化成字符串值/// </summary>/// <param name="cell"></param>/// <returns></returns>private string GetCellValue(ICell cell){string value = "";if (cell != null){string format = cell.CellStyle.GetDataFormatString();switch (cell.CellType){case CellType.String:value = cell.StringCellValue;break;case CellType.Numeric:if (format != null && (format.IndexOf("m") > 0 || format.IndexOf("d") > 0)){if (format == "m/d/yy"){DateTime dt=cell.DateCellValue;value = dt.ToString("yyyy-MM-dd");}else{//DateTime dt = DateTime.FromOADate(cell.NumericCellValue);value = cell.ToString();}}else{value = cell.NumericCellValue.ToString();}break;case CellType.Boolean:value = cell.BooleanCellValue.ToString();break;case CellType.Formula:value = cell.CellFormula;break;case CellType.Blank:value = "";break;default:break;}}return value;}/// <summary>/// 读取Excel文件内容到表里/// </summary>/// <param name="excelPath"></param>/// <param name="sheetName"></param>/// <param name="tableName"></param>/// <param name="fieldNameList">fieldNameList有值,为指定的字段,为“”;取excel的第一行做为列值;为null,取excel的列名</param>/// <param name="getMaxRecord">取值范围:-1取全部的值;>0取指定的值</param>/// <param name="dtExcel"></param>/// <returns></returns>private bool LocalReadExcelToDataTable(string excelPath, string sheetName, string tableName, string fieldNameList, int getMaxRecord, out DataTable dtExcel){dtExcel = new DataTable(tableName);FileStream file = null;try{sheetName = sheetName.Trim();file = new FileStream(excelPath, FileMode.Open);IWorkbook workbook = WorkbookFactory.Create(file);//HSSFWorkbook workbook = new HSSFWorkbook(file);//创建工作簿对象NPOI.SS.UserModel.ISheet sheet = null;if (sheetName.Length == 0){sheet = workbook.GetSheetAt(0);}else{sheet = workbook.GetSheet(sheetName);}int sheetHaveHeader = 0;int cellCount = 0;//创建表标题if (fieldNameList == null){NPOI.SS.UserModel.IRow headerRow = sheet.GetRow(0);cellCount = headerRow.LastCellNum;//读取并生成标题行,这里能成功执行for (int i = headerRow.FirstCellNum; i < cellCount; i++){DataColumn column = new DataColumn(i.ToString());dtExcel.Columns.Add(column);}}else if (fieldNameList.Trim().Length > 0){string[] fieldList = fieldNameList.Split(',');foreach (string field in fieldList){dtExcel.Columns.Add(new DataColumn(field.Trim()));}cellCount = fieldList.Length;sheetHaveHeader = 1;}else{sheetHaveHeader = 1;NPOI.SS.UserModel.IRow headerRow = sheet.GetRow(0);cellCount = headerRow.LastCellNum;//读取并生成标题行,这里能成功执行for (int i = headerRow.FirstCellNum; i < cellCount; i++){DataColumn column = new DataColumn(GetCellValue(headerRow.GetCell(i, MissingCellPolicy.RETURN_BLANK_AS_NULL)));dtExcel.Columns.Add(column);}}bool bFilter = getMaxRecord >= 0;//逐个读取单元格,这里就不能正确读取到                                       for (int i = (sheet.FirstRowNum + sheetHaveHeader); i <= sheet.LastRowNum; i++){if (bFilter){// 只取指定的记录数if (dtExcel.Rows.Count >= getMaxRecord){break;}}NPOI.SS.UserModel.IRow row = sheet.GetRow(i);DataRow dataRow = dtExcel.NewRow();for (int j = row.FirstCellNum; j < cellCount; j++){dataRow[j] = GetCellValue(row.GetCell(j, MissingCellPolicy.RETURN_BLANK_AS_NULL)).Replace("'", "’");}dtExcel.Rows.Add(dataRow);}//for (int i = 0; i < dtExcel.Columns.Count; i++)//{//    // sheet.AutoSizeColumn(i, false); // 此方法导致内存占用100%,导出速度慢//    sheet.SetColumnWidth(i, 9000);//}workbook = null;sheet = null;return true;}catch (System.Exception ex){Error = "读取Excel失败:" + ex.Message;return false;}finally{if (file != null){file.Close();}}}public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, string fieldNameList, out DataTable dtExcel){return LocalReadExcelToDataTable(excelPath, sheetName, tableName, fieldNameList, -1, out dtExcel);}public bool ReadExcelToDataTable(string excelPath, string sheetName, string tableName, int getMaxRecord, out DataTable dtExcel){return LocalReadExcelToDataTable(excelPath, sheetName, tableName, null, getMaxRecord, out dtExcel);}#endregion}
}


NPOI ADO office wps 四种方式 读excel表格相关推荐

  1. 隐藏列表的另一种方式,Excel表格怎么创建组?

    都2022年了,还在对表格进行隐藏吗?Excel表格中有个创建组的功能可以快速隐藏表格的同时也可以快速展开表格,对表格进行修改,这是隐藏列表的另一种方式,那么Excel表格怎么创建组呢? 方式一:隐藏 ...

  2. golang 读写文件的四种方式

    读文件 读取的文件放在file/test:也就是file包下的test这个文件,里面写多一点文件 读文件方式一:利用ioutil.ReadFile直接从文件读取到[]byte中 func Read0( ...

  3. Java实现文件复制的四种方式

    背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...

  4. Linux C++多线程同步的四种方式

    目录 一.互斥锁 二.条件变量 三.读写锁 原文链接:Linux C++多线程同步的四种方式(非常详细)_Y先森0.0-CSDN博客 背景问题:在特定的应用场景下,多线程不进行同步会造成什么问题? 通 ...

  5. photon四种同步方式_【Linux】多线程同步的四种方式

    背景问题:在特定的应用场景下,多线程不进行同步会造成什么问题? 通过多线程模拟多窗口售票为例: #include #include#include#include#include#include us ...

  6. jdbc Template 介绍和 spring 链接数据源的四种方式

    为什么具体讲template介绍和spring jdbc链接数据源的几种方式放在一起介绍,在于你在使用template的时候,具体配置数据源,或者在链接配置数据源的时候要是使用template,就在在 ...

  7. php协程和goroutine,golang中四种方式实现子goroutine与主协程的同步

    如何实现子goroutine与主线程的同步 第一种方式:time.sleep(),这种方式很太死板,就不演示了. 第二种方式:使用channel机制,每个goroutine传一个channel进去然后 ...

  8. Linux进程通信的四种方式——共享内存、信号量、无名管道、消息队列|实验、代码、分析、总结

    Linux进程通信的四种方式--共享内存.信号量.无名管道.消息队列|实验.代码.分析.总结 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须 ...

  9. 安卓androidstudio访问本地接口_安卓开发之数据存储在本地的四种方式

    ​ 安卓开发之数据存储在本地的四种方式 本地数据存储,在安卓开发过程中是不可避免的一个话题.这些本地的数据可能是用户的设置,程序的设置,用户的数据图片, 也可能是网络传输的一些缓冲数据. 基本上我们有 ...

最新文章

  1. leetcode dfs_深度优先搜索:具有6个Leetcode示例的DFS图遍历指南
  2. 超级实用的linux 下shell快捷键汇总
  3. php 第三方DB库NOTORM
  4. Warning: mysqli_connect()_php链接MySQL8.0_异常
  5. c++学习笔记之数组及vector
  6. LeetCode 531. 孤独像素 I
  7. Laravel核心解读--Session源码解析
  8. 【算法】剑指 Offer 56 - II. 数组中数字出现的次数 II 【重刷】
  9. python导入类有红线_解决Python中导入自己写的类,被划红线,但不影响执行的问题...
  10. 为什么你会觉得微服务架构很别扭
  11. Git下修改提交的author和email信息
  12. mschart控件_使用MSChart控件绘制图表。
  13. 软件测试工程师必备的27个基础技能
  14. 在线工具网,程序员必备小网址
  15. mldonkey 安装详细过程
  16. 电脑端10大图片处理类神器
  17. 数据(机器学习/深度学习)竞赛平台——Kaggle
  18. eclipse安装hadoop插件教程
  19. 大数据起步之wormhole初识
  20. memtester4.3.0

热门文章

  1. 蓝牙技术与个人局域网
  2. 主动式电容笔好用吗?平板电脑性价比电容笔测评
  3. html文字两端对齐
  4. 微信不是互联网,互联网也不是微信。
  5. 海康威视导出监控视频教程(详细版)
  6. Python-文件读写和嵌套循环
  7. 四合一图床上传-高速Cdn图床百度/新浪/360/搜狗图床
  8. 做养生吸引粉丝什么产品好?如何做好新客引粉?
  9. 移动c:\windows\installer文件夹
  10. 开源高仿印象笔记的iOS应用——朝夕笔记(Oncenote)