C#基于COM方式讀取Excel表格的方法
本文實(shí)例講述了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 += "條碼對(duì)照表.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["商品編號(hào)"];
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);
//將數(shù)據(jù)讀入到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;
//負(fù)責(zé)列頭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);
}
//將數(shù)據(jù)讀入到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();
}
}
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#操作Excel技巧總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
- C#?將Excel轉(zhuǎn)為PDF時(shí)自定義表格紙張大小的代碼思路
- c#使用EPPlus封裝excel表格導(dǎo)入功能的問(wèn)題
- C#插入圖片到Excel表格單元格代碼詳解
- C#在Excel表格中插入、編輯和刪除批注
- c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中
- c#中合并excel表格的方法示例
- C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
- C#使用oledb讀取excel表格內(nèi)容到datatable的方法
- C#使用Ado.Net更新和添加數(shù)據(jù)到Excel表格的方法
- 基于NPOI用C#開(kāi)發(fā)的Excel以及表格設(shè)置
相關(guān)文章
C#調(diào)用FFmpeg操作音視頻的實(shí)現(xiàn)示例
本文主要介紹了C#調(diào)用FFmpeg操作音視頻的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
c#中單例類與靜態(tài)類的區(qū)別以及使用場(chǎng)景
這篇文章主要給大家介紹了關(guān)于c#中單例類與靜態(tài)類的區(qū)別以及使用場(chǎng)景的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
基于WPF繪制一個(gè)點(diǎn)贊大拇指動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)繪制一個(gè)點(diǎn)贊大拇指動(dòng)畫,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2023-02-02
C# IQueryable<T>揭開(kāi)表達(dá)式樹(shù)的神秘面紗
這篇文章主要介紹了C# IQueryable<T>表達(dá)式樹(shù),對(duì)IQueryable<T>感興趣的同學(xué),必須要仔細(xì)看一下2021-04-04
winfrom 打印表格 字符串的封裝實(shí)現(xiàn)代碼 附源碼下載
以前寫打印都是根據(jù)打印機(jī)的型號(hào),找開(kāi)發(fā)類庫(kù)。然后在此基礎(chǔ)上開(kāi)發(fā)。寫的多了自然就想到了封裝。這是還是想到了微軟,微軟封裝了PrintDocument的打印類。但這只是在低層對(duì)串口的封裝2013-02-02
C#動(dòng)態(tài)創(chuàng)建button的方法
這篇文章主要介紹了C#動(dòng)態(tài)創(chuàng)建button的方法,涉及C#按鈕屬性動(dòng)態(tài)設(shè)置的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C# textbox實(shí)時(shí)輸入值檢測(cè)方式
這篇文章主要介紹了C# textbox實(shí)時(shí)輸入值檢測(cè)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

