01.
try
02.
{
03.
string
sqlQuery =
string
.Empty;
04.
FileInfo workBook =
new
FileInfo(@
"D:\invoice0012.xlsx"
);
05.
using
(ExcelPackage xlPackage =
new
ExcelPackage(workBook))
06.
{
07.
ExcelWorksheet invWorksheet = xlPackage.Workbook.Worksheets[
"Sheet1"
];
08.
for
(
int
rowNumber = 10; rowNumber <= 50; rowNumber++)
09.
{
10.
int
myVal = 0;
11.
object
col1Value = invWorksheet.Cells[rowNumber, 1].Value;
12.
String myVar = col1Value.ToString();
13.
14.
if
(
int
.TryParse(myVar ,
out
myNum)) {
15.
object
col2Value = invWorksheet.Cells[rowNumber, 2].Value;
16.
object
col3Value = invWorksheet.Cells[rowNumber, 3].Value;
17.
if
( sqlQuery ==
string
.Empty ){
18.
sqlQuery =
"insert into (xxxx,xxx) values ( "
+ col2Value.ToString(); +
","
+col3Value.ToString();+
")"
;
19.
}
else
{
20.
sqlQuery +=
", ( "
+ col2Value.ToString(); +
","
+col3Value.ToString();+
")"
;
21.
}
22.
23.
}
else
{
24.
25.
}
26.
}
27.
}
28.
MessageBox.Show(sqlQuery);
29.
30.
}
31.
catch
(IOException ioEx)
32.
{
33.
if
(!String.IsNullOrEmpty(ioEx.Message))
34.
{
35.
if
(ioEx.Message.Contains(
"because it is being used by another process."
))
36.
{
37.
Console.WriteLine(
"Could not read example data. Please make sure it not open in Excel."
);
38.
}
39.
}
40.
Console.WriteLine(
"Could not read example data. "
+ ioEx.Message);
41.
}
42.
catch
(Exception ex)
43.
{
44.
Console.WriteLine(
"An error occured while reading example data. "
+ ex.ToString());
45.
}