C#基于COM方式读取Excel表格的方法
作者:kagula 时间:2021-09-15 12:35:36
本文实例讲述了C#基于COM方式读取Excel表格的方法。分享给大家供大家参考,具体如下:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Collections;
//TestEnviroment:VS2013Update4 Excel2007
//Read by COM Object
namespace SmartStore.LocalModel
{
public class ExcelTable
{
private string _path;
public ExcelTable()
{
_path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
_path += "条码对照表.xls";
}
public void ReadEPC2BarCode(out ArrayList arrayPI)
{
DataTable dt = ReadSheet(2);
arrayPI = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
EPC2BarCode eb = new EPC2BarCode();
eb.EPC = (string)dr["epcID"];
eb.Barcode = (string)dr["条形码"];
eb.EPC = eb.EPC.Trim();
eb.Barcode = eb.Barcode.Trim();
if (eb.EPC == null || eb.EPC.Length <= 0)
break;
arrayPI.Add(eb);
}
}
public void ReadProductInfo(out ArrayList arrayPI)
{
DataTable dt = ReadSheet(1);
arrayPI = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
ProductInfo pi = new ProductInfo();
pi.Name = (string)dr["商品名称"];
pi.SN = (string)dr["商品编号"];
pi.BarCode = (string)dr["商品条码"];
pi.Brand = (string)dr["品牌"];
pi.Color = (string)dr["颜色"];
pi.Size = (string)dr["尺码"];
pi.Name = pi.Name.Trim();
pi.SN = pi.SN.Trim();
pi.BarCode = pi.BarCode.Trim();
pi.Brand = pi.Brand.Trim();
pi.Color = pi.Color.Trim();
pi.Size = pi.Size.Trim();
if (pi.Name == null || pi.Name.Length <= 0)
break;
arrayPI.Add(pi);
}
}
private DataTable ReadSheet(int indexSheet)
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Sheets sheets;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
object oMissiong = System.Reflection.Missing.Value;
System.Data.DataTable dt = new System.Data.DataTable();
try
{
workbook = app.Workbooks.Open(_path, oMissiong, oMissiong, oMissiong, oMissiong,
oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
//将数据读入到DataTable中——Start
sheets = workbook.Worksheets;
//输入1, 读取第一张表
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(indexSheet);
if (worksheet == null)
return null;
string cellContent;
int iRowCount = worksheet.UsedRange.Rows.Count;
int iColCount = worksheet.UsedRange.Columns.Count;
Microsoft.Office.Interop.Excel.Range range;
//负责列头Start
DataColumn dc;
int ColumnID = 1;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 1];
while (range.Text.ToString().Trim() != "")
{
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = range.Text.ToString().Trim();
dt.Columns.Add(dc);
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, ++ColumnID];
}
//End
for (int iRow = 2; iRow <= iRowCount; iRow++)
{
DataRow dr = dt.NewRow();
for (int iCol = 1; iCol <= iColCount; iCol++)
{
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, iCol];
cellContent = (range.Value2 == null) ? "" : range.Text.ToString();
//if (iRow == 1)
//{
// dt.Columns.Add(cellContent);
//}
//else
//{
dr[iCol - 1] = cellContent;
//}
}
//if (iRow != 1)
dt.Rows.Add(dr);
}
//将数据读入到DataTable中——End
return dt;
}
catch
{
return null;
}
finally
{
workbook.Close(false, oMissiong, oMissiong);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}
希望本文所述对大家C#程序设计有所帮助。
标签:C#,COM,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA基本概念详解
2022-06-09 11:54:53
![](https://img.aspxhome.com/file/2023/6/70996_0s.jpg)
深入了解c# 匿名类型
2022-04-08 22:38:21
JavaWeb验证码校验功能代码实例
2022-07-18 23:46:52
基于sharding-jdbc的使用限制
2023-09-01 12:33:58
详细解读Hibernate的缓存机制
2023-05-03 21:02:52
Java验证码功能的实现方法
2023-07-05 21:28:21
![](https://img.aspxhome.com/file/2023/4/92724_0s.gif)
SpringMVC 向jsp页面传递数据库读取到的值方法
2022-03-29 00:51:15
Java C++ 算法leetcode828统计子串中唯一字符乘法原理
2022-05-09 11:19:18
![](https://img.aspxhome.com/file/2023/8/61118_0s.png)
实例解析如何正确使用Java数组
2023-04-14 23:00:55
java 实现KMP算法
2022-09-14 15:44:40
![](https://img.aspxhome.com/file/2023/4/83104_0s.gif)
JAVA判断空值方法原理解析
2021-09-28 14:37:34
Java并发之ReentrantLock类源码解析
2022-08-09 18:10:35
![](https://img.aspxhome.com/file/2023/5/69695_0s.jpg)
详解Springboot自定义异常处理
2021-11-04 17:15:12
Java实现合并两个有序序列算法示例
2021-09-06 23:23:53
Jenkins自动构建部署项目到远程服务器上的方法步骤
2023-11-25 00:21:40
![](https://img.aspxhome.com/file/2023/8/85948_0s.png)
Fluent Mybatis让你摆脱Xml文件的技巧
2023-10-02 18:31:00
![](https://img.aspxhome.com/file/2023/0/91090_0s.jpg)
c#使用Unity粒子实现炮塔发射系统
2023-11-04 19:05:19
![](https://img.aspxhome.com/file/2023/2/92922_0s.png)
idea项目的左侧目录没了如何设置
2022-03-21 15:45:43
![](https://img.aspxhome.com/file/2023/1/86801_0s.jpg)
mybatis plus新增(insert)数据获取主键id的问题
2023-08-09 10:50:52
Springboot详解实现食品仓库管理系统流程
2023-11-10 18:33:15
![](https://img.aspxhome.com/file/2023/8/58688_0s.png)