我想阅读一些Excel文件并转换为我自己的Excel模板。我想在每一行读取B列(B1,B2,B3 ......就像这样)。
如果此列中有数字;在B3中有一个像“1,2,3,4,5,6,7,8,9”这样的数字然后我会得到整行并将它带到一个数组[i]。如果B4中有“5”数字,那么它将获得整行并将其带到数组[i]。如果相关行中没有数字,它将继续到下一行。
它将继续读取Excel文件的结尾。我想拿这个数组写一个新的Excel文件。
答案 0 :(得分:2)
示例:
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);
}
答案 1 :(得分:0)