在家用笔记本搞好的程序,到公司后添加了Microsoft Excel11 Object Libray之后,发现引用里面的” Microsoft.Office.Interop.Excel”不能用。重新引用也不行,
通过执行tlbimp excel.exe后可以得到excel.dll文件。
cd\到根目录
cd program files\........vs 的bin 下面
tlbimp excel.exe
使用Tlpimp.exe和Excel.olb来生成Excel.dll,然后引用。
搞了半天还是不行,
后来终于看到了一篇上面说是安装office2003的时候要选择“.Net可编程性支持”。后来把office2003修改了下,重新选择
1、 安装office2003的时候,请选择“选择应用程序的高级自定义”:
2、请选择“.Net可编程性支持”下选择菜单的“从本机运行全部程序”。

Code
1
先得到数组,一次填充快多哒!!!
2
objRecordset.MoveFirst();
3
for (int i = 0; i < nRows; i++)
4
{
5
for (int j = 0; j <5; j++)
6
{
7
a = s[j];
8
strValues[i, j] = objRecordset.GetFieldValue(a).ToString();
9
10
}
11
if (objRecordset.IsEOF() != true)
12
{
13
14
objRecordset.MoveNext();
15
}
16
}
17
18
Excel.Range StringRange = (Excel.Range)oSheet.Cells[9, 1];//从第9行第一列开始填充
19
20
StringRange = StringRange.get_Resize(nRows, 5);//填充范围为记录集行数,5列
21
22
StringRange.Value2 = strValues;
总算可以了,哈哈。。。。。。。。。。。。。。。。。。
把从supermap 中查询到的记录集添加到窗体中的excel 中,不能直接调用excel,不然用户会觉得你在忽悠他,
可以采用另外的控件(这一次叫它 axOA1),好了不多写了,上班时间,老大看到会骂人嘀

Code
1
1axOA1 操作
2
2
3
3 using System.Reflection;
4
4using Excel = Microsoft.Office.Interop.Excel;
5
5using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
6
6
7
7
8
8 ExcelApplication oExcel = new ExcelApplication();
9
9 Excel.Workbook oBook;//= new Microsoft.Office.Interop.Excel.Workbook();
10
10 Excel.Worksheet oSheet;// = new Microsoft.Office.Interop.Excel.Worksheet();
11
11 Excel.Range oRange;
12
12
13
13 AppPath = Application.StartupPath + "\\Template\\Sample.xls";
14
14 axOA1.Open(AppPath);
15
15
16
16
17
17 oBook = (Excel.Workbook)axOA1.GetIDispatch();
18
18 oExcel = oBook.Application;
19
19 oSheet = (Excel.Worksheet)oBook.Worksheets[1];
20
20
21
21
22
22 // oRange.Select();
23
23
24
24 nRows = objRecordset.RecordCount;
25
25 objFieldInfos=objRecordset .GetFieldInfos();
26
26
27
27 nCols = objRecordset.GetFieldInfos().Count;
28
28 string[] s=new string [nCols];
29
29 string[,] strValues1 = new string[nRows, nCols];
30
30
31
31 double[,] dSum = new double[nRows, nCols];
32
32
33
33 oSheet.Cells[1, 1] = "(" + Common.strDtName + "统计表)" + Common.strQuery;
34
34 oRange = oSheet.get_Range("A1", "E1");
35
35
36
36 oRange.Font.Size = 20;
37
37 // oRange.Font.Color = (object)Color.Red;
38
38
39
39 if (nRows>0 &&nCols>0)
40
40
{
41
41 for (j = 1; j <=nCols;j++ )
42
42
{
43
43 s[j-1] = objFieldInfos[j].Name;
44
44 }
45
45
46
46 objRecordset.MoveFirst();
47
47 for (i = 1; i <=nRows; i++)
48
48
{
49
49 for (j =1; j <=nCols; j++)
50
50
{
51
51
52
52 string a = s[j-1];
53
53 strValues1[i-1, j-1] = objRecordset.GetFieldValue(a).ToString();
54
54 oSheet.Cells[i + 1, j] = strValues1[i - 1, j - 1];
55
55 if (Common.bSum == true)
56
56
{
57
57 //dSum+=strValues1[i-1]
58
58 }
59
59 }
60
60 objRecordset.MoveNext();
61
61 }
62
62 }
63
63