C#第三次作业:在c#中导入excel,并生成html文件
2015-05-08 21:21
295 查看
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.OleDb;using System.IO;namespace readFile{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){}private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){}private void button1_Click(object sender, EventArgs e){OpenFileDialog openFiledialog1 = new OpenFileDialog();openFiledialog1.Filter = "Excel文件|*.xls"; //打开excel文件,并读取内容if (openFiledialog1.FilterIndex == 1 && openFiledialog1.ShowDialog() == DialogResult.OK){DataSet ds = ExcelToDS(openFiledialog1.FileName);PrintRows(ds);}}public DataSet ExcelToDS(String path){//存取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);ds.Tables.Add(table1);myCommand.Fill(table1);dataGridView1.DataSource = table1; //数据显示在datagridview上return ds;}private void PrintRows(DataSet dataset){string strhtmlFile=@"e:/1.html";if(File.Exists(strhtmlFile)==false){FileStream myFs=new FileStream(strhtmlFile,FileMode.Create);myFs.Close();}int count=0;using(StreamWriter sw=new StreamWriter(strhtmlFile,false,Encoding.Default)){sw.WriteLine("<html>\r\n <head>\r\n <title>我们的网页</title>\r\n </head> \r\n <body>");String strName="小小";String strWebsite="http://791078306.qzone.qq.com";foreach(DataTable table in dataset.Tables){foreach(DataRow row in table.Rows){foreach(DataColumn column in table.Columns){if(column.ColumnName=="姓名")strName=(String) row[column];if(column.ColumnName=="作业网址")strWebsite =(String) row[column];}sw.WriteLine(@"<a href=""" + strWebsite + @"""> " + strName + @"</a> <br />");}}sw.WriteLine("</body> \r\n </html>");}}}}源代码窗体首页在窗体中选择出excel文件中的姓名和链接 打开excel文件进行选择 生成html文件用浏览器打开生成的html文件 打开html文件中某个链接
相关文章推荐
- C#第三次作业:导入excel,并生成文本文件
- C#第三次作业:Excel数据读取 及 HTML文件初步
- c#第三次作业——Excel数据读取 及 HTML文件初步(目标三)
- C#第三次作业—目标二(Excel数据读取 及 HTML文件初步)
- C#第三次作业 Excel数据读取 及 HTML文件初步
- C#第三次作业 Excel数据读取 及 HTML文件初步
- 第三次C#作业-Excel数据读取及HTML文件初步
- [导入]从DataView中生成Excel报表的方案(C#)
- 第三次作业---excel导入数据库及显示
- 第三次作业 Excel数据读取及HTML文件初步 目标2
- 【c#第三次作业】打开excel,并保存于sql中
- 《C#程序设计》第三次作业 C#读取Excel文件,并保存为文本文件
- C#第三次作业-读取excel文件
- c#第三次作业 C#读取Excel文件,并保存成HTML格式
- 第三次作业---excel导入数据库及显示(2)
- 第三次作业:Excel数据读取 及 HTML文件初步
- 第三次作业——将排课Excel导入系统
- 将Excel生成SQL语句完成数据库导入
- excel 数据导入程序(C#)
- Excel导入Oracle数据库C#winform完整代码