c#第三次作业 C#读取Excel文件,并保存成HTML格式
2015-05-08 20:47
701 查看
【作业要求】
[b]1.
C#读取Excel文件
[/b]
[b][b]2.
保存成HTML格式
[/b][/b]
[b][b]3.用文本显示上一步保存内容(无聊的时候随便加的)[/b][/b]
[b][b]
[/b][/b]
[b]1.
C#读取Excel文件
[/b]
[b][b]2.
保存成HTML格式
[/b][/b]
[b][b]3.用文本显示上一步保存内容(无聊的时候随便加的)[/b][/b]
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "工作簿(*.xls)|*.xls|所有文件(*.*)|*.*"; if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK) { DataSet ds = ExcelToDS(openfile.FileName); PrintRows(ds); } } 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(); ds.Tables.Add(table1); myCommand.Fill(table1); dataGridView1.DataSource = table1; return ds; } private void PrintRows(DataSet dataSet) { using (StreamWriter sw = new StreamWriter("D:/ex03_demo.html", false, Encoding.Default)) { sw.WriteLine("<html>\r\n <head>\r\n <title>我们的网页</title>\r\n </head> \r\n <body>"); String strName = "小王"; String strWebsite = "http:\\549002798.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 />"); } // Read and show each line from the file. sw.WriteLine("</body> \r\n </html>"); } } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void button1_Click_1(object sender, EventArgs e) { StreamReader hc = new StreamReader(@"D:\ex03_demo.html", UnicodeEncoding.GetEncoding("GB2312")); string ss = hc.ReadToEnd(); MessageBox.Show(ss); } } }运行结果
[b][b]
[/b][/b]
相关文章推荐
- 《C#程序设计》第三次作业 C#读取Excel文件,并保存为文本文件
- C#第三次作业-读取excel文件
- C# 读写 excel (它支持html格式的excel文件)
- 第三次C#作业-Excel数据读取及HTML文件初步
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- C# conn.open() 外部表不是预期的格式( 读取EXCEL文件出错)
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- C#第三次作业 Excel数据读取 及 HTML文件初步
- C# 读取Excel文件 并保存为html文件
- c#第三次作业——Excel数据读取 及 HTML文件初步(目标三)
- C#第三次作业 Excel数据读取 及 HTML文件初步
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- C# 保存Excel文件 ( 打开Excel文件格式与扩展名指定格式不一致)
- C#第三次作业:Excel数据读取 及 HTML文件初步
- C#读取,修改并保存html文件——实例
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- C# 读写 excel (它支持html格式的excel文件)
- C#第三次作业—目标二(Excel数据读取 及 HTML文件初步)
- C# conn.open() 外部表不是预期的格式( 读取EXCEL文件出错)
- 【c#第三次作业】打开excel,并保存于sql中