using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.IO;
namespace zhoujing.baseclass
{
public class common
{
#region 选择文件
/// <summary>
/// 打开文件对话框
/// </summary>
/// <param name="filter">文件的格式</param>
/// <param name="ControlName">控件名称:</param>
/// <param name="ControlName">0:ExcelPath</param>
/// <param name="ControlName">1:AccessPath</param>
public static void OpenFileDialog(string filter, TextBox Name)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = filter;
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
Name.Text =
openFileDialog1.FileName.ToString();
myStream.Close();
}
}
catch
{
string errorMessages = "操作的文件正在被使用!";
MessageBox.Show(errorMessages);
}
}
}
#endregion
#region 操作Access
public static string GetExtensionFromFileName(string filePath)
{
if (filePath != string.Empty)
{
FileInfo eFile = new FileInfo(filePath);
return eFile.Extension;
}
else
{
return "";
}
}
public static DataSet GetFileDataSet(string filePath, string accessSql)
{
string extensionOfFile = GetExtensionFromFileName(filePath);
string connectionString = string.Empty;
DataSet dt = null;
if (extensionOfFile == ".xls")
{
connectionString = "provider=Microsoft.ACE.OLEDB.12.0;" +
"data source=" + filePath + ";Extended Properties=Excel 12.0";
}
else if (extensionOfFile == ".xlsx")
{
connectionString = "provider=Microsoft.ACE.OLEDB.12.0;" +
"data source=" + filePath + ";Extended Properties=Excel 12.0";
}
try
{
OleDbConnection cn = new OleDbConnection(connectionString);
cn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(accessSql, cn);
DataSet ds = new DataSet();
da.Fill(ds);
dt = ds;
}
catch
{
string errorMessages = "读取文件出错,请检查该文件是否正确!";
MessageBox.Show(errorMessages);
}
return dt;
}
#endregion
}
}
如需定做或者获取更多资料,请联系QQ:375279829