使用C#从Excel文件中读取特定列

时间:2022-05-12 01:39:15

I want to read some Excel files and convert to my own Excel template. I want to read B column every line (B1,B2,B3... going like this).

我想阅读一些Excel文件并转换为我自己的Excel模板。我想在每一行读取B列(B1,B2,B3 ......就像这样)。

If there is a number in this column; in B3 there is number like "1,2,3,4,5,6,7,8,9" then I'll get this whole line and take it to an array[i]. If there is "5" number in B4 then it'll get this whole line and take it to an array[i]. If there is no number in related line it will continue to next line.

如果此栏中有数字;在B3中有一些像“1,2,3,4,5,6,7,8,9”的数字然后我会得到这整行并把它带到一个数组[i]。如果在B4中有“5”数字,那么它将获得整行并将其带到数组[i]。如果相关行中没有数字,它将继续到下一行。

It will continue to read end of the Excel file. And I want to take this array and write to a new Excel file.

它将继续读取Excel文件的结尾。我想拿这个数组写一个新的Excel文件。

2 个解决方案

#1


  1. Download and install Office 2003 Primary Interop Assemblies on your computer
  2. 在您的计算机上下载并安装Office 2003主互操作程序集

  3. Create a Visual studio Project and add a reference to 'Microsoft.Office.Interop.Excel.dll' from the GAC.
  4. 创建一个Visual Studio项目并从GAC添加对“Microsoft.Office.Interop.Excel.dll”的引用。

  5. Now you can write this code to read data from any Excelfile
  6. 现在,您可以编写此代码以从任何Excelfile读取数据

Example:

using Excel = Microsoft.Office.Interop.Excel;

string pathOfExcelFile = "C:\\MyDataFile.xls";
Excel.Application excelApp = new Excel.Application();

excelApp.DisplayAlerts = false; //Don't want Excel to display error messageboxes  
Excel.Workbook workbook = excelApp.Workbooks.Open(pathOfExcelFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //This opens the file
Excel.Worksheet sheet = workbook.get_Item(1); //Get the first sheet in the file
Excel.Range bColumn = sheet.get_Range("B", null);

List<string> dataItems = new List<string>();

foreach (object o in bColumn) 
{
     Excel.Range row = o as Excel.Range;
     string s = row.get_Value(null);
     dataItems.Add(s);
}

#2


Please look at

请看

http://support.microsoft.com/kb/306572

and

http://support.microsoft.com/kb/306023/EN-US/

You can implement your idea..

你可以实现你的想法..

#1


  1. Download and install Office 2003 Primary Interop Assemblies on your computer
  2. 在您的计算机上下载并安装Office 2003主互操作程序集

  3. Create a Visual studio Project and add a reference to 'Microsoft.Office.Interop.Excel.dll' from the GAC.
  4. 创建一个Visual Studio项目并从GAC添加对“Microsoft.Office.Interop.Excel.dll”的引用。

  5. Now you can write this code to read data from any Excelfile
  6. 现在,您可以编写此代码以从任何Excelfile读取数据

Example:

using Excel = Microsoft.Office.Interop.Excel;

string pathOfExcelFile = "C:\\MyDataFile.xls";
Excel.Application excelApp = new Excel.Application();

excelApp.DisplayAlerts = false; //Don't want Excel to display error messageboxes  
Excel.Workbook workbook = excelApp.Workbooks.Open(pathOfExcelFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //This opens the file
Excel.Worksheet sheet = workbook.get_Item(1); //Get the first sheet in the file
Excel.Range bColumn = sheet.get_Range("B", null);

List<string> dataItems = new List<string>();

foreach (object o in bColumn) 
{
     Excel.Range row = o as Excel.Range;
     string s = row.get_Value(null);
     dataItems.Add(s);
}

#2


Please look at

请看

http://support.microsoft.com/kb/306572

and

http://support.microsoft.com/kb/306023/EN-US/

You can implement your idea..

你可以实现你的想法..