我创建了一个数据表列表:
List<DataTable> dataTableList = new List<DataTable>();
我用列创建了一个临时数据表:
DataTable tmp = new DataTable();
tmp.Columns.Add("SenderCostCenter" , typeof(String));
tmp.Columns.Add("ActivityType" , typeof(String));
tmp.Columns.Add("Quantity" , typeof(String));
tmp.Columns.Add("Unit", typeof(String));
tmp.Columns.Add("Costcnter" , typeof(String));
tmp.Columns.Add("InternalOrder" , typeof(String));
tmp.Columns.Add("WBSElement" , typeof(String));
tmp.Columns.Add("SalesOrder", typeof(String));
tmp.Columns.Add("SalesOrderItem" , typeof(String));
tmp.Columns.Add("Text" , typeof(String));
然后我导入了一些数据并用它填充了tmp
DataTable
我在一个循环中重复创建了tmp
个DataTable 13次。
此循环在另一个循环内和外部循环内,在内循环之后,我想用tmp数据表填充DataTable列表。
这是我的代码:
dataTableList[fs1].Columns.Add("SenderCostSender" , typeof(String));
dataTableList[fs1].Columns.Add("ActivityType", typeof(String));
dataTableList[fs1].Columns.Add("Quantity", typeof(String));
dataTableList[fs1].Columns.Add("Unit", typeof(String));
dataTableList[fs1].Columns.Add("Costcenter", typeof(String));
dataTableList[fs1].Columns.Add("InternalOrder", typeof(String));
dataTableList[fs1].Columns.Add("WBSElement", typeof(String));
dataTableList[fs1].Columns.Add("SalesOrder", typeof(String));
dataTableList[fs1].Columns.Add("SalesOrderItem", typeof(String));
dataTableList[fs1].Columns.Add("Text" , typeof(String));
dataTableList[fs1] = tmp;
fs1
是要遍历DataTableList的外部循环变量。
数据导入工作正常,但是当程序到达dataTableList[fs1].Columns.Add("SenderCostSender" , typeof(string));
时,我遇到了System.ArgumentOutOfRangeException
错误。
谢谢您的帮助。
这是完整的方法:(List<dataTable>
是全局的)
using (var fbd = new FolderBrowserDialog())
{
//Folder-Browser-Dialog öffnen
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
//Menge an Excel-Daten rausfinden
string[] files = System.IO.Directory.GetFiles(fbd.SelectedPath , "*.xlsx");
for (int fs1=0;fs1<=files.Length;fs1++)
{
//Excel Import
Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = application.Workbooks.Open(files[fs1]);
Microsoft.Office.Interop.Excel._Worksheet _Worksheet = workbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range range = _Worksheet.UsedRange;
//Neues DataTable
DataTable tmp = new DataTable();
tmp.Columns.Add("SenderCostCenter" , typeof(String));
tmp.Columns.Add("ActivityType" , typeof(String));
tmp.Columns.Add("Quantity" , typeof(String));
tmp.Columns.Add("Unit", typeof(String));
tmp.Columns.Add("Costcnter" , typeof(String));
tmp.Columns.Add("InternalOrder" , typeof(String));
tmp.Columns.Add("WBSElement" , typeof(String));
tmp.Columns.Add("SalesOrder", typeof(String));
tmp.Columns.Add("SalesOrderItem" , typeof(String));
tmp.Columns.Add("Text" , typeof(String));
//DataTable füllen
for (int fs2 = 24; fs2 <= range.Rows.Count; fs2++)
{
//DataRow erstellen & füllen
DataRow dataRow = tmp.NewRow();
dataRow[0] = Convert.ToString(range.Cells[fs2, 2].Value2);
dataRow[1] = Convert.ToString(range.Cells[fs2, 3].Value2);
dataRow[2] = Convert.ToString(range.Cells[fs2, 4].Value2);
dataRow[3] = Convert.ToString(range.Cells[fs2, 5].Value2);
dataRow[4] = Convert.ToString(range.Cells[fs2, 6].Value2);
dataRow[5] = Convert.ToString(range.Cells[fs2, 7].Value2);
dataRow[6] = Convert.ToString(range.Cells[fs2, 8].Value2);
dataRow[7] = Convert.ToString(range.Cells[fs2, 9].Value2);
dataRow[8] = Convert.ToString(range.Cells[fs2, 10].Value2);
dataRow[9] = Convert.ToString(range.Cells[fs2, 11].Value2);
tmp.Rows.Add(dataRow);
}
//tmp DataTable in die Globale DataTable-List kopieren
dataTableList[fs1].Columns.Add("SenderCostSender" , typeof(String));
dataTableList[fs1].Columns.Add("ActivityType", typeof(String));
dataTableList[fs1].Columns.Add("Quantity", typeof(String));
dataTableList[fs1].Columns.Add("Unit", typeof(String));
dataTableList[fs1].Columns.Add("Costcenter", typeof(String));
dataTableList[fs1].Columns.Add("InternalOrder", typeof(String));
dataTableList[fs1].Columns.Add("WBSElement", typeof(String));
dataTableList[fs1].Columns.Add("SalesOrder", typeof(String));
dataTableList[fs1].Columns.Add("SalesOrderItem", typeof(String));
dataTableList[fs1].Columns.Add("Text" , typeof(String));
dataTableList[fs1] = tmp;
}
}
}
}
答案 0 :(得分:0)
DataTable d = new DataTable();
List<DataTable> l = new List<DataTable>();
l.Add(d);