0

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

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

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

4

2 回答 2

2
  1. 在您的计算机上下载并安装 Office 2003 主要互操作程序集
  2. 创建一个 Visual Studio 项目并从 GAC 添加对“Microsoft.Office.Interop.Excel.dll”的引用。
  3. 现在您可以编写此代码来从任何 Excel 文件中读取数据

例子:

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);
}
于 2009-02-06T07:39:56.357 回答
0

请看

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

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

你可以实现你的想法..

于 2009-02-06T07:11:08.473 回答