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;
}
是的,你是對的我已經測試過,但如果我想在特定的位置添加數組,所以我會做什麼。像drow.ItemArray [i]。我是一個基於索引的位置 –
請參閱我的編輯。它的表格不是Row的位置。如果表格中沒有任何指定的索引,那麼它將被添加到位置1。 – Kashif
它的工作,但增加了新的數組在位置J明智意味着從上到下,而是我想補充這在位置j collums明智的手段從左到右。在位置j遠離第一個collum –