如何通过C#将Excel数据获取到Datagridview [英] how to get excel data to datagridview through c#
本文介绍了如何通过C#将Excel数据获取到Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
如何通过C#将excel文件数据获取到datagridview并仅使用sql连接
谢谢.
Hello,
How to get excel files data to datagridview and using only sql connection through c#
Thanks.
推荐答案
以下代码可能适合您,
Hi,
below code may work for you,
this.openFileDialog1.FileName = "*.xls";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
openFileDialog1.FileName, 0, true, 5,
"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
0, true);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
for (int i = 1; i <= 10; i++)
{
Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value;
string[] strArray = ConvertToStringArray(myvalues);
}
}
现在,将strArray分配给网格数据源.
从此链接获取的代码.
谢谢.
-amit
Now, assign strArray to your grid datasource.
code taken from this link.
thanks.
-amit
excel-data -into-gridview-in-asp.net [ ^ ]
类似 ^ ]和解决方案.
此链接-[将数据从Excel导入到C#中的DataGridView] [ ^ ]可能会帮助您
Similar question-[How to get data from excel sheet to datagridview in c#][^] in CP with solutions.
This link-[Import Data from Excel to DataGridView in C#][^] might help you
这篇关于如何通过C#将Excel数据获取到Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文