国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#基于COM方式讀取Excel表格的方法

2020-01-24 01:04:12
字體:
來源:轉載
供稿:網友

本文實例講述了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 Objectnamespace 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#操作Excel技巧總結》、《C#程序設計之線程使用技巧總結》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》、《C#數組操作技巧總結》及《C#面向對象程序設計入門教程

希望本文所述對大家C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 军事| 永胜县| 南宁市| 宜兰市| 定日县| 黔西| 阿鲁科尔沁旗| 鸡泽县| 恩平市| 百色市| 老河口市| 静海县| 郑州市| 明光市| 双辽市| 乌恰县| 庐江县| 井研县| 吉林省| 丹棱县| 天柱县| 砚山县| 察雅县| 荥经县| 临清市| 饶平县| 镇安县| 广安市| 民勤县| 孝义市| 凤山市| 略阳县| 长丰县| 长治县| 枣强县| 铁岭市| 砀山县| 铁岭市| 洛川县| 洛川县| 阿坝县|