惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
O
OpenAI News
V
V2EX
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
S
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
L
LINUX DO - 热门话题
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - Franky
Security Latest
Security Latest
S
SegmentFault 最新的问题
Project Zero
Project Zero
Spread Privacy
Spread Privacy
K
Kaspersky official blog
J
Java Code Geeks
V
Vulnerabilities – Threatpost
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
T
The Exploit Database - CXSecurity.com
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
Scott Helme
Scott Helme
WordPress大学
WordPress大学
量子位
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
S
Secure Thoughts
博客园 - 【当耐特】
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
Visual Studio Blog
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
腾讯CDC
Cyberwarzone
Cyberwarzone
IT之家
IT之家
GbyAI
GbyAI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
云风的 BLOG
云风的 BLOG
T
Troy Hunt's Blog
D
Docker

博客园 - Chris Liu

XP远程桌面连接强制登录 Oracle 存储过程基础 [原]做报表,写Sql语句的一点收获 [原]如何将flash插入到Excel文件中 [原]Excel与OWC做报表总结 [原]OWC做电子表格和图表的试验 [原]Winform自定义控件在网页上的应用 [转]微软面试100题——要想成为盖茨就来试试! 《ActionScript 3 CookBook 简体中文完整版》下载 [原]flash研究(三)——Falsh与JavaScript交互 [原]flash研究(二)——与asp.net服务交互(访问数据库) [原]Flash研究(一)——本地通讯 [转]ORACLE PL/SQ入门 [原]反射学习整理 事件的实现过程 C#基础—— check、lock、using语句归纳 Geoserver汉语版出来啦!! [原]回调实现步骤 [原]Web Service学习
[原]用Excel做报表的试验
Chris Liu · 2008-04-21 · via 博客园 - Chris Liu
 

写这篇文章之前,先提几个问题,希望高手留言指点、讨论:

1、              水晶报表(Crystal Reports)的表头能不能冻结,即:记录多的时候,记录翻页而报表的头部持久显示;如果能怎么实现。

2、              水晶报表在设计的时候表头字段是拖上去的,如果要做一个报表定制工具,这个表头能用程序控制吗?(vs2005自带的水晶报表)

3、              OWC组建在用vs2005Web开发的时候为什么添加不到工具栏,而vs2003可以,vs2005Winform程序也可以。

正是前面提到的问题1和问题2无法解决,现在要把整个系统中的水晶报表换成用Excel实现,Excel实现也存在一些问题,有难度,首先是报表表头的样式的控制,当然这我们可以用宏,但宏是不安全的,所有人都知道,不用宏就只能用VBA语句了,这个对我来说是比较陌生;其次是数据的统计和图表的显示;最后就是用户根据需求自行定制报表,这个灵活性太大,整个模型中数据表就近400张,组织管理细节太多。

先完成了一个小试验,分享一下吧,试验的内容包括:

1、 用简单的VBA语句给Excel中写数据。

2、 Excel放置在Web页面上来操作。

3、 通过不同框架传值来更新Web页面上的Excel数据内容。

4、 控制输出上下标格式的数据。

5、 更新Excel内容时强制结束前一个Excel进程(同时也会结束用户进程,是本实例的一个bug)。

步骤:

  一、新建一个asp.net网站,添加一个纯Html页面Default.htm,在页面上定义两个框架,如下:

二、              在添加一个输入的aspx页面:Input.aspx。在页面上放置一个DropDownList,命名为DDL_Time,和一个Button命名为BN_Query,在cs文件中写下面代码:

  三、再做一个Excel报表的模板,命名为Nomalr.xls,建一个简单的数据库,结构如下:

    四、 在添加一个输出的aspx页面:Output.aspx,在页面上放置一个label,来显示input框架传递过来的参数,在cs文件中添加代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Data.OleDb;
using System.IO;
using System.Diagnostics;

public partial class Output : System.Web.UI.Page
{
    OleDbConnection Olecn;
    OleDbCommand OleCamm;
    DataTable DT 
= new DataTable();

    
private string StrTime = "";
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//接收Input框架传递的参数
        if (!IsPostBack)
        
{
            StrTime 
= Request.QueryString["Time"];
            
if (StrTime != null)
                Label1.Text 
= "时间:" + StrTime;
        }


        
//在页面加载的时候将数据取出
        if (ConnectionDataBase())
        
{
            OleCamm 
= new OleDbCommand();
            OleCamm.Connection 
= Olecn;
            Olecn.Open();
            OleCamm.CommandText 
= "select * from pl where date = " + "'" + StrTime + "'";
            OleDbDataAdapter OleDAdp 
= new OleDbDataAdapter(OleCamm);
            OleDAdp.Fill(DT);
            Olecn.Close();
        }

        Exceltest();
    }

    
    
/// <summary>
    
/// 连接数据库
    
/// </summary>
    
