using System;
using System.Data;
using System.Data.OleDb;namespace ArchivesManageSystem
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class ExcelReaderClass
{
DataSet ds = new DataSet();
private int sheetsCount;
public ExcelReaderClass()
{ }
public ExcelReaderClass(string fileName)
{
this.ds.Tables.Clear();
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workBook = excel.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
this.sheetsCount = workBook.Sheets.Count;
this.fillSheets(fileName,workBook);
workBook.Close(false, Type.Missing, Type.Missing);
excel.Quit();
this.KillExcelProcess();
}
public int SheetsCount
{
get {return this.sheetsCount;}
}
public DataSet Data
{
get {return this.ds;}
}
private void fillSheets(string fileName,Excel.Workbook workBook)
{
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+fileName+";Extended Properties=Excel 8.0" ;
OleDbConnection myConn;
try
{
//创建一个数据链接
myConn = new OleDbConnection(strConn) ;
string sql,sheetName;
myConn.Open();
for(int i = 1;i <= workBook.Sheets.Count;i++)
{
try
{
sheetName = ((Excel.Worksheet)workBook.Sheets[i]).Name;
sql = " SELECT * FROM ["+sheetName+"$]" ;
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( sql , myConn ) ;
//得到自己的DataSet对象
myCommand.Fill (ds,sheetName);
}
catch(Exception ex)
{
throw ex;
}
}
//关闭此数据链接
myConn.Close ( ) ;
}
catch(Exception ex)
{
throw ex;
}
}
private void KillExcelProcess()
{
int ProceedingCount = 0;
try
{
System.Diagnostics.Process [] ProceddingCon = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process IsProcedding in ProceddingCon)
{
if(IsProcedding.ProcessName.ToUpper() == "EXCEL")
{
ProceedingCount += 1;
IsProcedding.Kill();
}
}
}
catch(System.Exception err)
{
throw err;
}
} }
}
测试如下:
private void btnOpen_Click(object sender, System.EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = this.openFileDialog1.FileName;
ExcelReaderClass reader = new ExcelReaderClass(this.textBox1.Text);
MessageBox.Show(reader.SheetsCount.ToString());
this.dataGrid1.DataSource = reader.Data.Tables[0];
} }
catch(Exception ex)
{
MessageBox.Show(ex.ToString()); }
}
我在自己的机器上(操作系统是xp)测试完全没有问题
但是今天在别人的机器上(win2000)就读不了了
一直出错
不知道问题出在哪儿?
谢谢
using System.Data;
using System.Data.OleDb;namespace ArchivesManageSystem
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class ExcelReaderClass
{
DataSet ds = new DataSet();
private int sheetsCount;
public ExcelReaderClass()
{ }
public ExcelReaderClass(string fileName)
{
this.ds.Tables.Clear();
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workBook = excel.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
this.sheetsCount = workBook.Sheets.Count;
this.fillSheets(fileName,workBook);
workBook.Close(false, Type.Missing, Type.Missing);
excel.Quit();
this.KillExcelProcess();
}
public int SheetsCount
{
get {return this.sheetsCount;}
}
public DataSet Data
{
get {return this.ds;}
}
private void fillSheets(string fileName,Excel.Workbook workBook)
{
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+fileName+";Extended Properties=Excel 8.0" ;
OleDbConnection myConn;
try
{
//创建一个数据链接
myConn = new OleDbConnection(strConn) ;
string sql,sheetName;
myConn.Open();
for(int i = 1;i <= workBook.Sheets.Count;i++)
{
try
{
sheetName = ((Excel.Worksheet)workBook.Sheets[i]).Name;
sql = " SELECT * FROM ["+sheetName+"$]" ;
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( sql , myConn ) ;
//得到自己的DataSet对象
myCommand.Fill (ds,sheetName);
}
catch(Exception ex)
{
throw ex;
}
}
//关闭此数据链接
myConn.Close ( ) ;
}
catch(Exception ex)
{
throw ex;
}
}
private void KillExcelProcess()
{
int ProceedingCount = 0;
try
{
System.Diagnostics.Process [] ProceddingCon = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process IsProcedding in ProceddingCon)
{
if(IsProcedding.ProcessName.ToUpper() == "EXCEL")
{
ProceedingCount += 1;
IsProcedding.Kill();
}
}
}
catch(System.Exception err)
{
throw err;
}
} }
}
测试如下:
private void btnOpen_Click(object sender, System.EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = this.openFileDialog1.FileName;
ExcelReaderClass reader = new ExcelReaderClass(this.textBox1.Text);
MessageBox.Show(reader.SheetsCount.ToString());
this.dataGrid1.DataSource = reader.Data.Tables[0];
} }
catch(Exception ex)
{
MessageBox.Show(ex.ToString()); }
}
我在自己的机器上(操作系统是xp)测试完全没有问题
但是今天在别人的机器上(win2000)就读不了了
一直出错
不知道问题出在哪儿?
谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货