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;
}
}
}
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;
}
}
}
相关文章推荐
- C#第三次作业—目标二(Excel数据读取 及 HTML文件初步)
- C#第三次作业:Excel数据读取 及 HTML文件初步
- C#第三次作业 Excel数据读取 及 HTML文件初步
- c#第三次作业——Excel数据读取 及 HTML文件初步(目标三)
- 第三次C#作业-Excel数据读取及HTML文件初步
- 第三次作业:Excel数据读取 及 HTML文件初步
- 第三次作业 Excel数据读取及HTML文件初步 目标2
- c#第三次作业 C#读取Excel文件,并保存成HTML格式
- C#第三次作业:在c#中导入excel,并生成html文件
- 《C#程序设计》第三次作业 C#读取Excel文件,并保存为文本文件
- C#第三次作业-读取excel文件
- C#读取Excel表中的数据时混合字段部分数据没有
- C#中NPOI操作excel之读取和写入excel数据
- C# 使用OLEDB读取不同版本Excel数据的连接字符串
- C# 读取EXCEL文件出现数据丢失问题
- 关于 C# select 读取 Excel 数据 为空,或成科学计数法解决方案
- C#读取Excel数据
- C# 读取EXCEL文件出现数据丢失问题,数据不完整
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C#操作EXCEL之用COM对象读取数据到DataTable