按以下编写
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,为何这样?
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,为何这样?
string text = ra.Text.ToString();
Excel.Range ra = ExcelObj.get_Range(excel.Cells[2, 2], excel.Cells[2, 2]);
string text = ra.Text.ToString();
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; //数据
object[] obj = new object[gridView.Columns.Count];
for (int r = 0; r < gridView.RowCount-1; r++)
...{
for (int l = 0; l < gridView.Columns.Count; l++)
...{
if (gridView[l, r].ValueType==typeof(DateTime))
...{
obj[l] = gridView[l, r].Value.ToString();
}
else
...{
obj[l] = gridView[l, r].Value;
}
}
string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2="A"+((int)(r+2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);
ran.Value2 = obj;
}
//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true;
}
finally
...{
//关闭
app.UserControl = false;
app.Quit();
}
return true; }
}
}