c#第三次作業
阿新 • • 發佈:2019-02-13
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;
using System.Data.SqlClient;
namespace Excel
{
public partial class Form1 : Form
{
//全域性變數,檔案全路徑
private string stFilePath = string.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable myT = ExcelToDataTable("D:/ex03_demo.xls", "sheet1");
String mystr = myT.Rows[0][0].ToString();
//用listview1顯示開啟的excel
this.listView1.GridLines = true;//顯示錶格線
this.listView1.View = View.Details;//列表顯示方式
this.listView1.Scrollable = true;//有滾動條
listView1.FullRowSelect = true;//是否選中整行
//設定表頭
for (int i = 0; i < 9; i++)
{
this.listView1.Columns.Add("lie" + i, myT.Rows[0][i].ToString());
}
//設定表的內容
for (int j = 1; j < 47; j++)
{
ListViewItem item = new ListViewItem();
item.SubItems.Clear();
item.SubItems[0].Text = myT.Rows[j][0].ToString();
for (int k = 1; k < 9; k++)
{
item.SubItems.Add(myT.Rows[j][k].ToString());
}
listView1.Items.Add(item);
}
//自適應寬度,-1根據內容設定寬度,-2根據標題設定寬度
for (int i = 0; i < 9; i++)
{
listView1.Columns["lie" + i].Width = -1;
listView1.Columns["lie" + i].Width = -2;
}
}
//將姓名和作業網址用txt存放於bin目錄下
private void button1_Click(object sender, EventArgs e)
{
//此處的文字檔案在工程下Bin的程式集目錄下
stFilePath = Application.StartupPath.Trim() + "//ex03_demo" + ".txt";
StreamWriter swStream;
if (File.Exists(stFilePath))
{
swStream = new StreamWriter(stFilePath);
}
else
{
swStream = File.CreateText(stFilePath);
}
for (int i = 0; i < listView1.Items.Count; i++)
{
for (int j = 4; j <= 6; j += 2)
{
string temp = listView1.Items[i].SubItems[j].Text;
swStream.Write(temp);
//插入------分隔符
swStream.Write("--------");
}
swStream.WriteLine();
}
//關閉流,釋放資源
swStream.Flush();
swStream.Close();
MessageBox.Show("已成功儲存在bin目錄下");
}
public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
//源的定義
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
//Sql語句
//string strExcel = string.Format("select * from [{0}$]", strSheetName); 這是一種方法
string strExcel = "select * from [sheet1$]";
//定義存放的資料表
DataSet ds = new DataSet();
//連線資料來源
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
//適配到資料來源
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
return ds.Tables[strSheetName];
}
}
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;
using System.Data.SqlClient;
namespace Excel
{
public partial class Form1 : Form
{
//全域性變數,檔案全路徑
private string stFilePath = string.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable myT = ExcelToDataTable("D:/ex03_demo.xls", "sheet1");
String mystr = myT.Rows[0][0].ToString();
//用listview1顯示開啟的excel
this.listView1.GridLines = true;//顯示錶格線
this.listView1.View = View.Details;//列表顯示方式
this.listView1.Scrollable = true;//有滾動條
listView1.FullRowSelect = true;//是否選中整行
//設定表頭
for (int i = 0; i < 9; i++)
{
this.listView1.Columns.Add("lie" + i, myT.Rows[0][i].ToString());
}
//設定表的內容
for (int j = 1; j < 47; j++)
{
ListViewItem item = new ListViewItem();
item.SubItems.Clear();
item.SubItems[0].Text = myT.Rows[j][0].ToString();
for (int k = 1; k < 9; k++)
{
item.SubItems.Add(myT.Rows[j][k].ToString());
}
listView1.Items.Add(item);
}
//自適應寬度,-1根據內容設定寬度,-2根據標題設定寬度
for (int i = 0; i < 9; i++)
{
listView1.Columns["lie" + i].Width = -1;
listView1.Columns["lie" + i].Width = -2;
}
}
//將姓名和作業網址用txt存放於bin目錄下
private void button1_Click(object sender, EventArgs e)
{
//此處的文字檔案在工程下Bin的程式集目錄下
stFilePath = Application.StartupPath.Trim() + "//ex03_demo" + ".txt";
StreamWriter swStream;
if (File.Exists(stFilePath))
{
swStream = new StreamWriter(stFilePath);
}
else
{
swStream = File.CreateText(stFilePath);
}
for (int i = 0; i < listView1.Items.Count; i++)
{
for (int j = 4; j <= 6; j += 2)
{
string temp = listView1.Items[i].SubItems[j].Text;
swStream.Write(temp);
//插入------分隔符
swStream.Write("--------");
}
swStream.WriteLine();
}
//關閉流,釋放資源
swStream.Flush();
swStream.Close();
MessageBox.Show("已成功儲存在bin目錄下");
}
public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
//源的定義
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
//Sql語句
//string strExcel = string.Format("select * from [{0}$]", strSheetName); 這是一種方法
string strExcel = "select * from [sheet1$]";
//定義存放的資料表
DataSet ds = new DataSet();
//連線資料來源
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
//適配到資料來源
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
return ds.Tables[strSheetName];
}
}
}