我正在開發一個窗體窗體應用程序在C#。我的程序將讀取excel單元格中的值並將數據插入到DataGridView中以顯示用戶。將字符串數組值插入DataGridView C#
我讀完了excel文件後,我得到了字符串數組的結果。我如何插入DataGridView?
private void btnImport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application ExcelObj = null;
ExcelObj = new Microsoft.Office.Interop.Excel.Application();
if (ExcelObj != null)
{
if (tbxFileDirectory.Text != "")
{
Workbook workbook = ExcelObj.Workbooks.Open(
openFD.FileName, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true);
Sheets sheets = workbook.Worksheets;
//Get excel sheet number
Worksheet worksheet = (Worksheet)sheets.get_Item(1);
for (int i = 6; i <= worksheet.UsedRange.Rows.Count; i++)
{
Range range = worksheet.get_Range("A" + i.ToString(), "K" + i.ToString());
Array myvalues = (Array)range.Cells.Value;
string[] strArray = ConvertToStringArray(myvalues);
//INSERT INTO DATAGRIDVIEW
}
MessageBox.Show("File was imported successfully");
}
else
{
MessageBox.Show("Please select the file to import.");
}
}
else
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
}
}
我的DataGrid中有4列,產品ID,產品名稱,價格,數量&。我想在每列中插入這些值,strArray [0],strArray [1],strArray [9] & strArray [10]。我怎樣才能做到這一點?任何幫助都感激不盡!
我可以直接將字符串插入DataGridView,但我會使用DataTable將它們插入到它。謝謝你的幫助。 +1 –