using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.IO;
namespace ExcelReadandWriteHtml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "工作薄(*.xls)|*.xls|所有文件(*.*)|*.*";
if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK)
ExcelToDS(openfile.FileName);
}
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);
dataGridView1.DataSource = table1;
return ds;
}
private void PrintRows(DataSet dataSet)
{
using (StreamWriter sw = new StreamWriter("F:/ex03_demo.html", false, Encoding.Default))
{
String strName = "隔壁家老王";
String strWebsite = "http:\\807661447.zone.qq.com";
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
if (column.ColumnName == "姓名")
strName = (String)row[column];
if (column.ColumnName == "作业网址")
strWebsite = (String)row[column];
}
sw.WriteLine(strName + " " + strWebsite);
}
}
// Read and show each line from the file.
}
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
C#第三次作业 Excel数据读取 及 HTML文件初步
最新推荐文章于 2024-01-19 16:36:54 发布