日期:2014-05-19  浏览次数:20801 次

如何读取excel的cells中值?
按以下编写
Excel.ApplicationClass   ExcelObj=   new   Excel.ApplicationClass();
Excel.Workbook   theWorkbook   =   ExcelObj.Workbooks.Open( "E:\\a.xls ",   0,   true,   5, " ",   " ",   true,   Excel.XlPlatform.xlWindows,   "\t ",   false,   false,0,   true,true,true);
Excel.Sheets   sheets   =   theWorkbook.Worksheets;
Excel.Worksheet   worksheet   =   (Excel.Worksheet)sheets.get_Item(1);
this.label1.Text=   worksheet.Cells[2,2].ToString();

运行结果:label1中得到的内容为:System.__ComObject,为何这样?


------解决方案--------------------
帮顶3下
------解决方案--------------------
你看看能不能用ODBC进行操作
------解决方案--------------------
错了
Excel.Range ra = ExcelObj.get_Range(excel.Cells[2, 2], excel.Cells[2, 2]);
string text = ra.Text.ToString();
------解决方案--------------------
我写了一个关于如何将DataView 到处为Excel的静态函数,希望对你有用。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ExcelExports
...{
public class ExcelExports
...{
/**//// <summary>
/// 把DataGridView到处到Excel中
/// </summary>
/// <param name= "gridView "> 目标DataGridView </param>
/// <param name= "fileName "> 保存文件名称 </param>
/// <param name= "isShowExcle "> 是否显示Excel界面 </param>
/// <returns> 导出是否成功 </returns>
public static bool ExportForDataGridview(DataGridView gridView,string fileName,bool isShowExcle)
...{

//建立Excel对象
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
try
...{
if (app == null)
...{
return false;
}
app.Visible = isShowExcle;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
...{
return false;
}
string sLen = " ";
//取得最后一列列名
char H=(char)(64+gridView.ColumnCount /26 );
char L = (char)(64 + gridView.ColumnCount % 26);
if (gridView.ColumnCount < 26)
...{
sLen = L.ToString();
}
else
...{
sLen = H.ToString() + L.ToString();
}


//标题
string sTmp = sLen + "1 ";
Range ranCaption = worksheet.get_Range(sTmp, "A1 ");
string[] asCaption = new string[gridView.ColumnCount];
for (int i = 0; i < gridView.ColumnCount; i++)
...{
asCaption[i] = gridView.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption;