以前做单机版的VB6程序中,可以很方便地使用Excel对象,将查询到的数据导出到Excle文件中。
但是现在用基于C#的ASP.NET,却无从下手,那位兄弟有没有相关例子?
但是现在用基于C#的ASP.NET,却无从下手,那位兄弟有没有相关例子?
解决方案 »
- 【讨论】 把页面的隐藏字段(ViewState)保存到Sesssion中好不好
- C#属性(字段)问题,为什么要用get,set,直接用public string Name不行吗
- Java脚本问题
- 用户控件拖到页面后,JavaScript不起作用?
- 关于FCKeditor上传的问题,用过的来帮忙看看~
- 无法连接oracle---System.Data.OracleClient requires Oracle client software version 8.1.7 or greater.
- !TcpChannel构造函数为什么错了
- aspnet_wp.exe当页面一方问时,内存就达到了40多兆!再进行数据访问时,就不断地增加。
- ***************请问关于委托的问题******************
- 有挑战性的工作!高手进来瞧瞧
- .NET中的COM组件和COM+怎么编写啊?小弟在这方面确实没有经验,请高手指教!
- 高分请教asp.net的部署方法
<%@ Page [...] AutoEventWireup="false" AspCompat="true" %>2. 使用 Visual Studio .NET 中的“添加 COM 引用”向导。在COM的选项卡里, 选择Excel Object Library( 这里需要注意版本的问题, Office是向上兼容的,可以选择版本低的dll )3. 添加对转变为CLR元数据的COM组件的命名空间, using Excel;
以下代码是将strData数据, "粘贴"到Excel里,并最后生成destFilePath(指定文件) private void MakeExcel(string strData, string destFilePath)
{
Excel.Workbook xlWorkbook;
Excel.Worksheet xlWorksheet;
Excel.Range xlCell; if( xlApp == null )
xlApp = new Excel.ApplicationClass();
xlApp.Visible = true;
object objNull = System.Reflection.Missing.Value;
xlWorkbook = xlApp.Workbooks.Open( destFilePath, objNull, objNull,
objNull, objNull, objNull,
objNull, objNull, objNull,
objNull, objNull, objNull,
objNull );
this.Application.Lock();
System.Windows.Forms.Clipboard.SetDataObject(strData);
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets[1];
xlCell = (Excel.Range)xlWorksheet.Cells[2,1];
xlCell.Select();
xlWorksheet.Paste(objNull,objNull);
System.Windows.Forms.Clipboard.SetDataObject("");
this.Application.UnLock();
int intEndColumn = xlWorksheet.UsedRange.Columns.Count;
int intEndRow = xlWorksheet.UsedRange.Rows.Count;
Excel.Range xlRange=xlWorksheet.Cells.get_Range(xlWorksheet.Cells[2,1],xlWorksheet.Cells[intEndRow,intEndColumn]); SetExcelBorderStyle( xlRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft) );
SetExcelBorderStyle( xlRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight) );
SetExcelBorderStyle( xlRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop) );
SetExcelBorderStyle( xlRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom) );
SetExcelBorderStyle( xlRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideHorizontal) );
SetExcelBorderStyle( xlRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideVertical) ); xlWorkbook.Close(Excel.XlSaveAction.xlSaveChanges , destFilePath, objNull);
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
GC.Collect();
}
objNull, objNull, objNull,
objNull, objNull, objNull,
objNull, objNull, objNull,
objNull );
以上方法仅适用于Excel 2000很不幸,不同版本的Excel Open的参数是不同的. 你可以使用反射来动态调用它.
Response.Clear();
Response.Buffer= true;
Response.Charset="UTF-8";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
//Response.ContentEncoding=System.Text.Encoding.GetEncoding("big5");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件
Response.Write(strHtml);
Response.End();
多谢各位老大的帮助。
现在程序在本机上运行没有问题,可以在服务器端生成excel文件。但是我把程序放置到租用的空间上后,其他页面都没有问题,唯独运行创建excel文件的页面时出现如下问题:
[Exception: 无法创建 ActiveX 组件。]
Microsoft.VisualBasic.Interaction.CreateObject(String ProgId, String ServerName) +233我的代码目的是在服务器端生成Excel文件,然后让用户去下载。租用的空间负责人说他们已经安装了office,应该可以调用到excel.dll了,怎么还是出现这个问题?上传我的程序时,我把所有文件连带bin目录都上传了。
另外我的网站主要用C#写的页面,而调用生成Excel文件的代码是用VB.NET 写的,生成了一个dll,在同一个解决方案中因此建了两个项目。难道是必须在服务器端手工去注册用VB.NET写的dll?