asp.net中使用Excel.dll 客户端读取 Excel 的解决方案 [VS2005]

   项目中有个将Excel表导入到数据库的功能,在参考了Javascript读取和上传文件后数据库对象读取的解决方案后,选择使用Excel.dll的方式进行客户端Excel文件的读取.暂时还不知道性能如何;
   首先是Excel.dll的获取,将Office安装目录下的Excel.exe文件Copy到DotNet的bin目录下,cmd到该目录下,运行 TlbImp EXCEL.EXE Excel.dll 得到Dll文件
   再在项目中引入该dll.
   HTML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>客户端导入Excel数据</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div id="ds" runat="server">
        
<input id="filepath" type="file" runat="server" /><input id="btnTJ" type="button" value="提交" runat="server" onserverclick="btnTJ_ServerClick" />&nbsp;
        
<asp:GridView ID="GridView1" runat="server">
        
</asp:GridView>
    
</div>
    
</form>
</body>
</html>

CS

string excelfilePath = filepath.Value;

        Excel.Application myExcel 
= new Excel.ApplicationClass();
        Excel.Workbooks myBooks 
= myExcel.Application.Workbooks;
        
object oMissing = System.Reflection.Missing.Value;

        Excel.Workbook myBook 
= myBooks.Open(excelfilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

        
//Excel.Workbook myBook = myExcel.Workbooks[1];
        int sheetint = myBook.Worksheets.Count;//能得到sheet的数量

        Excel.Worksheet mySheet 
= (Excel.Worksheet)myBook.Worksheets[1];
        
int rowsint = mySheet.UsedRange.Cells.Rows.Count; //得到行数
        int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数
        System.Data.DataTable dt = new System.Data.DataTable("mytable");
        
for (int i = 1; i < columnsint; i++)
        
{
            dt.Columns.Add(
"F"+i.ToString(), System.Type.GetType("System.String"));
        }

        DataSet myDs 
= new DataSet();
        myDs.Tables.Add(dt);
        DataRow myRow;
        myDs.Clear();
        
        
for (int i = 2; i <rowsint; i++//第一行为标题,不读取
        {             
            myRow 
= myDs.Tables["mytable"].NewRow();
            
for (int j = 1; j <columnsint; j++)
            
{

                Excel.Range r 
= (Excel.Range)mySheet.Cells[i, j];
                
string strValue = r.Text.ToString();                
                
string aa = strValue;
                
string columnname = "F" + j.ToString();
                myRow[columnname] 
= strValue;

            }

            myDs.Tables[
"mytable"].Rows.Add(myRow);
        }

        GridView1.DataSource 
= myDs.Tables["mytable"].DefaultView;
        GridView1.DataBind();

        Process[] procs 
= Process.GetProcessesByName("excel");

        
foreach (Process pro in procs)
        
{
            pro.Kill();
//没有更好的方法,只有杀掉进程
        }
                
        GC.Collect(); 

这样只是初步得到一个解决办法~代码部分是参考了一些网上的资料和一点自身的修改.
Excel.dll
现在还不知道性能如何.明天去试试~
据说还有访问权限等问题;
解决方法的链接;
http://www.cnblogs.com/dengsu888666/archive/2007/12/26/1015060.html
其他解决方案的链接
http://blog.csdn.net/crabo/archive/2005/12/08/547149.aspx
=================================================================

实际测试了下,这种方法在读取小的Excel表时还行,读取超过2000行的严重影响服务器性能,也许是没有采取线程的缘故,100%的占用太可怕了,最后还是用的上传文件用OleDBCommand的方式读取。
也许您有更好的的方案,可以给我留言,大家一起探讨!
posted @ 2009-06-18 13:51  暮色青衫  阅读(906)  评论(0编辑  收藏  举报