好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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(" ");                    sw.WriteLine(" ");                    sw.WriteLine(" ");                    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("");                                    name = row[column].ToString();                                    sw.WriteLine(" " + " " + name + " ");                                }                                if (column.ColumnName == "作业网址")                                {                                    sw.WriteLine(" "                                        + ""                                        + row[column].ToString()                                        + "");                                    sw.WriteLine(" ");                                    sw.WriteLine(" ");                                }                            }                        }                    }                    sw.WriteLine(" ");                    sw.WriteLine(" ");                    sw.WriteLine(" ");                }            }        }    }} 


3.运行结果:

查看更多关于第三次作业Excel数据读取及HTML文件初步目标2_html/css_WEB-ITnose的详细内容...

  阅读:38次