This article describes the method of C# reading Excel tables based on COM. Share it for your reference, as follows:
using System; using ; using ; using ; using ; using ; using ; using ; using ; //TestEnviroment:VS2013Update4 Excel2007 //Read by COM Object namespace { public class ExcelTable { private string _path; public ExcelTable() { _path = ; _path += "Barcode comparison table.xls"; } public void ReadEPC2BarCode(out ArrayList arrayPI) { DataTable dt = ReadSheet(2); arrayPI = new ArrayList(); foreach (DataRow dr in ) { EPC2BarCode eb = new EPC2BarCode(); = (string)dr["epcID"]; = (string)dr["Barcode"]; = (); = (); if ( == null || <= 0) break; (eb); } } public void ReadProductInfo(out ArrayList arrayPI) { DataTable dt = ReadSheet(1); arrayPI = new ArrayList(); foreach (DataRow dr in ) { ProductInfo pi = new ProductInfo(); = (string)dr["Product Name"]; = (string)dr["Product Number"]; = (string)dr["Product Barcode"]; = (string)dr["brand"]; = (string)dr["color"]; = (string)dr["size"]; = (); = (); = (); = (); = (); = (); if ( == null || <= 0) break; (pi); } } private DataTable ReadSheet(int indexSheet) { app = new (); sheets; workbook = null; object oMissiong = ; dt = new (); try { workbook = (_path, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong); //Read the data into the DataTable - Start sheets = ; //Enter 1 to read the first table worksheet = ()sheets.get_Item(indexSheet); if (worksheet == null) return null; string cellContent; int iRowCount = ; int iColCount = ; range; //Responsible for the Start DataColumn dc; int ColumnID = 1; range = ()[1, 1]; while (().Trim() != "") { dc = new DataColumn(); = (""); = ().Trim(); (dc); range = ()[1, ++ColumnID]; } //End for (int iRow = 2; iRow <= iRowCount; iRow++) { DataRow dr = (); for (int iCol = 1; iCol <= iColCount; iCol++) { range = ()[iRow, iCol]; cellContent = (range.Value2 == null) ? "" : (); //if (iRow == 1) //{ // (cellContent); //} //else //{ dr[iCol - 1] = cellContent; //} } //if (iRow != 1) (dr); } //Read the data into the DataTable—End return dt; } catch { return null; } finally { (false, oMissiong, oMissiong); (workbook); workbook = null; (); (); (app); app = null; (); (); } } } }
For more information about C# related content, please check out the topic of this site:Summary of C# operation skills》、《Summary of thread usage techniques for C# programming》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.