咳咳,先看下效果:
这是读取excel时,我把它显示在datagirdview上:
其实,我应该打上马赛克的。。。
放大窗体吧。。。
显示出来了。。。各位别误会,这不是SQL读取出来的,读取的是Excel。
额,写入html文件是吧。。。
到这里已经保存为:
好吧,打开文件看看:
这是在浏览器打开的,由于我没有加入什么CSS代码所以界面老吐请原谅。。。任意打开一个链接试一下,比如:彭阳
好吧,基本功能就是这样子了。下面讲一下如何实现的吧,
从窗体我们可以看到:一个button,一个datagirdview。简单至极
下面是如何打开文件并且缓存excel的数据于table1中:
上述代码涉及sql语句,比如其中的 strExcel = "select 姓名,作业网址 from [sheet1$] ";这里就是筛选姓名和作业网址列。
这里记得引用oledb类:using System.Data.OleDb;
table1存放了所需要的数据,那么怎么让这些数据显示在datagirdview上呢?
好吧,显示就是这么完成的。
那么如何保存为html文档呢?
首先html基本格式包括:
等标签,这里我们通过文件输出流把这些东西都输出到文件里:办法比较老土不要吐槽。。
大家注意到了str吗?
这里str存储的就是excel数据并且在我的修改后变为xxx的形式,也就是链接。
这就是怎么实现的过程了,只要文件输入并且保存为html文档就可以了。
好了,解决问题,下面是全部代码
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string FileName;
public string str;
public string str2;
public string path1;
static public string a;
static public string b;
static public string c;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFiledialog1 = new OpenFileDialog();
openFiledialog1.Filter = "Excel文件|*.xls"; //打开excel文件,并读取内容
if (openFiledialog1.ShowDialog() == DialogResult.OK)
{
String path = openFiledialog1.FileName; //保存路径
//存取excel数据
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);
//另存为html文件
saveFileDialog1.Filter = "HTML文件(*.html)|*.html";
dataGridView1.DataSource = table1; //数据显示在datagridview上
MessageBox.Show("Please set a HTML file to save the data.");
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = saveFileDialog1.FileName;
SaveFileDialog save = new SaveFileDialog();
path1 = FileName;
MessageBox.Show("Congratuation! The data has successfully save!");
foreach (DataRow dr in table1.Rows)
{
//str +=dr["姓名"].ToString() + "," + dr["作业网址"].ToString() + "\r\n";
str +=a+dr["作业网址"].ToString() +b+ dr["姓名"].ToString() +"
\r\n"+c;}
FileStream f = new FileStream(path1, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(f);
sw.WriteLine("");
sw.WriteLine("
");sw.WriteLine(@"");
sw.WriteLine("
Shen me gui!");sw.WriteLine("");
sw.WriteLine("
");sw.Write(str);
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.Flush();
sw.Close();
f.Close();
// File.WriteAllText(FileName,str);
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
} 搞定!