C # method of reading Excel tables based on COM _c# tutorials

Source: Internet
Author: User

The example in this article describes how C # reads Excel tables based on COM. Share to everyone for your reference, specific as follows:

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 + + "bar code table. 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["barcode"]; 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["commodity name"]; Pi.
        SN = (string) dr["commodity number"]; Pi.
        BarCode = (string) dr["commodity Barcode"]; Pi.
        Brand = (string) dr["brand"; Pi.
        color = (string) dr["Colors"]; Pi.
        Size = (string) dr["size"]; 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 M Icrosoft.
      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, OM
        Issiong, Omissiong, Omissiong, Omissiong, Omissiong, Omissiong); Read the data into the datatable--start sheets = workbook.
        worksheets; Enter 1, read the first table Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet) sheet
        S.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;
        Responsible for column header 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) wor Ksheet.
            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); //Read data into the 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 ();

 }
    }
  }
}

Read more about C # Interested readers can view the site topics: "C # Operations Excel Skills Summary", "C # Programming Thread usage Tips", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, summary of C # array manipulation techniques, and an introductory course on C # object-oriented programming

I hope this article will help you with C # programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.