51Testing软件测试论坛
标题:
asp.net打开excel文件
[打印本页]
作者:
lcghy
时间:
2005-1-25 09:32
标题:
asp.net打开excel文件
我用asp.net想要打开,放在根目录下的excel,代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
openExcel(exercise(Server.MapPath(".").ToString())[0].ToString());
}
private ArrayList exercise(string strPath)
{
ArrayList list = new ArrayList();
System.IO.DirectoryInfo dir=new System.IO.DirectoryInfo(strPath);
FileInfo[] fiarr = dir.GetFiles();
foreach(FileInfo fri in fiarr)
{
if(fri.Extension.ToString()==".xls"||fri.Extension.ToString()==".csv")
{
list.Add(fri.DirectoryName);
}
}
return list;
}
private void openExcel(string path)
{
Excel.Application oXL = new Excel.Application();
Excel._Workbook wb;
wb = oXL.Workbooks.Open(path,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);
}
报错信息是"Excel 无法访问“根目录的文件夹”。该文档可能为只读或加密文档。",请问是什么问题?
作者:
baitest
时间:
2005-1-25 10:55
文档的属性或者共享类的东西看一下,是否有用!
作者:
celine
时间:
2005-1-31 09:46
我找个例子,不知道对你是不是有用,发上来大家一起学习
老外操作的是Excel 2000,所有Excel的程序操作都来源于Excel的对象库Excel9.olb.本例也只是对这个东东做一个简单的操作了解。
首先的一步就是使用Tlbimp.exe这个工具将Excel9.0的对象库文件Excel8.olb转换成为dll,这样才能做为.Net平台Assembly来使用:)操作如下:
TlbImp Excel9.olb Excel.dll 只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。
下面就让我们具体看看C#是如何使用这些东东吧。
1. 创建一个新Excel的Application:
Application exc = new Application();if (exc == null) {Console.WriteLine("ERROR: EXCEL couldn't be started");return 0;}2. 让这个工程可见:
exc.set_Visible(0, true); 3. 获取WorkBooks集合:
Workbooks workbooks = exc.Workbooks; 4. 加入新的WorkBook:
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); 5. 获取WorkSheets集合:
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);if (worksheet == null) {Console.WriteLine ("ERROR in worksheet == null");}6. 给单元格设置变量:
Range range1 = worksheet.get_Range("C1", Missing.Value); if (range1 == null) { Console.WriteLine ("ERROR: range == null"); } const int nCells = 1; Object[] args1 = new Object[1]; args1[0] = nCells; range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);例程:
using System;using System.Reflection; using System.Runtime.InteropServices; using Excel; class Excel { public static int Main() { Application exc = new Application(); if (exc == null) { Console.WriteLine("ERROR: EXCEL couldn't be started!"); return 0; } exc.set_Visible(0, true); Workbooks workbooks = exc.Workbooks; _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); Sheets sheets = workbook.Worksheets; _Worksheet worksheet = (_Worksheet) sheets.get_Item(1); if (worksheet == null) { Console.WriteLine ("ERROR: worksheet == null"); } Range range1 = worksheet.get_Range("C1", Missing.Value); if (range1 == null) { Console.WriteLine ("ERROR: range == null"); } const int nCells = 1; Object[] args1 = new Object[1]; args1[0] = nCells;range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1); return 100; }}现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;
const int nCell = 5;Range range2 = worksheet.get_Range("A1", "E1"); int[] array2 = new int [nCell]; for (int i=0; i < array2.GetLength(0); i++) { array2[i] = i+1; } Object[] args2 = new Object[1]; args2[0] = array2; range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);
输出结果:
总结:
大家需要了解Tlbimp.exe 这个工具的使用啊:)这个东东很有用,可以将普通Win32程序移植到.Net下面来:)
作者:
lcghy
时间:
2005-1-31 10:04
谢谢,斑竹!
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2