您的位置:首页 > Web前端 > HTML

C#第三次作业 Excel数据读取 及 HTML文件初步

2015-05-05 03:05 369 查看
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 Excel

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void OpenButton_Click(object sender, EventArgs e)

        {

            OpenFileDialog openfile = new OpenFileDialog();

            openfile.Filter = "工作薄(*.xls)|*.xls|所有文件(*.*)|*.*";

            if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK)

            {

                

                if (openfile.FileName.EndsWith(".xls")) 

                {

                    ExcelToDS(openfile.FileName);//保证用户选择的是EXCEL文件

                }

                else

                {

                    MessageBox.Show("请选择.xls结尾的Excel文件");

                    return;

                }

            }

           using (StreamWriter sw = new StreamWriter("e:\\1.html", false, Encoding.Default))

            {

                sw.WriteLine("<html><body>");

                foreach (DataTable table in ExcelToDS(openfile.FileName).Tables)

                {

                    foreach (DataRow row in table.Rows)

                    {

                        foreach (DataColumn column in table.Columns)

                        {

                                if (column.ColumnName == "作业网址")

                                {

                                //<a href="http://www.w3chtml.com/">Visit W3C HTML</a>

                                sw.Write("<a href=\"" + row[column] + "\">");

                                }

                                if (column.ColumnName == "姓名")

                                {

                                    //sw.WriteAsync(row[column] + "</a>");

                                    sw.Write(row[column] + "</a>");

                                }

                        }

                        sw.WriteLine();

                    }

                }

                sw.WriteLine("</body></html>");

            }

        }

        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();

                myCommand.Fill(table1);

                myCommand.Fill(ds);

                dataGridView1.DataSource = table1;

                return ds;

        }

     }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: