如何取出Excel数据?
有谁用C#编写过从Excel读取数据的程序(不是往excel写数据),我编了很久就是不能取出数据,可以很容易的把数据写入excel,但是怎么也取不出实际准确的数据,取出来的全是System._ComObject,它是数据类型,而不是实际的数据,不知谁编过这样的程序,能否探讨一下,谢谢!我想或许可以通过转换数据类型来实现,但是没有成功。代码如下: using System;
using System.Data.OleDb;
using System.Data;
using Excel;
using Office;
using System.Windows.Forms;
using System.Diagnostics;
using
using System.Reflection;
using
//using Microsoft.Office.Interop.Excel;
namespace ConsoleApplication8
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
// TOD 在此处添加代码以启动应用程序
string ExcelFileName="D:\\111.xls";
string FilePath = ();
int R;
OleDbConnection Conn = new OleDbConnection(("Provider = microsoft.Jet.OLEDB.4.0;" +"Data Source = " + "D:\\111.mdb"+ ";"));
OleDbDataAdapter DataAdapter = new OleDbDataAdapter();
DataSet Ds = new DataSet();
string InsSQL, DelSQL;
object missing = System.Reflection.Missing.Value;
Excel.Application ExcelApplication = new Excel.Application();
Excel.Workbook workBook = ExcelApplication.Workbooks.Open(ExcelFileName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
Conn.Open();
//将Excel表的数据写入数据库中
try
{
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
DelSQL = "Delete From 系统设置 ";
OleDbCommand Dmd = new OleDbCommand(DelSQL, Conn);
Dmd.ExecuteNonQuery();
R = 1;
for(R=1;R<5;R++)
{
InsSQL = "Insert Into 系统设置 (项目, 值) Values('"+workSheet.Cells[R+2,3]+ "','"+workSheet.Cells[R+2,3] +"')";
OleDbCommand Imd = new OleDbCommand(InsSQL, Conn);
Imd.ExecuteNonQuery();
}
}
catch(Exception e)
{
MessageBox.Show("导入错误出现"+"\n"+e.Message,"数据导入",MessageBoxButtons.OK);
}
workBook.Close(missing,missing,missing);
ExcelApplication.Quit();
Conn.Close();
}
}
}