第三次作业 Excel数据读取及HTML文件初步 目标2_html/css_WEB-ITnose

WBOY
發布: 2016-06-21 09:13:29
原創
958 人瀏覽過

1.功能: 

1)导入excel文件内容: 
2)导出excel.txt文件: 
3)导出excel.html文本文件:


2.源程序如下:

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 DataSet ReadExcel(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;        }        private void button1_Click(object sender, EventArgs e)        {            OpenFileDialog openfile = new OpenFileDialog();            openfile.Filter = "工作薄(*.xls)|*.xls|所有文件(*.*)|*.*|工作薄(*.xlsx)|*.xlsx";            if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK)            {                DataSet dataSet = ReadExcel(openfile.FileName);                String name = "";                using (StreamWriter sw = new StreamWriter(@"C:\Users\Administrator.LBDZ-20121019BZ\Documents\学习\C#\3\excel.txt", false, Encoding.Default))                {                    foreach (DataTable table in dataSet.Tables)                    {                        foreach (DataRow row in table.Rows)                        {                            foreach (DataColumn column in table.Columns)                            {                                //Console.WriteLine(row[column]);                                if (column.ColumnName == "姓名")                                {                                    name = row[column].ToString();                                }                                if (column.ColumnName == "作业网址")                                {                                    sw.WriteLine(name + ":" + row[column].ToString() + ";");                                }                            }                        }                    }                }                using (StreamWriter sw = new StreamWriter(@"C:\Users\Administrator.LBDZ-20121019BZ\Documents\学习\C#\3\excel.html", false, Encoding.Default))                {                    sw.WriteLine("<html>");                    sw.WriteLine("<body>");                    sw.WriteLine("<table cellPadding=1 align=center>");                    foreach (DataTable table in dataSet.Tables)                    {                        foreach (DataRow row in table.Rows)                        {                            foreach (DataColumn column in table.Columns)                            {                                //Console.WriteLine(row[column]);                                if (column.ColumnName == "姓名")                                {                                    // sw.Write("<a href=\"" + row[column] + "\">");                                    name = row[column].ToString();                                    sw.WriteLine("<tr>" + "<td>" + name + "</td>");                                }                                if (column.ColumnName == "作业网址")                                {                                    sw.WriteLine("<td>"                                        + "<A Href=\"" + row[column].ToString()                                        + "\">"                                        + row[column].ToString()                                        + "</a>");                                    sw.WriteLine("</td>");                                    sw.WriteLine("</tr>");                                }                            }                        }                    }                    sw.WriteLine("</table>");                    sw.WriteLine("</body>");                    sw.WriteLine("</html>");                }            }        }    }}
登入後複製


3.运行结果:


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板