我有一个数组“MyArray”,并希望这个数组添加到位置spcified的数据表中。第二行显示我想要将itemarray指定为位置3行前进的位置。我为一个像datarow.insertAt [i]这样的单个字符串做了同样的过程,但是当我在一个特定的位置插入一堆数组时,它就无法工作了。
string[] MyArray= new string[3];
MyArray[0] = "Tom";
MyArray[1] = "Canada";
MyArray[2] = "+42-54948354-9";
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Address");
table.Columns.Add("CellNo");
DataRow drow;
drow = table.NewRow();
drow.ItemArray[2] = MyArray; //Here I want to Add Array to DataRow at position 3 rows forward.
table.Rows.Add(drow);
答案 0 :(得分:2)
string[] MyArray= new string[3];
MyArray[0] = "Tom";
MyArray[1] = "Canada";
MyArray[2] = "+42-54948354-9";
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Address");
table.Columns.Add("CellNo");
DataRow drow;
drow = table.NewRow();
drow.ItemArray = MyArray;
table.Rows.Add(drow);
要在特定索引处添加行,可以使用InsertAt
但如果表中没有行,则无法将其插入位置2(索引1),因为位置1处没有行,例如以下会工作:
string[] MyArray = new string[3];
MyArray[0] = "Tom";
MyArray[1] = "Canada";
MyArray[2] = "+42-54948354-9";
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Address");
table.Columns.Add("CellNo");
for (int j = 0; j < 2; j++)
{
DataRow drow;
drow = table.NewRow();
drow.ItemArray = MyArray;
table.Rows.InsertAt(drow, j);
}
编辑2:
string[] MyArray = new string[3];
MyArray[0] = "Tom";
MyArray[1] = "Canada";
MyArray[2] = "+42-54948354-9";
DataTable table = new DataTable();
//table.Columns.Add("Name");
//table.Columns.Add("Address");
//table.Columns.Add("CellNo");
DataRow drow;
drow = table.NewRow();
for (int j = 0; j < 2; j++)
{
table.Columns.Add(j.ToString());
string s = string.Join(" ", MyArray);
drow[j.ToString()] = s;
}
table.Rows.Add(drow);
答案 1 :(得分:0)
string[] MyArray= new string[3];
MyArray[0] = "Tom";
MyArray[1] = "Canada";
MyArray[2] = "+42-54948354-9";
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Address");
table.Columns.Add("CellNo");
DataRow drow;
drow = table.NewRow();
drow.ItemArray = string.Join(",", arr);
table.Rows.Add(drow);
答案 2 :(得分:0)
public System.Data.DataTable SetOne(string ExcelFilePath)
{
System.Data.DataTable table = new System.Data.DataTable();
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(ExcelFilePath,
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);
int NoOfSheetRows=0;
foreach (Worksheet item in app.Worksheets)
{
string sheetname = item.Name;
Worksheet sheet = (Worksheet)wb.Sheets[sheetname];
Range excelRange = sheet.UsedRange;
string fileRange = sheet.UsedRange.Address;
string filecolums = fileRange.Substring(6, 1);
List<string> str = new List<string>();
int cntr = 0;
foreach (Microsoft.Office.Interop.Excel.Range row in excelRange.Rows)
{
int rowNumber = row.Row;
string[] A4D4 = this.GetRange("A" + rowNumber + ":" + filecolums + "" + rowNumber + "", sheet);
if (rowNumber.Equals(1))
{
foreach (var itm in A4D4)
{
if (table.Columns.Contains(itm)==false)
{
table.Columns.Add(itm);
str.Add(itm);
cntr++;
}
else
{
table.Columns.Add(itm + "..");
str.Add(itm);
cntr++;
}
}
}
else
{
DataRow drow;
drow = table.NewRow();
drow.ItemArray = A4D4;
for(int i=0;i<A4D4.Length; i++)
{
drow= table.NewRow();
drow["name"] = A4D4[i];
table.Rows.Add(drow);
}
//table.Rows.InsertAt(drow, NoOfSheetRows);
//table.Rows.Add(drow); // This is Area where the Problem is created the the sheet 2,3,4 and so forth data is inserted to 1st Sheet Columns
}
}
NoOfSheetRows += cntr;
}
return table;
}
public string[] GetRange(string range, Worksheet excelWorksheet)
{
Microsoft.Office.Interop.Excel.Range workingRangeCells =
excelWorksheet.get_Range(range, Type.Missing);
System.Array array = (System.Array)workingRangeCells.Cells.Value2;
string[] arrayS = array.OfType<object>().Select(o => o.ToString()).ToArray();
return arrayS;
}