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

WBOY
Release: 2016-06-21 09:13:29
Original
889 people have browsed it

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


3.运行结果:


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!