using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
ComponentModel
;
using
System
.
Data
;
using
System
.
Drawing
;
using
System
.
Linq
;
using
System
.
Text
;
using
System
.
Threading
.
Tasks
;
using
System
.
Windows
.
Forms
;
using
System
.
Data
.
OleDb
;
using
System
.
IO
;
namespace
Excel
{
public
partial
class
Form1
:
Form
{
public
Form1
(
)
{
InitializeComponent
(
)
;
}
private
void
OpenButton_Click
(
object
sender
,
EventArgs
e
)
{
OpenFileDialog
openfile
=
new
OpenFileDialog
(
)
;
openfile
.
Filter
=
"
工作薄(*.xls)|*.xls|所有文件(*.*)|*.*
"
;
if
(
openfile
.
FilterIndex
==
1
&&
openfile
.
ShowDialog
(
)
==
DialogResult
.
OK
)
{
if
(
openfile
.
FileName
.
EndsWith
(
"
.xls
"
))
{
ExcelToDS
(
openfile
.
FileName
)
;//保证用户选择的是EXCEL文件
}
else
{
MessageBox
.
Show
(
"请选择.xls结尾的Excel文件"
)
;
return
;
}
}
using
(
StreamWriter
sw
=
new
StreamWriter
(
"e
:
\\
1.html
"
,
false
,
Encoding
.
Default
))
{
sw
.
WriteLine
(
"
<html><body>
"
)
;
foreach
(
DataTable
table
in
ExcelToDS
(
openfile
.
FileName
)
.
Tables
)
{
foreach
(
DataRow
row
in
table
.
Rows
)
{
foreach
(
DataColumn
column
in
table
.
Columns
)
{
if
(
column
.
ColumnName
==
"
作业网址
"
)
{
sw
.
Write
(
"
<a href=
\"
"
+
row
[
column
]
+
"
\"
>
"
)
;
}
if
(
column
.
ColumnName
==
"
姓名
"
)
{
//sw.WriteAsync(row[column] + "</a>");
sw
.
Write
(
row
[
column
]
+
"
</a>
"
)
;
}
}
sw
.
WriteLine
(
)
;
}
}
sw
.
WriteLine
(
"
</body></html>
"
)
;
}
}
public
DataSet
ExcelToDS
(
string
path
)
{
string
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+ @
path
+
"
;
"
+
"
Extended Properties=Excel 8.0;
"
;
OleDbConnection
conn
=
new
OleDbConnection
(
strConn
)
;
conn
.
Open
(
)
;
string
strExcel
=
"
"
;
OleDbDataAdapter
myCommand
=
null
;
DataSet
ds
=
null
;
strExcel
=
"
select * from [sheet1$]
"
;
myCommand
=
new
OleDbDataAdapter
(
strExcel
,
strConn
)
;
DataTable
table1
=
new
DataTable
(
)
;
ds
=
new
DataSet
(
)
;
myCommand
.
Fill
(
table1
)
;
myCommand
.
Fill
(
ds
)
;
dataGridView1
.
DataSource
=
table1
;
return
ds
;
}
}
}