01.
string
nameExcel =
""
;
02.
OpenFileDialog fdlg =
new
OpenFileDialog();
03.
fdlg.Title =
"C# Corner Open File Dialog"
;
04.
fdlg.InitialDirectory = @
"C:\Users\Administrator.UDZOWK481SBWBZN\Desktop\"
;
05.
fdlg.Filter =
"All files (*.*)|*.*|All Files (*.*)|*.xlsx"
;
06.
fdlg.FilterIndex = 2;
07.
fdlg.RestoreDirectory =
true
;
08.
if
(fdlg.ShowDialog() == DialogResult.OK)
09.
{
10.
textBox1.Text = fdlg.FileName;
11.
}
12.
13.
nameExcel = fdlg.FileName;
14.
15.
excel.Application App;
16.
excel.Workbook workBook;
17.
excel.Worksheet worksheet;
18.
string
strtemplate = nameExcel;
19.
App =
new
excel.Application();
20.
workBook = App.Workbooks.Open(nameExcel, 0,
true
, 5,
""
,
""
,
true
, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t"
,
false
,
false
, 0,
true
, 1, 0);
21.
worksheet = (excel.Worksheet)workBook.Worksheets.get_Item(1);
22.
23.
Microsoft.Office.Interop.Excel.Range range = worksheet.UsedRange;
24.
int
lastColumn = range.Columns.Count;
25.
int
lastRow = range.Rows.Count;
26.
27.
28.
29.
textBox2.Text = worksheet.get_Range(
"c4"
).Value2.ToString();
30.
31.
32.
DataTable dt =
new
DataTable();
33.
dt.Columns.Add(
new
DataColumn(
"1"
,
typeof
(
string
)));
34.
dt.Columns.Add(
new
DataColumn(
"2"
,
typeof
(
string
)));
35.
dt.Columns.Add(
new
DataColumn(
"3"
,
typeof
(
string
)));
36.
dt.Columns.Add(
new
DataColumn(
"4"
,
typeof
(
string
)));
37.
dt.Columns.Add(
new
DataColumn(
"5"
,
typeof
(
string
)));
38.
dt.Columns.Add(
new
DataColumn(
"6"
,
typeof
(
string
)));
39.
dt.Columns.Add(
new
DataColumn(
"7"
,
typeof
(
string
)));
40.
41.
42.
for
(
int
i; i = worksheet.Rows.End; i++)
43.
{
44.
45.
}