c#第三次作业——Excel数据读取 及 HTML文件初步(目标三)

《C#程序设计》第三次作业: Excel数据读取 及 HTML文件初步

【目的】
1. 了解C#读取Excel数据的方法。
2. 提高自己通过搜索解决问题的能力。

【目标】
目标1:C#读取Excel文件(80分)
目标2:C#读取Excel文件,并保存成HTML格式(90分)。
目标3: C#读取Excel文件, 并保存成HTML格式,同时把Excel数据保存到MySQL数据库中 (100分)。

【方法】
1. 百度、Google进行搜索。
2. 周五(物联网)、下周二(信管)的课堂上会进行较为详细的介绍。 

【作业要求】
1. C#读取Excel文件(样本文件:ex03_demo.xls
  http://pan.baidu.com/s/1rXDoE
    读取ex03_demo.xls文件中的“ 姓名”和“作业网址”,保存到文本文件中(文件名:ex03_demo.txt)。
2. 保存成HTML格式
    把“姓名”和“作业网址”,转换为HTML识别的链接形式,相应的HTML文件名为 ex03_demo.html。
    HTML文件的格式可以查看网络资料,最终的展现形式样本(链接源代码) 。
3. 把Excel数据保存到MySQL数据库中
    把 ”班级“、“学号”、“姓名”、“作业网址”信息写入到MySQL数据库中。

    需要安装MySQL数据库;创建相应的数据库及Table;C#连接MySQL;C#向MySQL数据库写入相应数据。


心得体会:

在连接SQL数据库那里出了点小问题,一直提示将截取字符串或二进制数据,语句已终止,后来发现原来是数据库里面定义的列字符大小不够,录入网址时超出了100个字符(开始定义为100),后来修改为300就没问题了。总体来说,编写过程比较顺利。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace ExcelReader
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            String connection = "Data Source=Sam-PC;Initial Catalog=sm;User Id=sa;Password=123;";
            connectionObj = new SqlConnection(connection);
            connectionObj.Open();
        }
        public SqlConnection connectionObj;
        public DataTable table1 = new DataTable();
        private FileStream fs;
        private StreamWriter sw;
        private void btn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = "工作簿(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK)
                ExcelToDs(openfile.FileName);
            if (!File.Exists("F:\\Sam\\素材\\c#\\ex03_demo.html"))
            {
                fs = new FileStream("F:\\Sam\\素材\\c#\\ex03_demo.html", FileMode.Create, FileAccess.Write);
            }
            else
            {
                fs = new FileStream("F:\\Sam\\素材\\c#\\ex03_demo.html", FileMode.Open, FileAccess.Write);
            }
            sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
            sw.WriteLine("<html><head><title>学生连接</title></head><body>");
            foreach (DataRow dr in table1.Rows)
            {
                sw.WriteLine("<a href=\"" + dr["网址"] + "\">" + dr["姓名"] + "</a>");
                string sql = "insert into a values('" + dr["姓名"] + "','" + dr["网址"]  + "')";
                SqlCommand cmd = new SqlCommand(sql, connectionObj);
                cmd.ExecuteNonQuery(); 
                //insertToSql(dr["名字", dr["网址"]]);
            }
            sw.WriteLine("</body></html>");
            sw.Close();
            fs.Close();
            connectionObj.Close();
        }
        public DataSet ExcelToDs(string path)
        {
            string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + @path + ";" + "Extended Properties=Excel 12.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);
            dataGridView1.DataSource = table1;
            return ds;
        }

        private void openfile_FileOk(object sender, CancelEventArgs e)
        {

        }
    }
}


<img src="https://img-blog.csdn.net/20150502230237517?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHlkeV8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" style="font-family: punctuation, 微软雅黑, Tohoma; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);" />



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值