我想阅读一些 Excel 文件并转换为我自己的 Excel 模板。我想每行读取 B 列(B1、B2、B3 ......就像这样)。
如果此栏有数字;在 B3 中有像“1,2,3,4,5,6,7,8,9”这样的数字,然后我会得到整行并将其放入数组 [i]。如果 B4 中有“5”数字,那么它将获取这一整行并将其带到数组 [i]。如果相关行中没有数字,它将继续下一行。
它将继续读取 Excel 文件的末尾。我想把这个数组写入一个新的 Excel 文件。
我想阅读一些 Excel 文件并转换为我自己的 Excel 模板。我想每行读取 B 列(B1、B2、B3 ......就像这样)。
如果此栏有数字;在 B3 中有像“1,2,3,4,5,6,7,8,9”这样的数字,然后我会得到整行并将其放入数组 [i]。如果 B4 中有“5”数字,那么它将获取这一整行并将其带到数组 [i]。如果相关行中没有数字,它将继续下一行。
它将继续读取 Excel 文件的末尾。我想把这个数组写入一个新的 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);
}
请看
http://support.microsoft.com/kb/306572
和
http://support.microsoft.com/kb/306023/EN-US/
你可以实现你的想法..