码迷,mamicode.com
首页 > Windows程序 > 详细

C#第三次作业:在c#中导入excel,并生成html文件

时间:2015-07-07 17:04:25      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

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文件,打开链接 就可以进入博文

版权声明:本文为博主原创文章,未经博主允许不得转载。

C#第三次作业:在c#中导入excel,并生成html文件

标签:

原文地址:http://blog.csdn.net/zjc710268362/article/details/46789965

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!