操作后关闭EXCEL和释放资源GC.Collect().

解决方案 »

  1.   

    oleConn.Close()没有调用,这样的话,backup.xls已经在进程中,从资源管理器可以看到excel.exe在进程中。
      

  2.   

    自己手动建立一个excel的话,没有建立相应的表结构,
    下面插入数据的话找不到相应的字段
    insert into Sheet1$
      

  3.   

       OdbcConnection MyConn = new OdbcConnection();
                MyConn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DBQ=" + Server.MapPath("/Excel.xls");
                OdbcCommand MyComm = new OdbcCommand();
                MyComm.Connection = MyConn;
                MyComm.CommandText = "SELECT * FROM [Sheet1$]";
                OdbcDataAdapter MyODA = new OdbcDataAdapter(MyComm);
                DataTable MyTable = new DataTable();
                MyODA.Fill(MyTable);
                MyConn.Close();
      

  4.   

     1, string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";" + @"Extended Properties=""Excel 8.0;HDR=Yes;""";
    要先创建文件,backup.xls文件,否则strConnExcel就报错说没有文件,电脑不会自动寻找创建的
    2WinFomr程序占据数据,不关闭,就不能释放资源,数据库就无法写入excel
      

  5.   

    只是插入的话,就直接用XML或者Html写吧,还简单些
      

  6.   

    附:Excel Html:<html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40"><head>
    <meta http-equiv=Content-Type content="text/html; charset=gb2312">
    <meta name=ProgId content=Excel.Sheet>
    <meta name=Generator content="Microsoft Excel 11">
    <link rel=File-List href="TaskList_files/filelist.xml">
    <link rel=Edit-Time-Data href="TaskList_files/editdata.mso">
    <link rel=OLE-Object-Data href="TaskList_files/oledata.mso">
    <!--[if gte mso 9]><xml>
     <o:DocumentProperties>
      <o:Author>cx15</o:Author>
      <o:LastAuthor>cx15</o:LastAuthor>
      <o:Created>2008-06-20T02:32:29Z</o:Created>
      <o:LastSaved>2008-11-25T05:30:19Z</o:LastSaved>
      <o:Company>Microsoft Corporation</o:Company>
      <o:Version>11.9999</o:Version>
     </o:DocumentProperties>
    </xml><![endif]-->
    <style>
    <!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
    @page
    {margin:1.0in .75in 1.0in .75in;
    mso-header-margin:.5in;
    mso-footer-margin:.5in;}
    tr
    {mso-height-source:auto;
    mso-ruby-visibility:none;}
    col
    {mso-width-source:auto;
    mso-ruby-visibility:none;}
    br
    {mso-data-placement:same-cell;}
    .style0
    {mso-number-format:General;
    text-align:general;
    vertical-align:middle;
    white-space:nowrap;
    mso-rotate:0;
    mso-background-source:auto;
    mso-pattern:auto;
    color:windowtext;
    font-size:12.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:宋体;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    border:none;
    mso-protection:locked visible;
    mso-style-name:Normal;
    mso-style-id:0;}
    td
    {mso-style-parent:style0;
    padding-top:1px;
    padding-right:1px;
    padding-left:1px;
    mso-ignore:padding;
    color:windowtext;
    font-size:12.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:宋体;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    mso-number-format:General;
    text-align:general;
    vertical-align:middle;
    border:none;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-protection:locked visible;
    white-space:nowrap;
    mso-rotate:0;}
    ruby
    {ruby-align:left;}
    rt
    {color:windowtext;
    font-size:9.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:宋体;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    mso-char-type:none;
    display:none;}
    -->
    </style>
    <!--[if gte mso 9]><xml>
     <x:ExcelWorkbook>
      <x:ExcelWorksheets>
       <x:ExcelWorksheet>
        <x:Name>Sheet1</x:Name>
        <x:WorksheetOptions>
         <x:DefaultRowHeight>285</x:DefaultRowHeight>
         <x:Selected/>
         <x:Panes>
          <x:Pane>
           <x:Number>3</x:Number>
           <x:ActiveRow>4</x:ActiveRow>
           <x:ActiveCol>1</x:ActiveCol>
          </x:Pane>
         </x:Panes>
         <x:ProtectContents>False</x:ProtectContents>
         <x:ProtectObjects>False</x:ProtectObjects>
         <x:ProtectScenarios>False</x:ProtectScenarios>
        </x:WorksheetOptions>
       </x:ExcelWorksheet>
       <x:ExcelWorksheet>
        <x:Name>Sheet2</x:Name>
        <x:WorksheetOptions>
         <x:DefaultRowHeight>285</x:DefaultRowHeight>
         <x:ProtectContents>False</x:ProtectContents>
         <x:ProtectObjects>False</x:ProtectObjects>
         <x:ProtectScenarios>False</x:ProtectScenarios>
        </x:WorksheetOptions>
       </x:ExcelWorksheet>
       <x:ExcelWorksheet>
        <x:Name>Sheet3</x:Name>
        <x:WorksheetOptions>
         <x:DefaultRowHeight>285</x:DefaultRowHeight>
         <x:ProtectContents>False</x:ProtectContents>
         <x:ProtectObjects>False</x:ProtectObjects>
         <x:ProtectScenarios>False</x:ProtectScenarios>
        </x:WorksheetOptions>
       </x:ExcelWorksheet>
      </x:ExcelWorksheets>
      <x:WindowHeight>11820</x:WindowHeight>
      <x:WindowWidth>14955</x:WindowWidth>
      <x:WindowTopX>480</x:WindowTopX>
      <x:WindowTopY>15</x:WindowTopY>
      <x:ProtectStructure>False</x:ProtectStructure>
      <x:ProtectWindows>False</x:ProtectWindows>
     </x:ExcelWorkbook>
    </xml><![endif]-->
    </head><body link=blue vlink=purple><table x:str border=0 cellpadding=0 cellspacing=0 width=120 style='border-collapse:
     collapse;table-layout:fixed;width:90pt'>
     <col width=120 style='mso-width-source:userset;mso-width-alt:3840;width:90pt'>
     <tr height=19 style='height:14.25pt'>
      <td height=19 width=120 style='height:14.25pt;width:90pt'>Cellvalue here</td>
     </tr>
     <tr height=19 style='height:14.25pt'>
      <td height=19 style='height:14.25pt'>Cellvalue here</td>
     </tr>
     <![if supportMisalignedColumns]>
     <tr height=0 style='display:none'>
      <td width=120 style='width:90pt'></td>
     </tr>
     <![endif]>
    </table></body></html>