使用owc,请高手给出例子,谢谢!使用.net(c#)

解决方案 »

  1.   

    代码:<input type="hidden" name="out_word" onclick="vbscript:buildDoc" value="导出到word" class="notPrint"> 
    <input type="hidden" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint"> <title>浏览器表格导出到Excel代码</title>
    <input type="button" name="out_word" onclick="vbscript:buildDoc" value="导出到word" class="notPrint"> 
    <input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="导出到excel" class="notPrint"> 
    <table id="data" width="200" border="1">
      <tr>
        <td>11</td>
        <td>11</td>
      </tr>
      <tr>
        <td>22</td>
        <td>22</td>
      </tr>
      <tr>
        <td>33</td>
        <td>33</td>
      </tr>
      <tr>
        <td>44 </td>
        <td>44</td>
      </tr>
    </table>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function AutomateExcel() 
    {
    // Start Excel and get Application object.
    var oXL = new ActiveXObject("Excel.Application"); 
    // Get a new workbook.
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var table = document.all.data; 
    var hang = table.rows.length;var lie = table.rows(0).cells.length; // Add table headers going cell by cell.
    for (i=0;i<hang;i++)
    {
    for (j=0;j<lie;j++)
    {
    oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
    }}
    oXL.Visible = true;
    oXL.UserControl = true;
    }
    //-->
    </SCRIPT>
      

  2.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
      //写入Excel的方法:
      //定义需要参数。
      string SourceFile="Data.XLS";                                //源文件名称。
      string TemplatePath=Server.MapPath("ExcelTemplate");    //存放源文件的文件夹路径。
      string DownloadPath=Server.MapPath("ExcelDownload");    //副本的文件夹路径。
      //副本的文件名。
      string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";  
      object missing = System.Reflection.Missing.Value;
      Excel.Application myExcel=new Excel.Application();
      //打开新文件
      myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
    missing,missing,missing,missing,missing,missing, missing,missing); 
      Excel.Workbook myBook=myExcel.Workbooks[1];
      Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];

      string DownloadFilePath=DownloadPath+"\\"+TempFileName;

      int i=0;
      while (i<=10)
      {
        myExcel.Cells[4+i,2]=i.ToString();
        myExcel.Cells[4+i,3]=i.ToString();
        myExcel.Cells[4+i,4]=i.ToString();
        myExcel.Cells[4+i,5]=i.ToString();
        myExcel.Cells[4+i,6]=i.ToString();
        i++;
      }   

      myBook.Saved=true;
      //myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);

      myBook.PrintPreview(0);
      //myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
      myBook.Close(false, null,null);
      myExcel.Quit();
      System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
      myBook = null;
      myExcel = null;
      GC.Collect();
      //Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
    }
      

  3.   

    不是很明白,只有通过ActiveX来做吧.
      

  4.   

    是从excel中导出吧,我也正需要这代码,谁有分享一下吧