将后台数据读取到前台的EXCEL文件中去,用javascript实现.(发一个原创)

本文介绍如何利用ASP.NET结合C#实现与Microsoft Excel的交互操作,包括创建工作簿、添加工作表、设置单元格值及格式等。通过具体代码示例展示了动态生成Excel文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">

<%
    Response.Write(
"请加入信任站点");
 
%>
        
<script language="javascript" type="text/javascript">
             
var xlApp,xlBook,xlSheet;
             xlApp 
= new ActiveXObject("Excel.Application");
             xlBook 
= xlApp.Workbooks.Add();
             xlBook.Worksheets.Add();
             xlBook.Worksheets.Add();
             
var quArray = new Array(" ","1","2","3","4","5");    
             
var i;
             
for( i=1;i<quArray.length;i++)
            {
                  xlSheet 
= xlBook.Worksheets(i);
                  xlSheet.Name 
= quArray[i];
             }
               
         
<%
              
for(int j=1;j<=5;j++)
              {               
         
%>
               xlSheet 
= xlBook.Worksheets(quArray[<%=j%>]);
               i
=1;
               xlSheet.Cells(i
++,1).Value = "小名";
               xlSheet.Cells(
1,2).Value = "d";
               xlSheet.Cells(
1,3).Value = "a";
               xlSheet.Cells(
1,4).Value = "c";
               
               
var con = i-1;        
               xlSheet.Rows(
"1:"+con).Font.Size = 9;
               xlSheet.Rows(
"1:"+con).RowHeight = 15;
               xlSheet.Range(
"A1:D1").Interior.ColorIndex = 15;
               xlSheet.Range(
"A1:D1").Interior.Pattern = 1;
               xlSheet.Range(
"A1:D1").Font.ColorIndex = 1;
               xlSheet.Range(
"A1:D1").HorizontalAlignment = 3;   
               xlSheet.Range(
"A1:D"+con).Borders.Weight = 2;
               xlSheet.Range(
"A1:D"+con).Borders.ColorIndex = 1;
               xlSheet.Columns(
"A:D").EntireColumn.AutoFit;               

<%
     }
%>
              
    xlApp.Visible 
= true;
    idTmr 
= window.setInterval("Cleanup();",1);
function Cleanup() {
   window.clearInterval(idTmr);
   CollectGarbage();
 }
  window.opener
=null;
window.close();
            
        
</script>

    
</form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值