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#常見控件用法教程》、《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#開發(fā)的Excel以及表格設(shè)置
相關(guān)文章
c#關(guān)于非托管內(nèi)存的釋放問(wèn)題及解讀
這篇文章主要介紹了c#關(guān)于非托管內(nèi)存的釋放問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
C#實(shí)現(xiàn)快遞api接口調(diào)用方法
這篇文章主要介紹了C#實(shí)現(xiàn)快遞api接口調(diào)用方法,主要是通過(guò)快遞API網(wǎng)接口的服務(wù),使用的時(shí)候直接申請(qǐng)個(gè)接口UID即可,有需要的小伙伴來(lái)參考下吧。2015-03-03
C#使用SQL DataAdapter數(shù)據(jù)適配代碼實(shí)例
今天小編就為大家分享一篇關(guān)于C#使用SQL DataAdapter數(shù)據(jù)適配代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
C#多線程開發(fā)實(shí)戰(zhàn)記錄之線程基礎(chǔ)
線程是一個(gè)獨(dú)立的運(yùn)行單元,每個(gè)進(jìn)程內(nèi)部有多個(gè)線程,每個(gè)線程可以各自同時(shí)執(zhí)行指令,每個(gè)線程有自己獨(dú)立的棧,但是與進(jìn)程內(nèi)的其他線程共享內(nèi)存,這篇文章主要給大家介紹了關(guān)于C#多線程開發(fā)實(shí)戰(zhàn)記錄之線程基礎(chǔ)的相關(guān)資料,需要的朋友可以參考下2021-09-09
C#使用Dynamic實(shí)現(xiàn)簡(jiǎn)化反射
這篇文章主要為大家詳細(xì)介紹了C#如何使用Dynamic來(lái)實(shí)現(xiàn)簡(jiǎn)化反射,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07
C#實(shí)現(xiàn)TCP客戶端和服務(wù)器的基本功能
本文將介紹如何使用C#實(shí)現(xiàn)TCP客戶端和服務(wù)器的基本功能,客戶端與服務(wù)器可以相互發(fā)送消息,文章通過(guò)代碼講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12

