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 += "條碼對照表.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); //將數(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ì)入門教程》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
上一篇:在WPF中動態(tài)加載XAML中的控件實(shí)例代碼
欄 目:C#教程
下一篇:C#使用foreach循環(huán)遍歷數(shù)組完整實(shí)例
本文標(biāo)題:C#基于COM方式讀取Excel表格的方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6381.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)綁定Combobox的方法
- 01-10C#實(shí)現(xiàn)在啟動目錄創(chuàng)建快捷方式的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法
- 01-10基于C#實(shí)現(xiàn)簡單離線注冊碼生成與驗(yàn)證
- 01-10C#基于UDP實(shí)現(xiàn)的P2P語音聊天工具
- 01-10C#實(shí)現(xiàn)基于加減按鈕形式控制系統(tǒng)音量及靜音的方法
- 01-10C#創(chuàng)建不規(guī)則窗體的4種方式詳解
- 01-10C#基于WebBrowser獲取cookie的實(shí)現(xiàn)方法
- 01-10C#讀取Excel的三種方式以及比較分析


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)