用excel数据写入html,c#作业第三期(将excel数据写入html文档)

咳咳,先看下效果:

这是读取excel时,我把它显示在datagirdview上:

0818b9ca8b590ca3270a3433284dd417.png

其实,我应该打上马赛克的。。。

放大窗体吧。。。

0818b9ca8b590ca3270a3433284dd417.png

显示出来了。。。各位别误会,这不是SQL读取出来的,读取的是Excel。

额,写入html文件是吧。。。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

到这里已经保存为:

0818b9ca8b590ca3270a3433284dd417.png

好吧,打开文件看看:

0818b9ca8b590ca3270a3433284dd417.png

这是在浏览器打开的,由于我没有加入什么CSS代码所以界面老吐请原谅。。。任意打开一个链接试一下,比如:彭阳

0818b9ca8b590ca3270a3433284dd417.png

好吧,基本功能就是这样子了。下面讲一下如何实现的吧,

从窗体我们可以看到:一个button,一个datagirdview。简单至极

下面是如何打开文件并且缓存excel的数据于table1中:

0818b9ca8b590ca3270a3433284dd417.png

上述代码涉及sql语句,比如其中的 strExcel = "select 姓名,作业网址  from [sheet1$] ";这里就是筛选姓名和作业网址列。

这里记得引用oledb类:using System.Data.OleDb;

table1存放了所需要的数据,那么怎么让这些数据显示在datagirdview上呢?

0818b9ca8b590ca3270a3433284dd417.png

好吧,显示就是这么完成的。

那么如何保存为html文档呢?

首先html基本格式包括:

等标签,这里我们通过文件输出流把这些东西都输出到文件里:

0818b9ca8b590ca3270a3433284dd417.png

办法比较老土不要吐槽。。

大家注意到了str吗?

这里str存储的就是excel数据并且在我的修改后变为xxx的形式,也就是链接。

0818b9ca8b590ca3270a3433284dd417.png

这就是怎么实现的过程了,只要文件输入并且保存为html文档就可以了。

0818b9ca8b590ca3270a3433284dd417.png

好了,解决问题,下面是全部代码

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)

{

a =@"

b = @""">";

c = "
";

//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)

{

}

}

} 搞定!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值