/// <returns></returns>

    private bool ConnectionDataBase()
    
{
        
try
        
{
            
string DataSource = Server.MapPath("~"+ "\\App_Data\\Excel.mdb";
            Olecn 
= new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DataSource);
            Olecn.Open();
        }

        
catch (Exception e1)
        
{
            Response.Write(e1.Message);
            
return false;
        }

        
finally
        
{
            Olecn.Close();
        }

        
return true;
    }


    
/// <summary>
    
/// 对Excel的操作
    
/// </summary>

    private void Exceltest()

    
{
        
//在创建Excel Application前先将已经打开的Excel资源释放掉
        EndExcelProcesse();

        
//指定模板文件
        FileInfo mode = new FileInfo(Server.MapPath("~"+ "\\Nomarl.xls");

        
//打开复制后的文件X
        object missing = Missing.Value;
        Excel.Application myExcel 
= new Excel.Application();

        
//打开模板文件
        myExcel.Application.Workbooks.Open(mode.FullName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

        
//选中有数据的Cells
        Excel.Workbook myBook = myExcel.Workbooks[1];
        Excel.Worksheet mySheet 
= (Excel.Worksheet)myBook.Worksheets[1];
        Excel.Range r 
= mySheet.get_Range(mySheet.Cells[11], mySheet.Cells[DT.Rows.Count + 2, DT.Columns.Count - 3]);
        r.Select();

        
//不单独显示Excel,最后在IE中显示
        myExcel.Visible = false;

        
//第一行为报表的标题
        myExcel.Cells[11= "用模板导出的报表";

        
//逐行写入数据,数组中第一行为报表的列标题
        for (int i = 0; i < DT.Columns.Count - 3; i++)
        
{
            myExcel.Cells[
21 + i] = DT.Columns[i].Caption; ;
        }

        
        
//为报表填充数据并设置显示上下标格式
        for (int i = 0; i < DT.Rows.Count; i++)
        
{
            
for (int j = 0; j < DT.Columns.Count - 4; j++)
            
{
                myExcel.Cells[
3 + i, 1 + j] = DT.Rows[i][j];
            }

            
string a = DT.Rows[i][DT.Columns.Count-4].ToString();
            
string b = DT.Rows[i][DT.Columns.Count-3].ToString();
            
string c = DT.Rows[i][DT.Columns.Count -2].ToString();
            myExcel.Cells[
3 + i, DT.Columns.Count - 3= a + b + c;

            
//控制输出样式为下标
            mySheet.get_Range(mySheet.Cells[i + 3, DT.Columns.Count - 3], mySheet.Cells[i + 3, DT.Columns.Count - 3]).get_Characters(a.Length + 1, b.Length).Font.Subscript = true;

            
//控制输出样式为上标
            mySheet.get_Range(mySheet.Cells[i + 3, DT.Columns.Count - 3], mySheet.Cells[i + 3, DT.Columns.Count - 3]).get_Characters(a.Length + b.Length + 1, c.Length).Font.Superscript = true;
            mySheet.Columns.AutoFit();
        }


        
//在当前目录下指定一个临时文件
        string FileName = Server.MapPath("~"+ "\\Temp.xls";
        
if (File.Exists(FileName))
        
{
            File.Delete(FileName);
        }

        myExcel.Save(FileName);
        mySheet.Cells.Clear() ;
        
//设置不出现保存提示框
        myBook.Saved = true;
        myExcel.Application.Workbooks.Close();

        
//将Excel文件嵌入在IE里面,也可以在aspx页面用
        
//<iframe id="myExcelHtml" src ="E:\\练习\\excell\\WebSite2\\Temp.xls" width="100%" height="100%" runat ="server"></iframe>
        
//标签来嵌入
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType 
= "application/vnd.ms-excel";
        Response.AddHeader(
"Content-Disposition""inline;filename='我的文件'");
        Response.WriteFile(FileName);
        Response.Flush();
        Response.Close();
    }


    
/// <summary>
    
/// 当报表数据更新时,先强制结束前一个报表的数据源
    
/// 这种方法会同时杀死掉用户的excel进程
    
/// </summary>

    protected void EndExcelProcesse()
    
{
        
try
        
{    
            Process[] myProcesses 
= Process.GetProcessesByName("excel");
            
foreach (Process instance in myProcesses)
            
{
                instance.Kill();   
//强制终止    
            }

        }

        
catch { }
        
//在网上查的用下面的方法结束Excel进程表较好,但一直没有效果
        
//try
        
//{
        
//    System.Runtime.InteropServices.Marshal.ReleaseComObject(r);
        
//    System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
        
//    System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
        
//    System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
            
        
//    myBook = null;
        
//    mySheet = null;
        
//    myExcel = null;
        
//    r = null;
        
//    GC.Collect();
        
//}
        
//catch { }
    }


    
}

运行后的结果

如需要源代码,请留下联系方式